QOJ.ac

QOJ

Time Limit: 6.0 s Memory Limit: 2048 MB

#22863. 空交集

Statistics

給定兩個整數序列 $A = (A_1, \dots, A_N)$ 與 $B = (B_1, \dots, B_N)$,以及兩個正整數序列 $V = (V_1, \dots, V_N)$ 與 $W = (W_1, \dots, W_N)$。這四個序列的長度皆為 $N$。

請選擇兩個(可能為空的)指標區間:$[L, R)$ 對應 $A$,$[U, D)$ 對應 $B$。其中,$L, R, U, D$ 為滿足 $1 \le L \le R \le N + 1$ 且 $1 \le U \le D \le N + 1$ 的整數。

令 $X$ 與 $Y$ 分別表示在這兩個區間中出現的值所成的集合:

$$ \begin{aligned} X &= \{A_i \mid L \le i < R\}, \\ Y &= \{B_j \mid U \le j < D\}. \end{aligned} $$

所選區間必須滿足 $X \cap Y = \emptyset$。換句話說,沒有任何值可以同時出現在兩個區間中。

若 $L = R$,則 $A$ 的區間為空且 $X = \emptyset$。同樣地,若 $U = D$,則 $B$ 的區間為空且 $Y = \emptyset$。

所選兩個區間的分數為

$$ \sum_{i=L}^{R-1} V_i + \sum_{j=U}^{D-1} W_j. $$

請在所有合法的選擇中,求出可能的最大分數。

輸入格式

輸入包含一或多個測試資料。輸入的第一行包含一個整數 $T$($1 \le T \le 10^4$),表示測試資料的數量。接下來是 $T$ 組測試資料的描述,每組格式如下:

$$ \begin{aligned} &N \\ &A_1 \ A_2 \ \dots \ A_N \\ &B_1 \ B_2 \ \dots \ B_N \\ &V_1 \ V_2 \ \dots \ V_N \\ &W_1 \ W_2 \ \dots \ W_N \end{aligned} $$

第一行包含一個整數 $N$,代表四個序列的共同長度($1 \le N \le 2 \times 10^5$)。

接下來的四行每行包含 $N$ 個整數,分別描述序列 $A$、$B$、$V$ 與 $W$。

整數 $A_i$ 與 $B_i$ 滿足 $1 \le A_i, B_i \le N$(對於每個 $i$,$1 \le i \le N$)。

整數 $V_i$ 與 $W_i$ 滿足 $1 \le V_i, W_i \le 10^9$(對於每個 $i$,$1 \le i \le N$)。

所有測試資料中 $N$ 的總和不會超過 $6 \times 10^5$。

輸出格式

輸出 $T$ 行。第 $i$ 行應包含第 $i$ 組測試資料的最大可能分數。

範例

輸入格式 1

4
3
1 2 3
3 2 1
3 4 5
6 2 1
3
1 1 1
1 1 1
2 3 4
5 5 5
4
1 2 1 3
4 2 4 3
5 6 5 1
4 10 4 10
5
3 1 2 4 3
1 1 2 2 4
1 3 3 2 1
2 2 3 1 1

輸出格式 1

13
15
33
11

說明

在第一組測試資料中,我們可以選擇區間 $[1, 3)$(對應 $A$)以及區間 $[1, 2)$(對應 $B$)。更精確地說,我們可以設定 $L = 1$、$R = 3$、$U = 1$ 與 $D = 2$。它們的值集合分別為 $\{1, 2\}$ 與 $\{3\}$,彼此互斥。分數為

$$ V_1 + V_2 + W_1 = 3 + 4 + 6 = 13, $$

這是可能的最大分數。

在第二組測試資料中,我們可以為 $A$ 選擇一個空區間,並選擇完整的區間 $[1, 4)$(對應 $B$)。更精確地說,即 $L = 2$、$R = 2$、$U = 1$ 與 $D = 4$。

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
#2891EditorialOpensolnullptr_qwqView

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.