A - Twice Subsequence

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 400

問題文

数列 A = (A_1,\dots,A_N) があります。A の部分列であって、数列 B = (B_1,\dots,B_M) と一致するものが 2 つ以上あるかどうかを判定してください。ただし、2 つの部分列が数列として一致していても、取り出す位置が異なれば区別するものとします。

部分列とは A の部分列とは、A の要素を 0 個以上選んで削除し、残った要素を元の順序を保って並べた数列のことを指します。

制約

  • 1 \leq M \leq N \leq 2\times 10^5
  • 1 \leq A_i \leq 10^9
  • 1 \leq B_i \leq 10^9
  • 入力される値は全て整数

入力

入力は以下の形式で標準入力から与えられる。

N M
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_M

出力

A の部分列であって、数列 B と一致するものが 2 つ以上ある場合は Yes、そうでない場合は No を出力せよ。


入力例 1

4 2
1 2 1 2
1 2

出力例 1

Yes

A の部分列であって B と一致するものは、(A_1,A_2),(A_1,A_4),(A_3,A_4)3 つです。


入力例 2

3 2
1 2 1
1 2

出力例 2

No

A の部分列であって B と一致するものは、(A_1,A_2) のみです。


入力例 3

3 2
1 1 2
2 1

出力例 3

No

A の部分列であって B と一致するものはありません。

Score : 400 points

Problem Statement

There is a sequence A = (A_1,\dots,A_N). Determine whether there are at least two subsequences of A that match the sequence B = (B_1,\dots,B_M). Two subsequences are distinguished if they are taken from different positions, even if they coincide as sequences.

Subsequence A subsequence of A is a sequence obtained by removing zero or more elements from A and leaving the remaining elements in their original order.

Constraints

  • 1 \leq M \leq N \leq 2\times 10^5
  • 1 \leq A_i \leq 10^9
  • 1 \leq B_i \leq 10^9
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N M
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_M

Output

If there are at least two subsequences of A that match B, print Yes. Otherwise, print No.


Sample Input 1

4 2
1 2 1 2
1 2

Sample Output 1

Yes

There are three subsequences of A that match B: (A_1,A_2), (A_1,A_4), (A_3,A_4).


Sample Input 2

3 2
1 2 1
1 2

Sample Output 2

No

There is only one subsequence of A that matches B: (A_1,A_2).


Sample Input 3

3 2
1 1 2
2 1

Sample Output 3

No

There are no subsequences of A that match B.

B - Uniform Sum

実行時間制限: 5 sec / メモリ制限: 1024 MiB

配点 : 500

問題文

数列 A=(A_1,\dots,A_N), B=(B_1,\dots,B_N) があります。これらに対して、以下の 3 種類の操作を、任意の順番で任意の回数だけ行うことができます。

  • A_i = -1 を満たす i を選び、A_i を任意の非負整数に置き換える。
  • B_i = -1 を満たす i を選び、B_i を任意の非負整数に置き換える。
  • 数列 A の要素を任意の順番に並び替える。

操作の結果、A, B の全ての要素が非負であり、なおかつ A_1 + B_1 = A_2 + B_2 = \dots = A_N + B_N となるようにできるかを判定してください。

制約

  • 2 \leq N \leq 2000
  • -1 \leq A_i \leq 10^9
  • -1 \leq B_i \leq 10^9
  • 入力される値は全て整数

入力

入力は以下の形式で標準入力から与えられる。

N
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_N

出力

操作の結果、A, B の全ての要素が非負かつ A_1 + B_1 = A_2 + B_2 = \dots = A_N + B_N となるようにできる場合は Yes、そうでない場合は No を出力せよ。


入力例 1

4
2 0 -1 3
3 -1 4 2

出力例 1

Yes

以下の操作を行うことを考えます。

  • A_31 に置き換える。
  • B_21 に置き換える。
  • A を並び替えて (1,3,0,2) にする。

操作の結果、A = (1,3,0,2), B = (3,1,4,2) となり、A, B の全ての要素が非負かつ A_1+B_1 = A_2+B_2 = A_3+B_3 = A_4+B_4 = 4 が満たされます。


入力例 2

3
1 2 3
1 2 4

出力例 2

No

どのように操作を行っても、A_1+B_1 = A_2+B_2 = A_3+B_3 を満たすようにはできません。


入力例 3

3
1 2 -1
1 2 4

出力例 3

No

Score : 500 points

Problem Statement

There are two sequences A=(A_1,\dots,A_N) and B=(B_1,\dots,B_N). You can perform the following three types of operations any number of times in any order:

  • Choose an index i such that A_i = -1, and replace A_i with any non-negative integer.
  • Choose an index i such that B_i = -1, and replace B_i with any non-negative integer.
  • Rearrange the elements of sequence A in any order.

Determine whether it is possible, after these operations, for all elements of A and B to be non-negative and satisfy A_1 + B_1 = A_2 + B_2 = \dots = A_N + B_N.

Constraints

  • 2 \leq N \leq 2000
  • -1 \leq A_i \leq 10^9
  • -1 \leq B_i \leq 10^9
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_N

Output

If it is possible, after the operations, for all elements of A and B to be non-negative and satisfy A_1 + B_1 = A_2 + B_2 = \dots = A_N + B_N, print Yes. Otherwise, print No.


Sample Input 1

4
2 0 -1 3
3 -1 4 2

Sample Output 1

Yes

Consider the following operations:

  • Replace A_3 with 1.
  • Replace B_2 with 1.
  • Rearrange A to (1,3,0,2).

After these operations, A = (1,3,0,2) and B = (3,1,4,2): all elements of A and B are non-negative, and A_1+B_1 = A_2+B_2 = A_3+B_3 = A_4+B_4 = 4 is satisfied.


Sample Input 2

3
1 2 3
1 2 4

Sample Output 2

No

No matter how you perform the operations, it is impossible to satisfy A_1+B_1 = A_2+B_2 = A_3+B_3.


Sample Input 3

3
1 2 -1
1 2 4

Sample Output 3

No
C - Hamiltonian Pieces

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 600

問題文

10^9 マス、横 10^9 マスの盤と、R 個の赤い駒、B 個の青い駒があります。ただし、2\leq R+B が成り立ちます。盤の上から r 行目、左から c 列目のマスを、マス (r,c) と呼びます。赤い駒は 1 手で縦横に 1 マス、青い駒は 1 手で斜めに 1 マス移動することができます。より正確には、マス (r,c) にある赤い駒はマス (r+1,c),(r,c+1),(r-1,c),(r,c-1) に、マス (r,c) にある青い駒はマス (r+1,c+1),(r+1,c-1),(r-1,c+1),(r-1,c-1) に、移動先のマスが存在すればそれぞれ 1 手で移動することができます。

この盤に、(R+B) 個全ての駒を任意の順番で 1 つずつ置きます。ただし、以下の条件を満たすように置かなければいけません。

  • 1 つのマスに置かれる駒は高々 1 個である。
  • i (1 \leq i \leq R+B-1) について、i 番目に置かれる駒は (i+1) 番目に置かれる駒のあるマスに 1 手で移動することができる。
  • (R+B) 番目に置かれる駒は 1 番目に置かれる駒のあるマスに 1 手で移動することができる。

条件を満たす (R+B) 個の駒の置き方が存在するかを判定し、存在するならばその一例を示してください。

T 個のテストケースが与えられるので、それぞれについて解いてください。

制約

  • 1\leq T\leq 10^5
  • 0 \leq R, B
  • 2 \leq R + B \leq 2 \times 10^5
  • すべてのテストケースにわたる R+B の総和は 2\times 10^5 以下
  • 入力される値は全て整数

入力

入力は以下の形式で標準入力から与えられる。

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

各ケースは以下の形式で与えられる。

R B

出力

各テストケースに対する答えを順に改行区切りで出力せよ。

あるテストケースについて、条件を満たす駒の置き方が存在しない場合は No と出力せよ。

そうでない場合、条件を満たす駒の置き方を以下の形式で出力せよ。

Yes
p_1 r_1 c_1
\vdots
p_{R+B} r_{R+B} c_{R+B}

ここで、p_ii 番目に置く駒が赤い駒ならば R、青い駒ならば B である。また、r_i,c_i1 以上 10^9 以下の整数であり、i 番目に置く駒がマス (r_i,c_i) に置かれることを意味する。


入力例 1

3
2 3
1 1
4 0

出力例 1

Yes
B 2 3
R 3 2 
B 2 2
B 3 3
R 2 4
No
Yes
R 1 1
R 1 2
R 2 2
R 2 1

1 番目のテストケースについて、盤の左上 4\times 5 マスを抽出すると、駒の配置は以下のようになります。

.....
.BBR.
.RB..
.....

ここで、R はそのマスに赤い駒が置かれること、B は青い駒が置かれること、. は駒が何も置かれないことをそれぞれ意味します。

2 番目のテストケースについて、条件を満たすような駒の置き方は存在しません。

Score : 600 points

Problem Statement

There is a board with 10^9 rows and 10^9 columns, and R red pieces and B blue pieces. Here, R+B is not less than 2. The square at the r-th row from the top and the c-th column from the left is called square (r,c). A red piece can move vertically or horizontally by one square in one move, and a blue piece can move diagonally by one square in one move. More precisely, a red piece on square (r,c) can move to (r+1,c), (r,c+1), (r-1,c), (r,c-1) in one move if the destination square exists, and a blue piece on square (r,c) can move to (r+1,c+1), (r+1,c-1), (r-1,c+1), (r-1,c-1) in one move if the destination square exists.

We want to place all (R+B) pieces on the board in any order, one by one, subject to the following conditions:

  • At most one piece is placed on a single square.
  • For each i (1 \leq i \leq R+B-1), the i-th piece placed can move in one move to the square containing the (i+1)-th piece placed.
  • The (R+B)-th piece placed can move in one move to the square containing the 1-st piece placed.

Determine whether there is a way to place the (R+B) pieces satisfying these conditions. If it exists, show one example.

You are given T test cases; solve each of them.

Constraints

  • 1\leq T\leq 10^5
  • 0 \leq R, B
  • 2 \leq R + B \leq 2 \times 10^5
  • The sum of (R+B) over all test cases is at most 2\times 10^5.
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

Each case is given in the following format:

R B

Output

Print the answer for each test case in order, separated by newlines.

If there is no way to place the pieces satisfying the conditions for a test case, print No.

Otherwise, print such a placement in the following format:

Yes
p_1 r_1 c_1
\vdots
p_{R+B} r_{R+B} c_{R+B}

Here, p_i is R if the i-th piece placed is red, and B if it is blue. r_i and c_i are integers between 1 and 10^9 (inclusive), indicating that the i-th piece is placed on square (r_i,c_i).


Sample Input 1

3
2 3
1 1
4 0

Sample Output 1

Yes
B 2 3
R 3 2 
B 2 2
B 3 3
R 2 4
No
Yes
R 1 1
R 1 2
R 2 2
R 2 1

For the 1st test case, if we extract the top-left 4\times 5 squares of the board, the placement of the pieces is as follows:

.....
.BBR.
.RB..
.....

Here, R indicates a red piece on that square, B indicates a blue piece on that square, and . indicates an empty square.

For the 2nd test case, there is no placement of the pieces that satisfies the conditions.

D - Swap and Erase

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 700

問題文

数列 A = (A_1,\ldots,A_N) があります。これに対して、以下の 2 種類の操作を、任意の順番で任意の回数だけ行うことができます。

  • 操作直前の A の長さを K とする。1\leq i\leq K-1 を満たす整数 i を選び、A の第 i 項と第 (i+1) 項を入れ替える。
  • 操作直前の A の長さを K とする。1\leq i\leq K かつ A の第 1 項から第 i 項の値が全て等しいような整数 i を選び、A の第 1 項から第 i 項までを全て削除する。

A を空の数列にするために必要な合計操作回数の最小値を求めてください。

T 個のテストケースが与えられるので、それぞれについて解いてください。

制約

  • 1\leq T\leq 10^5
  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq N
  • すべてのテストケースにわたる N の総和は 2\times 10^5 以下
  • 入力される値は全て整数

入力

入力は以下の形式で標準入力から与えられる。

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

各ケースは以下の形式で与えられる。

N
A_1 A_2 \ldots A_N

出力

各テストケースに対する答えを順に改行区切りで出力せよ。


入力例 1

3
5
1 1 2 1 2
4
4 2 1 3
11
1 2 1 2 1 2 1 2 1 2 1

出力例 1

3
4
8

1 番目のテストケースについて、以下の 3 回の操作によって A を空の数列にすることができます。

  • A の第 3 項と第 4 項を入れ替える。結果として、A(1,1,1,2,2) となる。
  • A の第 1 項から第 3 項までを削除する。結果として、A(2,2) となる。
  • A の第 1 項から第 2 項までを削除する。結果として、A は空の数列となる。

2 番目のテストケースについて、A の第 1 項を削除する操作を 4 回行うことで A を空の数列にすることができます。また、3 回以下の操作では A を空の数列にすることはできません。

Score : 700 points

Problem Statement

There is a sequence A = (A_1,\ldots,A_N). You can perform the following two types of operations any number of times in any order:

  • Let K be the length of A just before the operation. Choose an integer i such that 1 \leq i \leq K-1, and swap the i-th and (i+1)-th elements of A.
  • Let K be the length of A just before the operation. Choose an integer i such that 1 \leq i \leq K and all the values from the 1-st through the i-th elements of A are equal, and delete all the elements from the 1-st through the i-th of A.

Find the minimum total number of operations required to make A an empty sequence.

You are given T test cases; solve each of them.

Constraints

  • 1\leq T\leq 10^5
  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq N
  • The sum of N over all test cases is at most 2\times 10^5.
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

Each case is given in the following format:

N
A_1 A_2 \ldots A_N

Output

Print the answer for each test case in order, separated by newlines.


Sample Input 1

3
5
1 1 2 1 2
4
4 2 1 3
11
1 2 1 2 1 2 1 2 1 2 1

Sample Output 1

3
4
8

For the 1st test case, A can be made empty by the following three operations:

  • Swap the 3rd and 4th elements of A. Now, A is (1,1,1,2,2).
  • Delete the 1st through 3rd elements of A. Now, A is (2,2).
  • Delete the 1st through 2nd elements of A. Now, A is an empty sequence.

For the 2nd test case, A can be made empty by deleting the 1st element four times. Also, it is impossible to make A empty in three or fewer operations.

E - Random Tree Distance

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 900

問題文

整数列 A = (A_2,A_3,\ldots,A_N) があります。また、各 i (2 \leq i \leq N) について 1 \leq P_i \leq i-1 を満たす整数列 P=(P_2, P_3, \ldots ,P_N) に対して、頂点 1 を根とする N 頂点の重み付き木 T(P) を以下のように定義します。

  • i (2 \leq i \leq N) について、i の親が P_i であり、iP_i を結ぶ辺の重みが A_i であるような根付き木

Q 個のクエリが与えられるので、順に処理してください。i 番目のクエリは以下の通りです。

  • 1 以上 N 以下の整数 u_i,v_i が与えられる。P としてあり得る (N-1)! 通りの数列全てについての木 T(P) での頂点 u_i と頂点 v_i の距離の総和を 998244353 で割った余りを出力する。ただし、頂点 u_i と頂点 v_i の距離とは、この 2 頂点を結ぶ唯一の(同じ頂点を 2 回以上通らない)パスに含まれる辺の重みの総和である。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^9
  • 1 \leq u_i \lt v_i \leq N
  • 入力される値は全て整数

入力

入力は以下の形式で標準入力から与えられる。

N Q
A_2 A_3 \ldots A_N
u_1 v_1
u_2 v_2
\vdots
u_Q v_Q

出力

Q 行出力せよ。i 行目には、i 番目のクエリの答えを出力せよ。


入力例 1

3 2
1 1
1 2
1 3

出力例 1

2
3
  • P = (1,1) の場合、木 T(P) での頂点 1 と頂点 2 の距離は 1、頂点 1 と頂点 3 の距離は 1 です。
  • P = (1,2) の場合、木 T(P) での頂点 1 と頂点 2 の距離は 1、頂点 1 と頂点 3 の距離は 2 です。

以上より、全ての P に対する木 T(P) での頂点 1 と頂点 2 の距離の総和は 2、頂点 1 と頂点 3 の距離の総和は 3 です。


入力例 2

2 1
100
1 2

出力例 2

100

入力例 3

9 6
765689282 93267307 563699854 951829154 801512848 389123318 924504746 596035433
3 8
2 5
5 8
2 9
8 9
5 7

出力例 3

55973424
496202632
903509579
343265517
550981449
68482696

総和を 998244353 で割った余りを求めることに注意してください。

Score : 900 points

Problem Statement

There is an integer sequence A = (A_2,A_3,\ldots,A_N). Also, for an integer sequence P=(P_2, P_3, \ldots ,P_N) where 1 \leq P_i \leq i-1 for each i (2 \leq i \leq N), define the weighted tree T(P) with N vertices, rooted at vertex 1, as follows:

  • A rooted tree where, for each i (2 \leq i \leq N), the parent of i is P_i, and the weight of the edge between i and P_i is A_i.

You are given Q queries. Process them in order. The i-th query is as follows:

  • You are given integers u_i and v_i, each between 1 and N. For each of the possible (N-1)! sequences P, take the tree T(P) and consider the distance between vertices u_i and v_i in this tree. Output the sum, modulo 998244353, of these distances over all T(P). Here, the distance between two vertices u_i and v_i is the sum of the weights of the edges on the unique path (not visiting the same vertex more than once) that connects them.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^9
  • 1 \leq u_i < v_i \leq N
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N Q
A_2 A_3 \ldots A_N
u_1 v_1
u_2 v_2
\vdots
u_Q v_Q

Output

Print Q lines. The i-th line should contain the answer to the i-th query.


Sample Input 1

3 2
1 1
1 2
1 3

Sample Output 1

2
3
  • If P = (1,1), then in the tree T(P), the distance between vertices 1 and 2 is 1, and the distance between vertices 1 and 3 is 1.
  • If P = (1,2), then in the tree T(P), the distance between vertices 1 and 2 is 1, and the distance between vertices 1 and 3 is 2.

Therefore, the total distance between vertices 1 and 2 over all T(P) is 2, and the total distance between vertices 1 and 3 over all T(P) is 3.


Sample Input 2

2 1
100
1 2

Sample Output 2

100

Sample Input 3

9 6
765689282 93267307 563699854 951829154 801512848 389123318 924504746 596035433
3 8
2 5
5 8
2 9
8 9
5 7

Sample Output 3

55973424
496202632
903509579
343265517
550981449
68482696

Remember to take the sum modulo 998244353.