Universal Cup Judging System

Universal Cup

حد الوقت: 2 s حد الذاكرة: 1024 MB مجموع النقاط: 100 قابلة للهجوم ✓
الإحصائيات

畀定兩個長度分別係 $2n$ 同 $2m$ 嘅整數序列 $A$ 同 $B$。所有元素都係非零整數。

序列 $A = (a_1, a_2, \dots, a_{2n})$ 滿足以下條件:對於每個滿足 $1 \le i \le n$ 嘅整數 $i$,恰好有兩個下標 $j$ 滿足 $|a_j| = i$。特別地,$\{|a_j| : 1 \le j \le 2n\} = \{1, 2, \dots, n\}$,而且如果唔考慮正負號,每個絕對值恰好出現兩次。

序列 $B = (b_1, b_2, \dots, b_{2m})$ 滿足類似嘅條件:對於每個滿足 $1 \le i \le m$ 嘅 $i$,恰好有兩個下標 $j$ 滿足 $|b_j| = i$。

你可以對序列 $A$ 以任意順序進行任意多次以下操作:

  1. 循環移位。 將當前序列視為一個環,並進行旋轉: $$(a_1, a_2, \dots, a_{2k}) \longrightarrow (a_{r+1}, a_{r+2}, \dots, a_{2k}, a_1, \dots, a_r)$$ 其中 $0 \le r < 2k$。
  2. 刪除前綴入面嘅一對相反數。 如果當前序列嘅形式係 $A = (x, -x, S_1)$,其中 $x \neq 0$ 而且 $S_1$ 可以為空,你可以將 $A$ 替換為 $S_1$;即係刪除頭兩個元素 $x$ 同 $-x$。
  3. 喺最前面插入一對新嘅相反數。 如果當前序列係 $A = (S_1)$(可以為空),你可以將佢替換為 $(x, -x, S_1)$,其中 $x \neq 0$,而且 $x$ 同 $-x$ 都冇喺 $S_1$ 入面出現過。
  4. 反轉並取反。 如果當前序列係 $$A = (a_1, a_2, \dots, a_{2k})$$ 你可以將佢替換為 $$\text{nRev}(A) = (-a_{2k}, -a_{2k-1}, \dots, -a_1)$$
  5. 跨切口重排。 揀一個切口,將當前序列分割為一個前綴同一個後綴。假設序列可以寫成 $$A = (S_1, x, S_2, S_3, y, S_4)$$ 其中 $x > 0$,$y \in \{x, -x\}$,而且切口位於 $S_2$ 同 $S_3$ 之間。呢度 $S_1$、$S_2$、$S_3$ 同 $S_4$ 可以係空序列。然後你可以應用以下變換之一:

    • 如果 $y = x$(即係你揀咗一對 $x$ 同 $x$),你可以將 $A$ 替換為 $$(S_1, \text{nRev}(S_3), x, \text{nRev}(S_4), S_2, y)$$
    • 如果 $y = -x$(即係你揀咗一對 $x$ 同 $-x$),你可以將 $A$ 替換為 $$(S_1, S_4, x, S_3, S_2, y)$$

你需要判斷係咪可以透過有限次應用呢啲操作,將序列 $A$ 轉換為一個同序列 $B$ 等價嘅序列。

設 $C = (c_1, \dots, c_{2k})$ 同 $D = (d_1, \dots, d_{2k})$ 係兩個長度相同嘅整數序列。我哋稱 $C$ 同 $D$ 等價當且僅當存在一個映射 $f : \mathbb{Z} \to \mathbb{Z}$ 滿足:

  • 對於每個整數 $x$,$f(-x) = -f(x)$,
  • 只要 $f(x) \neq 0$,整數 $x$ 同 $f(x)$ 就具有相同嘅正負號(同為正或者同為負),

而且對於所有 $1 \le i \le 2k$ 都有 $d_i = f(c_i)$。換句話講,如果我哋用 $f(c_i)$ 替換序列 $C$ 入面嘅每個元素 $c_i$,就可以得到序列 $D$。對於冇喺序列入面出現嘅整數,$f$ 嘅取值可以任意選擇。

輸入格式

第一行包含一個整數 $t$($1 \le t \le 10^5$),代表測試資料嘅組數。對於每組測試資料:

第一行包含一個整數 $n$($1 \le n \le 5 \cdot 10^5$),代表序列 $A$ 嘅長度係 $2n$。

第二行包含 $2n$ 個整數,代表 $a_1, a_2, \dots, a_{2n}$。

第三行包含一個整數 $m$($1 \le m \le 5 \cdot 10^5$),代表序列 $B$ 嘅長度係 $2m$。

第四行包含 $2m$ 個整數,代表 $b_1, b_2, \dots, b_{2m}$。

序列 $A$ 同 $B$ 滿足題目描述入面嘅要求。

所有測試資料嘅總規模滿足 $\sum n + \sum m \le 10^6$。

輸出格式

對於每組測試資料,如果可以令 $A$ 同 $B$ 等價,輸出 YES;否則,輸出 NO。

範例

4
1
1 -1
2
-1 -2 2 1
1
1 1
1
-1 1
4
1 2 -1 3 -4 2 3 -4
4
1 2 -1 -2 4 3 4 3
4
4 3 -1 -2 -3 2 1 -4
3
-1 1 3 -3 2 2
YES
NO
YES
NO
5
4
1 2 -3 -2 4 -1 3 -4
6
-3 5 2 3 -2 -5 1 -6 -4 -1 4 6
5
-2 4 -4 3 2 -1 -5 1 -3 5
5
3 -4 1 -5 4 -1 2 -2 -3 5
4
-2 1 4 2 -4 3 -1 -3
5
-1 -5 -2 2 1 -4 -3 3 4 5
3
2 -1 -1 -3 -2 3
4
-4 -2 -1 4 1 -2 3 -3
5
3 5 -5 -2 2 -1 -3 -4 1 4
5
3 5 -2 -5 -1 2 -4 1 4 -3
YES
YES
NO
YES
NO

提示

喺第一個範例入面:

喺第一組測試資料入面,$(1, -1) \xrightarrow{\text{op 3}} (-2, 2, 1, -1) \xrightarrow{\text{op 1}} (-1, -2, 2, 1)$。

喺第三組測試資料入面,假設 $x = 3$,$y = 3$,$S_1 = (1, 2, -1)$,$S_3 = (-4, 2)$,$S_4 = (-4)$,而且 $S_2$ 為空。 咁樣 $A = (S_1, x, S_2, S_3, y, S_4)$,而且 $A \xrightarrow{\text{op 5}} (1, 2, -1, -2, 4, 3, 4, 3)$。

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.