A - First ABC

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

配点 : 100

問題文

A, B, C からなる文字列 S が与えられます。SA, B, C を全て含むことが保証されます。

S を左から 1 文字ずつ見ていったときに、はじめて次の条件を満たした状態になるのは、左から何文字目まで見たときですか?

  • A, B, C が全て 1 回以上出現している。

制約

  • 3 \leq N \leq 100
  • SA, B, C からなる長さ N の文字列
  • SA, B, C を全て含む

入力

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

N
S

出力

答えを出力せよ。


入力例 1

5
ACABB

出力例 1

4

左から 4 文字目までに A2 回, B1 回, C1 回出現していて、条件を満たします。
3 文字目以前では条件を満たさないので答えは 4 です。


入力例 2

4
CABC

出力例 2

3

左から 3 文字目までに A, B, C1 回ずつ出現していて、条件を満たします。


入力例 3

30
AABABBBABABBABABCABACAABCBACCA

出力例 3

17

Score : 100 points

Problem Statement

You are given a string S consisting of A, B, and C. S is guaranteed to contain all of A, B, and C.

If the characters of S are checked one by one from the left, how many characters will have been checked when the following condition is satisfied for the first time?

  • All of A, B, and C have appeared at least once.

Constraints

  • 3 \leq N \leq 100
  • S is a string of length N consisting of A, B, and C.
  • S contains all of A, B, and C.

Input

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

N
S

Output

Print the answer.


Sample Input 1

5
ACABB

Sample Output 1

4

In the first four characters from the left, A, B, and C appear twice, once, and once, respectively, satisfying the condition.
The condition is not satisfied by checking three or fewer characters, so the answer is 4.


Sample Input 2

4
CABC

Sample Output 2

3

In the first three characters from the left, each of A, B, and C appears once, satisfying the condition.


Sample Input 3

30
AABABBBABABBABABCABACAABCBACCA

Sample Output 3

17
B - Intersection

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

配点 : 100

問題文

数直線があり、高橋君はこれを赤色と青色で次のように塗りました。

  • X=L_1 から X=R_1 までをすべて赤色で塗る。
  • X=L_2 から X=R_2 までをすべて青色で塗る。

数直線のうち、赤色と青色の両方で塗られている部分の長さを求めてください。

制約

  • 0\leq L_1<R_1\leq 100
  • 0\leq L_2<R_2\leq 100
  • 入力はすべて整数

入力

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

L_1 R_1 L_2 R_2

出力

両方の色で塗られている部分の長さを整数で出力せよ。


入力例 1

0 3 1 5

出力例 1

2

X=0 から X=3 までが赤く、 X=1 から X=5 までが青く塗られています。

よって、両方の色で塗られている部分は X=1 から X=3 までであり、その長さは 2 となります。


入力例 2

0 1 4 5

出力例 2

0

両方の色で塗られている部分はありません。


入力例 3

0 3 3 7

出力例 3

0

赤色と青色で塗られている部分が接している場合でも、 両方の色で塗られている部分の長さは 0 となります。

Score : 100 points

Problem Statement

We have a number line. Takahashi painted some parts of this line, as follows:

  • First, he painted the part from X=L_1 to X=R_1 red.
  • Next, he painted the part from X=L_2 to X=R_2 blue.

Find the length of the part of the line painted both red and blue.

Constraints

  • 0\leq L_1<R_1\leq 100
  • 0\leq L_2<R_2\leq 100
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

L_1 R_1 L_2 R_2

Output

Print the length of the part of the line painted both red and blue, as an integer.


Sample Input 1

0 3 1 5

Sample Output 1

2

The part from X=0 to X=3 is painted red, and the part from X=1 to X=5 is painted blue.

Thus, the part from X=1 to X=3 is painted both red and blue, and its length is 2.


Sample Input 2

0 1 4 5

Sample Output 2

0

No part is painted both red and blue.


Sample Input 3

0 3 3 7

Sample Output 3

0

If the part painted red and the part painted blue are adjacent to each other, the length of the part painted both red and blue is 0.

C - Strictly Superior

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

配点 : 200

問題文

AtCoder 商店には N 個の商品があります。 i\ (1\leq i\leq N) 番目の商品の価格は P _ i です。 i\ (1\leq i\leq N) 番目の商品は C _ i 個の機能をもち、i\ (1\leq i\leq N) 番目の商品の j\ (1\leq j\leq C _ i) 番目の機能は 1 以上 M 以下の整数 F _ {i,j} として表されます。

高橋くんは、AtCoder 商店の商品で一方が一方の上位互換であるものがないか気になりました。 i 番目の商品と j 番目の商品 (1\leq i,j\leq N) であって、次の条件をすべて満たすものがあるとき Yes と、ないとき No と出力してください。

  • P _ i\geq P _ j である。
  • j 番目の製品は i 番目の製品がもつ機能をすべてもつ。
  • P _ i\gt P _ j であるか、j 番目の製品は i 番目の製品にない機能を 1 つ以上もつ。

制約

  • 2\leq N\leq100
  • 1\leq M\leq100
  • 1\leq P _ i\leq10^5\ (1\leq i\leq N)
  • 1\leq C _ i\leq M\ (1\leq i\leq N)
  • 1\leq F _ {i,1}\lt F _ {i,2}\lt\cdots\lt F _ {i,C _ i}\leq M\ (1\leq i\leq N)
  • 入力はすべて整数

入力

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

N M
P _ 1 C _ 1 F _ {1,1} F _ {1,2} \ldots F _ {1,C _ 1}
P _ 2 C _ 2 F _ {2,1} F _ {2,2} \ldots F _ {2,C _ 2}
\vdots
P _ N C _ N F _ {N,1} F _ {N,2} \ldots F _ {N,C _ N}

出力

答えを 1 行で出力せよ。


入力例 1

5 6
10000 2 1 3
15000 3 1 2 4
30000 3 1 3 5
35000 2 1 5
100000 6 1 2 3 4 5 6

出力例 1

Yes

(i,j)=(4,3) とすると、条件を全て満たします。

他の組は条件を満たしません。例えば (i,j)=(4,5) とすると j 番目の商品は i 番目の商品の機能をすべてもっていますが、P _ i\lt P _ j なので上位互換ではありません。


入力例 2

4 4
3 1 1
3 1 2
3 1 2
4 2 2 3

出力例 2

No

まったく同じ価格と機能をもった商品がある場合もあります。


入力例 3

20 10
72036 3 3 4 9
7716 4 1 2 3 6
54093 5 1 6 7 8 10
25517 7 3 4 5 6 7 9 10
96930 8 2 3 4 6 7 8 9 10
47774 6 2 4 5 6 7 9
36959 5 1 3 4 5 8
46622 7 1 2 3 5 6 8 10
34315 9 1 3 4 5 6 7 8 9 10
54129 7 1 3 4 6 7 8 9
4274 5 2 4 7 9 10
16578 5 2 3 6 7 9
61809 4 1 2 4 5
1659 5 3 5 6 9 10
59183 5 1 2 3 4 9
22186 4 3 5 6 8
98282 4 1 4 7 10
72865 8 1 2 3 4 6 8 9 10
33796 6 1 3 5 7 9 10
74670 4 1 2 6 8

出力例 3

Yes

Score : 200 points

Problem Statement

AtCoder Shop has N products. The price of the i-th product (1\leq i\leq N) is P _ i. The i-th product (1\leq i\leq N) has C_i functions. The j-th function (1\leq j\leq C _ i) of the i-th product (1\leq i\leq N) is represented as an integer F _ {i,j} between 1 and M, inclusive.

Takahashi wonders whether there is a product that is strictly superior to another. If there are i and j (1\leq i,j\leq N) such that the i-th and j-th products satisfy all of the following conditions, print Yes; otherwise, print No.

  • P _ i\geq P _ j.
  • The j-th product has all functions of the i-th product.
  • P _ i\gt P _ j, or the j-th product has one or more functions that the i-th product lacks.

Constraints

  • 2\leq N\leq100
  • 1\leq M\leq100
  • 1\leq P _ i\leq10^5\ (1\leq i\leq N)
  • 1\leq C _ i\leq M\ (1\leq i\leq N)
  • 1\leq F _ {i,1}\lt F _ {i,2}\lt\cdots\lt F _ {i,C _ i}\leq M\ (1\leq i\leq N)
  • All input values are integers.

Input

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

N M
P _ 1 C _ 1 F _ {1,1} F _ {1,2} \ldots F _ {1,C _ 1}
P _ 2 C _ 2 F _ {2,1} F _ {2,2} \ldots F _ {2,C _ 2}
\vdots
P _ N C _ N F _ {N,1} F _ {N,2} \ldots F _ {N,C _ N}

Output

Print the answer in a single line.


Sample Input 1

5 6
10000 2 1 3
15000 3 1 2 4
30000 3 1 3 5
35000 2 1 5
100000 6 1 2 3 4 5 6

Sample Output 1

Yes

(i,j)=(4,3) satisfies all of the conditions.

No other pair satisfies them. For instance, for (i,j)=(4,5), the j-th product has all functions of the i-th one, but P _ i\lt P _ j, so it is not strictly superior.


Sample Input 2

4 4
3 1 1
3 1 2
3 1 2
4 2 2 3

Sample Output 2

No

Multiple products may have the same price and functions.


Sample Input 3

20 10
72036 3 3 4 9
7716 4 1 2 3 6
54093 5 1 6 7 8 10
25517 7 3 4 5 6 7 9 10
96930 8 2 3 4 6 7 8 9 10
47774 6 2 4 5 6 7 9
36959 5 1 3 4 5 8
46622 7 1 2 3 5 6 8 10
34315 9 1 3 4 5 6 7 8 9 10
54129 7 1 3 4 6 7 8 9
4274 5 2 4 7 9 10
16578 5 2 3 6 7 9
61809 4 1 2 4 5
1659 5 3 5 6 9 10
59183 5 1 2 3 4 9
22186 4 3 5 6 8
98282 4 1 4 7 10
72865 8 1 2 3 4 6 8 9 10
33796 6 1 3 5 7 9 10
74670 4 1 2 6 8

Sample Output 3

Yes
D - Buy One Carton of Milk

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

配点 : 200

問題文

スーパーマーケットで卵のパックが売られています。

6 個入りのパックは S 円、卵 8 個入りのパックは M 円、卵 12 個入りのパックは L 円です。

どのパックも何パックでも購入できるとき、N 個以上の卵を買うために必要な最小の金額を求めてください。

制約

  • 1 \leq N \leq 100
  • 1 \leq S,M,L \leq 10^4
  • 入力は全て整数である

入力

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

N S M L

出力

答えを出力せよ。


入力例 1

16 120 150 200

出力例 1

300

8 個入りのパックを 2 個買うのが最適です。


入力例 2

10 100 50 10

出力例 2

10

12 個入りのパックを 1 個買うのが最適です。


入力例 3

99 600 800 1200

出力例 3

10000

8 個入りのパックと 12 個入りのパックを 5 個ずつ買うのが最適です。

Score : 200 points

Problem Statement

A supermarket sells egg packs.

A pack of 6 eggs costs S yen, a pack of 8 eggs costs M yen, and a pack of 12 eggs costs L yen.

When you can buy any number of each pack, find the minimum amount of money required to purchase at least N eggs.

Constraints

  • 1 \leq N \leq 100
  • 1 \leq S,M,L \leq 10^4
  • All input values are integers.

Input

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

N S M L

Output

Print the answer.


Sample Input 1

16 120 150 200

Sample Output 1

300

It is optimal to buy two 8-egg packs.


Sample Input 2

10 100 50 10

Sample Output 2

10

It is optimal to buy one 12-egg pack.


Sample Input 3

99 600 800 1200

Sample Output 3

10000

It is optimal to buy five 8-egg packs and five 12-egg packs.

E - Domino

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

配点 : 300

問題文

N 個のドミノが数直線上に一列に並んでいます。i 番目のドミノは座標 i の位置に立っており、高さは A_i です。

i 番目のドミノが右に倒れると、座標 i 以上 i+A_i 未満の範囲にあるドミノが全て右に倒れます。

1 番目のドミノを右に倒したとき、全部でいくつのドミノが倒れますか?

制約

  • 1\leq N \leq 5\times 10^5
  • 1\leq A_i \leq N
  • 入力は全て整数

入力

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

N
A_1 A_2 \ldots A_N

出力

1 番目のドミノを右に倒したときに倒れるドミノの個数を出力せよ。


入力例 1

4
3 1 4 1

出力例 1

4

1 番目のドミノが右に倒れると、2,3 番目のドミノも右に倒れます。3 番目のドミノが右に倒れると、4 番目のドミノも倒れます。


入力例 2

9
1 4 1 4 2 1 3 5 6

出力例 2

1

1 番目のドミノを右に倒しても、他のドミノが倒れることはありません。


入力例 3

10
5 4 3 2 1 1 2 3 4 5

出力例 3

5

Score : 300 points

Problem Statement

There are N dominoes standing in a row on a number line. The i-th domino is standing at coordinate i and has height A_i.

When the i-th domino falls to the right, all dominoes in the range between coordinates i and i+A_i-1, inclusive, fall to the right.

How many dominoes will fall in total when the first domino falls to the right?

Constraints

  • 1\leq N \leq 5\times 10^5
  • 1\leq A_i \leq N
  • 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

Output

Output the number of dominoes that fall when the first domino falls to the right.


Sample Input 1

4
3 1 4 1

Sample Output 1

4

When the first domino falls to the right, the second and third dominoes also fall to the right. When the third domino falls to the right, the fourth domino also falls.


Sample Input 2

9
1 4 1 4 2 1 3 5 6

Sample Output 2

1

When the first domino falls to the right, no other dominoes will fall.


Sample Input 3

10
5 4 3 2 1 1 2 3 4 5

Sample Output 3

5
F - A+B+C

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

配点 : 250

問題文

3 個の数列 A=(A_1,\ldots,A_N), B=(B_1,\ldots,B_M), C=(C_1,\ldots,C_L) が与えられます。

さらに数列 X=(X_1,\ldots,X_Q) が与えられるので、各 i=1,\ldots,Q に対して次の問題を解いてください。

問題:A,B,C からそれぞれ 1 個ずつ要素を選び、和を X_i にすることができるか?

制約

  • 1 \leq N,M,L \leq 100
  • 0 \leq A_i, B_i ,C_i \leq 10^8
  • 1 \leq Q \leq 2\times 10^5
  • 0 \leq X_i \leq 3\times 10^8
  • 入力は全て整数である

入力

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

N
A_1 \ldots A_N
M
B_1 \ldots B_M
L
C_1 \ldots C_L
Q
X_1 \ldots X_Q

出力

Q 行出力せよ。
i 行目には、A,B,C からそれぞれ 1 個ずつ要素を選び和を X_i にすることができるならば Yes、できないならば No と出力せよ。


入力例 1

3
1 2 3
2
2 4
6
1 2 4 8 16 32
4
1 5 10 50

出力例 1

No
Yes
Yes
No
  • A,B,C からそれぞれ 1 個ずつ要素を選び和を 1 にすることはできません。
  • A,B,C からそれぞれ 1,2,2 を選ぶと和を 5 にすることができます。
  • A,B,C からそれぞれ 2,4,4 を選ぶと和を 10 にすることができます。
  • A,B,C からそれぞれ 1 個ずつ要素を選び和を 50 にすることはできません。

Score: 250 points

Problem Statement

You are given three sequences A=(A_1,\ldots,A_N), B=(B_1,\ldots,B_M), and C=(C_1,\ldots,C_L).

Additionally, a sequence X=(X_1,\ldots,X_Q) is given. For each i=1,\ldots,Q, solve the following problem:

Problem: Is it possible to select one element from each of A, B, and C so that their sum is X_i?

Constraints

  • 1 \leq N,M,L \leq 100
  • 0 \leq A_i, B_i ,C_i \leq 10^8
  • 1 \leq Q \leq 2\times 10^5
  • 0 \leq X_i \leq 3\times 10^8
  • All input values are integers.

Input

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

N
A_1 \ldots A_N
M
B_1 \ldots B_M
L 
C_1 \ldots C_L
Q
X_1 \ldots X_Q

Output

Print Q lines. The i-th line should contain Yes if it is possible to select one element from each of A, B, and C so that their sum is X_i, and No otherwise.


Sample Input 1

3
1 2 3
2
2 4
6
1 2 4 8 16 32
4
1 5 10 50

Sample Output 1

No
Yes
Yes
No
  • It is impossible to select one element from each of A, B, and C so that their sum is 1.
  • Selecting 1, 2, and 2 from A, B, and C, respectively, makes the sum 5.
  • Selecting 2, 4, and 4 from A, B, and C, respectively, makes the sum 10.
  • It is impossible to select one element from each of A, B, and C so that their sum is 50.
G - Hidden Weights

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

配点 : 400

問題文

N 頂点 M 辺の有向グラフが与えられます。j 番目の有向辺は頂点 u_j から頂点 v_j に向かっており、重み w_j を持っています。

各頂点に -10^{18} 以上 10^{18} 以下の整数を書き込む方法であって、次の条件を満たすものを 1 つ見つけてください。

  • 頂点 i に書き込まれている値を x_i とする。すべての辺 j=1,2,\dots,M について、x_{v_j} - x_{u_j} = w_j が成り立つ。

与えられる入力について、条件を満たす書き込み方が少なくとも 1 つ存在することが保証されます。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq \min\{2 \times 10^5,N(N-1)/2\}
  • 1 \leq u_j, v_j \leq N
  • u_j \neq v_j
  • i \neq j なら (u_i,v_i) \neq (u_j,v_j) かつ (u_i,v_i) \neq (v_j,u_j)
  • |w_j| \leq 10^9
  • 入力はすべて整数
  • 条件を満たす書き込み方が少なくとも 1 つ存在する

入力

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

N M
u_1 v_1 w_1
u_2 v_2 w_2
\vdots
u_M v_M w_M

出力

頂点 i に書き込む整数を x_i として、x_1,x_2,\dots,x_N をこの順に空白区切りで 1 行で出力せよ。答えが複数ある場合、どれを出力しても良い。


入力例 1

3 3
1 2 2
3 2 3
1 3 -1

出力例 1

3 5 2

x=(3,5,2) とすることで、x_2-x_1=w_1=2,x_2-x_3=w_2=3,x_3-x_1=w_3=-1 となり、条件を満たします。

他にも、たとえば x=(-1,1,-2) としても正解となります。


入力例 2

4 2
2 1 5
3 4 -3

出力例 2

5 0 6 3

他にも、たとえば x=(0,-5,4,1)x=(5,0,4,1) としても正解となります。


入力例 3

5 7
2 1 18169343
3 1 307110901
4 1 130955934
2 3 -288941558
2 5 96267410
5 3 -385208968
4 3 -176154967

出力例 3

200401298 182231955 -106709603 69445364 278499365

Score : 400 points

Problem Statement

You are given a directed graph with N vertices and M edges. The j-th directed edge goes from vertex u_j to vertex v_j and has a weight of w_j.

Find one way to write an integer between -10^{18} and 10^{18}, inclusive, to each vertex such that the following condition is satisfied.

  • Let x_i be the value written on vertex i. For all edges j=1,2,\dots,M, it holds that x_{v_j} - x_{u_j} = w_j.

It is guaranteed that at least one such assignment exists for the given input.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq \min\{2 \times 10^5,N(N-1)/2\}
  • 1 \leq u_j, v_j \leq N
  • u_j \neq v_j
  • If i \neq j, then (u_i, v_i) \neq (u_j, v_j) and (u_i, v_i) \neq (v_j, u_j)
  • |w_j| \leq 10^9
  • All input values are integers.
  • There exists at least one assignment satisfying the conditions.

Input

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

N M
u_1 v_1 w_1
u_2 v_2 w_2
\vdots
u_M v_M w_M

Output

Let x_i be the integer written on vertex i. Print x_1, x_2, \dots, x_N in this order, separated by spaces, on a single line. If there are multiple solutions, you may print any of them.


Sample Input 1

3 3
1 2 2
3 2 3
1 3 -1

Sample Output 1

3 5 2

By setting x = (3, 5, 2), we have x_2 - x_1 = w_1 = 2, x_2 - x_3 = w_2 = 3, x_3 - x_1 = w_3 = -1, satisfying the conditions.

For example, x = (-1, 1, -2) is also a valid answer.


Sample Input 2

4 2
2 1 5
3 4 -3

Sample Output 2

5 0 6 3

For example, x = (0, -5, 4, 1) and x = (5, 0, 4, 1) are also valid answers.


Sample Input 3

5 7
2 1 18169343
3 1 307110901
4 1 130955934
2 3 -288941558
2 5 96267410
5 3 -385208968
4 3 -176154967

Sample Output 3

200401298 182231955 -106709603 69445364 278499365
H - Reverse 2^i

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

配点 : 450

問題文

(1,2,3,\ldots,2^{N}) の順列 P=(P_0,P_1,\ldots,P_{2^{N}-1}) が与えられます。

あなたは次の操作を何回でも(0 回でもよい)行うことができます。

  • 0 \leq a \times 2^{b} < (a+1) \times 2^{b} \leq 2^{N} を満たす非負整数 a,b を選び、P_{a \times 2^{b}}, P_{a\times 2^{b}+1},\ldots,P_{(a+1) \times 2^{b}-1} を反転する。ここで、P_{a \times 2^{b}},P_{a \times 2^{b}+1},\ldots,P_{(a+1) \times 2^{b}-1} を反転するとは、P_{a \times 2^{b}}, P_{a\times 2^{b}+1},\ldots,P_{(a+1) \times 2^{b}-1}P_{(a+1) \times 2^{b}-1}, P_{(a+1) \times 2^{b}-2},\ldots,P_{a \times 2^{b}} に同時に置き換えることを意味する。

操作を繰り返して得られる P のうち、辞書順最小のものを求めてください。

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

制約

  • 1 \leq T \leq 10^{5}
  • 1 \leq N \leq 18
  • P(1,2,3,\ldots,2^{N}) の順列
  • 各入力ファイルについて、すべてのテストケースの 2^N の総和は 3 \times 10^{5} 以下
  • 入力はすべて整数

入力

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

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

\textrm{case}_ii 番目のテストケースを表し、以下の形式で与えられる。

N
P_0 P_1 \ldots P_{2^N-1}

出力

T 行出力せよ。i 行目 (1 \leq i \leq T) には i 番目のテストケースに対する答えを出力せよ。


入力例 1

4
1
1 2
2
1 3 4 2
2
2 3 4 1
3
8 3 4 2 1 5 7 6

出力例 1

1 2
1 3 2 4
1 4 2 3
1 5 6 7 2 4 3 8

1 番目のテストケースにおいて、P に対して操作を一回も行わないとき、P=(1,2) となります。これは辞書順で最小の順列です。よって答えは(1,2) です。

2 番目のテストケースにおいて、a=1,b=1 を選んで操作を行うと P=(1,3,2,4) となります。P に対して何回操作を行っても (1,3,2,4) より辞書順で小さい順列を得られません。よって答えは (1,3,2,4) です。

3 番目のテストケースにおいて、以下の順番で操作を行うことで、P=(1,4,2,3) が得られます。

  • a=0,b=1 を選んで操作を行う。P=(3,2,4,1) となる。
  • a=0,b=2 を選んで操作を行う。P=(1,4,2,3) となる。

P に対して何回操作を行っても (1,4,2,3) より辞書順で小さい順列を得られません。よって答えは (1,4,2,3) です。

Score : 450 points

Problem Statement

You are given a permutation P=(P_0,P_1,\ldots,P_{2^{N}-1}) of (1,2,3,\ldots,2^{N}).

You can perform the following operation any number of times (possibly zero):

  • Choose non-negative integers a,b satisfying 0 \leq a \times 2^{b} < (a+1) \times 2^{b} \leq 2^{N}, and reverse P_{a \times 2^{b}}, P_{a\times 2^{b}+1},\ldots,P_{(a+1) \times 2^{b}-1}. Here, reversing P_{a \times 2^{b}}, P_{a\times 2^{b}+1},\ldots,P_{(a+1) \times 2^{b}-1} means simultaneously replacing P_{a \times 2^{b}}, P_{a\times 2^{b}+1},\ldots,P_{(a+1) \times 2^{b}-1} with P_{(a+1) \times 2^{b}-1}, P_{(a+1) \times 2^{b}-2},\ldots,P_{a \times 2^{b}}.

Find the lexicographically smallest permutation P that can be obtained by repeating the operation.

You are given T test cases, so find the answer for each.

Constraints

  • 1 \leq T \leq 10^{5}
  • 1 \leq N \leq 18
  • P is a permutation of (1,2,3,\ldots,2^{N}).
  • For each input file, the sum of 2^N over all test cases is at most 3 \times 10^{5}.
  • All input values are integers.

Input

The input is given from standard input in the following format:

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

\textrm{case}_i represents the i-th test case and is given in the following format:

N
P_0 P_1 \ldots P_{2^N-1}

Output

Output T lines. The i-th line (1 \leq i \leq T) should contain the answer to the i-th test case.


Sample Input 1

4
1
1 2
2
1 3 4 2
2
2 3 4 1
3
8 3 4 2 1 5 7 6

Sample Output 1

1 2
1 3 2 4
1 4 2 3
1 5 6 7 2 4 3 8

In the first test case, when no operation is performed on P, P=(1,2). This is the lexicographically smallest permutation. Thus, the answer is (1,2).

In the second test case, when we perform the operation with a=1,b=1, P becomes (1,3,2,4). No matter how many operations we perform on P, we cannot obtain a permutation lexicographically smaller than (1,3,2,4). Thus, the answer is (1,3,2,4).

In the third test case, by performing operations in the following order, we can obtain P=(1,4,2,3):

  • Perform the operation with a=0,b=1. P becomes (3,2,4,1).
  • Perform the operation with a=0,b=2. P becomes (1,4,2,3).

No matter how many operations we perform on P, we cannot obtain a permutation lexicographically smaller than (1,4,2,3). Thus, the answer is (1,4,2,3).

I - Cumulative Cumulative Cumulative Sum

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

配点 : 500

問題文

N,Q および A=(A_1,\ldots,A_N) が与えられます。
以下のクエリを Q 個処理してください。クエリは次の 2 種類のいずれかです。

  • 1 x v : A_xv に更新する。
  • 2 x : B_i=\sum_{j=1}^{i}A_jC_i=\sum_{j=1}^{i}B_jD_i=\sum_{j=1}^{i}C_j としたときの D_x\bmod\ 998244353 で出力する。

制約

  • 1 \leq N \leq 2\times10^5
  • 1 \leq Q \leq 2\times10^5
  • 0 \leq A_i \leq 10^9
  • 1 \leq x \leq N
  • 0 \leq v \leq 10^9
  • 入力に含まれる値は全て整数である

入力

入力は以下の形式で標準入力から与えられる。ここで {\rm query}_ii 番目に処理するクエリである。

N Q
A_1 A_2 \ldots A_N
{\rm query}_1
{\rm query}_2
\vdots
{\rm query}_Q

各クエリは以下の 2 種類のいずれかの形式で与えられる。

1 x v
2 x

出力

クエリへの答えを改行区切りで出力せよ。


入力例 1

3 3
1 2 3
2 3
1 2 0
2 3

出力例 1

15
9

1 番目のクエリの時点で A=(1,2,3) であるため、B=(1,3,6)C=(1,4,10)D=(1,5,15) となり、D_3=15 です。

3 番目のクエリの時点で A=(1,0,3) であるため、B=(1,1,4)C=(1,2,6)D=(1,3,9) となり、D_3=9 です。


入力例 2

2 1
998244353 998244353
2 1

出力例 2

0

Score : 500 points

Problem Statement

You are given N, Q, and A=(A_1,\ldots,A_N).
Process Q queries, each of which is of one of the following two kinds:

  • 1 x v: update A_x to v.
  • 2 x: let B_i=\sum_{j=1}^{i}A_j, C_i=\sum_{j=1}^{i}B_j, and D_i=\sum_{j=1}^{i}C_j. Print D_x modulo 998244353.

Constraints

  • 1 \leq N \leq 2\times10^5
  • 1 \leq Q \leq 2\times10^5
  • 0 \leq A_i \leq 10^9
  • 1 \leq x \leq N
  • 0 \leq v \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format, where {\rm query}_i denotes the i-th query to be processed:

N Q
A_1 A_2 \ldots A_N
{\rm query}_1
{\rm query}_2
\vdots
{\rm query}_Q

Each query is given in one of the following two formats:

1 x v
2 x

Output

Print the answer to the queries, with newlines in between.


Sample Input 1

3 3
1 2 3
2 3
1 2 0
2 3

Sample Output 1

15
9

When the 1-st query is given, A=(1,2,3), so B=(1,3,6), C=(1,4,10), and D=(1,5,15); thus, D_3=15.

When the 3-rd query is given, A=(1,0,3), so B=(1,1,4), C=(1,2,6), and D=(1,3,9); thus, D_3=9.


Sample Input 2

2 1
998244353 998244353
2 1

Sample Output 2

0