A - Mod While Positive

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

正整数 N, M が与えられます。

M の値が 0 でない間以下の操作を繰り返すとき、操作を行う回数を求めてください。

  • NM で割った余りを x とする。M の値を x で置き換える。

なお、この操作を有限回行うことにより M = 0 になることが証明できます。

制約

  • 1 \leq N, M \leq 1000
  • 入力される値はすべて整数

入力

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

N M

出力

答えを出力せよ。


入力例 1

8 5

出力例 1

3

はじめ、N = 8, M = 5 です。

85 で割った余りは 3 であるため、1 回操作を行うと M = 3 となります。

83 で割った余りは 2 であるため、2 回操作を行うと M = 2 となります。

82 で割った余りは 0 であるため、3 回操作を行うと M = 0 となります。

したがって、3 を答えとして出力します。


入力例 2

14 6

出力例 2

2

入力例 3

460 33

出力例 3

5

Score : 100 points

Problem Statement

You are given positive integers N and M.

If the following operation is repeated while the value of M is not 0, find the number of operations performed.

  • Let x be the remainder when N is divided by M. Replace the value of M with x.

It can be proved that M becomes 0 after a finite number of operations.

Constraints

  • 1 \leq N, M \leq 1000
  • All input values are integers.

Input

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

N M

Output

Output the answer.


Sample Input 1

8 5

Sample Output 1

3

Initially, N = 8 and M = 5.

The remainder when 8 is divided by 5 is 3, so M = 3 after one operation.

The remainder when 8 is divided by 3 is 2, so M = 2 after two operations.

The remainder when 8 is divided by 2 is 0, so M = 0 after three operations.

Thus, output 3 as the answer.


Sample Input 2

14 6

Sample Output 2

2

Sample Input 3

460 33

Sample Output 3

5
B - Two Rings

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 250

問題文

xy 平面上に 2 つの円 C_1, C_2 があります。ただし、本問題において円とは円周のことを指します。
C_1 は中心の座標が (X_1, Y_1) で半径が R_1 です。
C_2 は中心の座標が (X_2, Y_2) で半径が R_2 です。
C_1 と円 C_2 が共有点を持つかどうかを判定してください。言い換えると、点 (X_1, Y_1) からの距離が R_1 であり、かつ点 (X_2, Y_2) からの距離が R_2 であるような点が 1 個以上存在するかどうかを判定してください。

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

制約

  • 1 \leq T \leq 100
  • 0 \leq X_1, Y_1, X_2, Y_2 \leq 10^9
  • 1 \leq R_1, R_2 \leq 10^9
  • 入力される値は全て整数

入力

入力は以下の形式で標準入力から与えられる。ここで \mathrm{case}_ii 番目のテストケースを意味する。

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

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

X_1 Y_1 R_1 X_2 Y_2 R_2

出力

T 行出力せよ。i 行目には i 番目のテストケースの答えを出力せよ。
各テストケースでは、円 C_1 と円 C_2 が共有点を持つ場合は Yes を、そうでない場合は No を出力せよ。


入力例 1

7
0 0 2 2 3 2
0 0 2 2 3 1
1 2 5 3 2 1
5 4 2 8 8 3
2 1 5 5 1 2
0 0 1 0 0 1
0 0 500000000 1 1000000000 500000000

出力例 1

Yes
No
No
Yes
Yes
Yes
No

例えば 1 番目のテストケースでは、C_1C_2 の位置関係は以下の図のようになります。

image

Score : 250 points

Problem Statement

There are two circles C_1 and C_2 on the xy-plane. In this problem, a circle refers to the circumference.
Circle C_1 has its center at (X_1, Y_1) and radius R_1.
Circle C_2 has its center at (X_2, Y_2) and radius R_2.
Determine whether circles C_1 and C_2 have a common point. In other words, determine whether there exists at least one point whose distance from (X_1, Y_1) is R_1 and whose distance from (X_2, Y_2) is R_2.

You are given T test cases; solve each one.

Constraints

  • 1 \leq T \leq 100
  • 0 \leq X_1, Y_1, X_2, Y_2 \leq 10^9
  • 1 \leq R_1, R_2 \leq 10^9
  • All input values are integers.

Input

The input is given from Standard Input in the following format, where \mathrm{case}_i denotes the i-th test case:

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

Each test case is given in the following format:

X_1 Y_1 R_1 X_2 Y_2 R_2

Output

Output T lines. The i-th line should contain the answer for the i-th test case.
For each test case, output Yes if circles C_1 and C_2 have a common point, and No otherwise.


Sample Input 1

7
0 0 2 2 3 2
0 0 2 2 3 1
1 2 5 3 2 1
5 4 2 8 8 3
2 1 5 5 1 2
0 0 1 0 0 1
0 0 500000000 1 1000000000 500000000

Sample Output 1

Yes
No
No
Yes
Yes
Yes
No

For example, in the first test case, the relative positions of C_1 and C_2 are as shown in the following figure.

image

C - Sushi

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

寿司の材料として、N 個のシャリと M 個のネタがあります。

i 番目のシャリの重さは A_ij 番目のネタの重さは B_j です。

あなたは、シャリとネタを組み合わせることで寿司を作ろうとしています。

寿司を 1 つ作るためには、1 つのシャリと 1 つのネタを組み合わせる必要があります。ただし、ネタの重さはシャリの重さの 2 倍以下でなければなりません。また、1 つのシャリやネタを複数の寿司に使うことはできません。

作ることのできる寿司の個数の最大値を求めてください。

制約

  • 1 \leq N, M \leq 2 \times 10^5
  • 1 \leq A_i, B_j \leq 10^9
  • 入力される値はすべて整数

入力

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

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

出力

答えを出力せよ。


入力例 1

4 5
4 2 1 8
14 9 3 2 9

出力例 1

3

1 番目のシャリと 3 番目のネタ、2 番目のシャリと 4 番目のネタ、4 番目のシャリと 1 番目のネタを組み合わせるなどの方法で 3 つの寿司を作ることができます。4 つ以上の寿司を作ることはできないため、3 を出力します。


入力例 2

3 3
5 5 3
11 1000 1000

出力例 2

0

入力例 3

8 7
2 3 4 4 4 3 2 3
8 5 5 9 9 7 1

出力例 3

5

Score : 300 points

Problem Statement

There are N pieces of shari (vinegared rice) and M pieces of neta (toppings) as ingredients for sushi.

The weight of the i-th shari is A_i, and the weight of the j-th neta is B_j.

You will make sushi by combining shari and neta.

To make one piece of sushi, you need to combine one shari with one neta. Here, the weight of the neta must be at most twice the weight of the shari. Also, the same shari or neta cannot be used in multiple pieces of sushi.

Find the maximum number of sushi that can be made.

Constraints

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

Output the answer.


Sample Input 1

4 5
4 2 1 8
14 9 3 2 9

Sample Output 1

3

By combining the 1st shari with the 3rd neta, the 2nd shari with the 4th neta, and the 4th shari with the 1st neta, for example, you can make three pieces of sushi. It is impossible to make four or more pieces of sushi, so output 3.


Sample Input 2

3 3
5 5 3
11 1000 1000

Sample Output 2

0

Sample Input 3

8 7
2 3 4 4 4 3 2 3
8 5 5 9 9 7 1

Sample Output 3

5
D - Repeatedly Repainting

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 425

問題文

HW 列のマス目があります。このマス目の上から i 行目、左から j 列目のマスをマス (i, j) と呼びます。

すべてのマスは白または黒で塗られています。マス目の情報は H 個の長さ W の文字列 S_1, S_2, \ldots, S_H によって与えられ、S_ij 文字目が . のときマス (i, j) は白で、S_ij 文字目が # のときマス (i, j) は黒で塗られています。

あなたは、以下の操作を 10^{100} 回行います。

  • すべてのマスに対して同時に以下の規則で色の塗り替えを行う。
    • 操作前に白く塗られているマスは、そのマスに隣接する黒く塗られているマスが存在するとき、またそのときに限り黒く塗り替える。ただし、マス (x, y) とマス (x', y') が隣接しているとは、片方のマスがもう片方のマスの 8 近傍にある、すなわち \max(|x-x'|, |y-y'|) = 1 であることを指す。
    • 操作前に黒く塗られているマスは、白く塗り替える。

操作を終えた後に各マスが何色で塗られているか求めてください。

制約

  • 1 \leq H \times W \leq 10^6
  • H, W は正整数
  • S_i., # からなる長さ W の文字列

入力

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

H W
S_1
S_2
\vdots
S_H

出力

H 行出力せよ。

各行に ., # からなる長さ W の文字列を 1 つずつ出力せよ。

i 行目の文字列の j 文字目は 10^{100} 回の操作を行った後にマス (i, j) が白で塗られているとき . に、黒で塗られているとき # になるようにせよ。


入力例 1

3 4
#.#.
.#..
#...

出力例 1

#.#.
.#..
#..#

はじめ、マス目は以下のようになっています。

1 回操作を行うと、マス目は以下のようになります。

10^{100} 回操作を行うと、マス目は以下のようになります。


入力例 2

3 3
###
###
###

出力例 2

...
...
...

入力例 3

5 7
.#.....
.......
..#....
.......
....#..

出力例 3

.#.##.#
....#..
#.#.###
#.....#
###.#.#

Score : 425 points

Problem Statement

There is a grid with H rows and W columns. The cell at the i-th row from the top and the j-th column from the left is called cell (i, j).

Every cell is colored white or black. The grid is described by H strings S_1, S_2, \ldots, S_H, each of length W. If the j-th character of S_i is ., cell (i, j) is white; if the j-th character of S_i is #, cell (i, j) is black.

You perform the following operation 10^{100} times.

  • Simultaneously apply the following rules to all cells.
    • A cell that is white before the operation becomes black if and only if there exists at least one black cell adjacent to it. Here, cells (x, y) and (x', y') are adjacent if and only if one of them is within the 8-neighborhood of the other, that is, \max(|x-x'|, |y-y'|) = 1.
    • A cell that is black before the operation becomes white.

Find the color of each cell after the operations.

Constraints

  • 1 \leq H \times W \leq 10^6
  • H and W are positive integers.
  • S_i is a string of length W consisting of . and #.

Input

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

H W
S_1
S_2
\vdots
S_H

Output

Output H lines.

Output one string of length W consisting of . and # per line.

The j-th character of the i-th line should be . if cell (i, j) is white after 10^{100} operations, and # if it is black.


Sample Input 1

3 4
#.#.
.#..
#...

Sample Output 1

#.#.
.#..
#..#

Initially, the grid is as follows.

After one operation, the grid is as follows.

After 10^{100} operations, the grid is as follows.


Sample Input 2

3 3
###
###
###

Sample Output 2

...
...
...

Sample Input 3

5 7
.#.....
.......
..#....
.......
....#..

Sample Output 3

.#.##.#
....#..
#.#.###
#.....#
###.#.#
E - x + y ≡ x + y

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 450

問題文

正整数 a,b に対して、ab を繋げて書いた時に出来る整数を \mathrm{concat}(a, b) と呼びます。厳密に述べると、\mathrm{concat}(a, b) を次のように定義します。

  • a, b10 進表記して出来る文字列を A, B とする。A, B をこの順に結合して出来る文字列を C とする。C10 進表記された整数とみなした時の値を \mathrm{concat}(a, b) とする。

例えば a = 123, b = 45 の時 \mathrm{concat}(a, b)=12345 です。

正整数 N, M が与えられます。
N 以下の正整数の組 (x,y) であって \mathrm{concat}(x, y) \equiv x + y \pmod{M} であるものの個数を 998244353 で割った余りを求めてください。

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

制約

  • 1 \leq T \leq 10^4
  • 1 \leq N \leq 10^{18}
  • 2 \leq M \leq 10^9
  • 入力される値は全て整数

入力

入力は以下の形式で標準入力から与えられる。ここで \mathrm{case}_ii 番目のテストケースを意味する。

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

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

N M

出力

T 行出力せよ。i 行目には i 番目のテストケースの答えを出力せよ。
各テストケースでは、条件を満たす (x,y) の個数を 998244353 で割った余りを出力せよ。


入力例 1

4
3 2
123 456
20260530 460
123456789123456789 998244353

出力例 1

3
0
922576091
422081792

1 番目のテストケースについて、条件を満たす (x,y)(2,1),(2,2),(2,3)3 個です。

Score : 450 points

Problem Statement

For positive integers a and b, define \mathrm{concat}(a, b) as the integer formed by writing a and b one after another. More formally, \mathrm{concat}(a, b) is defined as follows.

  • Let A and B be the strings formed by writing a and b in decimal, respectively. Let C be the string formed by concatenating A and B in this order. The value of C interpreted as an integer in decimal notation is \mathrm{concat}(a, b).

For example, if a = 123 and b = 45, then \mathrm{concat}(a, b) = 12345.

You are given positive integers N and M.
Find the number, modulo 998244353, of pairs (x, y) of positive integers not greater than N such that \mathrm{concat}(x, y) \equiv x + y \pmod{M}.

You are given T test cases; solve each one.

Constraints

  • 1 \leq T \leq 10^4
  • 1 \leq N \leq 10^{18}
  • 2 \leq M \leq 10^9
  • All input values are integers.

Input

The input is given from Standard Input in the following format, where \mathrm{case}_i denotes the i-th test case:

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

Each test case is given in the following format:

N M

Output

Output T lines. The i-th line should contain the answer for the i-th test case.
For each test case, output the number, modulo 998244353, of pairs (x, y) satisfying the condition.


Sample Input 1

4
3 2
123 456
20260530 460
123456789123456789 998244353

Sample Output 1

3
0
922576091
422081792

For the first test case, three pairs (x, y) satisfy the condition: (2, 1), (2, 2), (2, 3).

F - Farthest Pair Query

Time Limit: 4 sec / Memory Limit: 1024 MiB

配点 : 550

問題文

N 頂点の木があります。頂点には 1, 2, \ldots, N の番号が付けられており、i 番目の辺は頂点 U_i と頂点 V_i を結んでいます。

はじめ、すべての頂点は黒く塗られています。

以下のクエリを Q 個順に処理したときの各クエリの答えを求めてください。

  • 整数 x (1 \leq x \leq N) が与えられる。頂点 x が白く塗られているならば黒く、黒く塗られているならば白く塗り替える。その後、黒く塗られている頂点どうしの距離の最大値を求める。ただし、木上の 2 頂点間の距離とは、その 2 頂点を端点とする単純パスに含まれる辺の本数のことを指す。

なお、クエリを順に処理したとき、操作の過程で黒く塗られている頂点が常に 2 個以上存在するような入力のみが与えられます。

制約

  • 3 \leq N \leq 10^5
  • 1 \leq U_i, V_i \leq N
  • 与えられるグラフは木
  • 1 \leq Q \leq 10^5
  • 各クエリについて、1 \leq x \leq N
  • どの時点でも黒く塗られている頂点が 2 個以上存在する
  • 入力される値はすべて整数

入力

入力は以下の形式で標準入力から与えられる。ここで \text{query}_ii 番目のクエリを意味する。

N
U_1 V_1
U_2 V_2
\vdots
U_{N-1} V_{N-1}
Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

各クエリは以下の形式で与えられる。

x

出力

Q 行出力せよ。

i 行目には、クエリを順に処理したときの i 番目のクエリの答えを出力せよ。


入力例 1

7
1 2
2 3
2 4
6 7
3 5
7 3
9
1
4
2
6
3
1
1
4
6

出力例 1

4
3
3
2
2
3
2
3
4
  • 1 番目のクエリで色を塗り替えた後、黒く塗られている頂点は 2,3,4,5,6,7 です。 頂点 4 と頂点 6 の距離は 4 であり、答えは 4 です。
  • 2 番目のクエリで色を塗り替えた後、黒く塗られている頂点は 2,3,5,6,7 です。 頂点 2 と頂点 6 の距離は 3 であり、答えは 3 です。
  • 3 番目のクエリで色を塗り替えた後、黒く塗られている頂点は 3,5,6,7 です。 頂点 5 と頂点 6 の距離は 3 であり、答えは 3 です。
  • 4 番目のクエリで色を塗り替えた後、黒く塗られている頂点は 3,5,7 です。 頂点 5 と頂点 7 の距離は 2 であり、答えは 2 です。
  • 5 番目のクエリで色を塗り替えた後、黒く塗られている頂点は 5,7 です。 頂点 5 と頂点 7 の距離は 2 であり、答えは 2 です。
  • 6 番目のクエリで色を塗り替えた後、黒く塗られている頂点は 1,5,7 です。 頂点 1 と頂点 5 の距離は 3 であり、答えは 3 です。
  • 7 番目のクエリで色を塗り替えた後、黒く塗られている頂点は 5,7 です。 頂点 5 と頂点 7 の距離は 2 であり、答えは 2 です。
  • 8 番目のクエリで色を塗り替えた後、黒く塗られている頂点は 4,5,7 です。 頂点 4 と頂点 5 の距離は 3 であり、答えは 3 です。
  • 9 番目のクエリで色を塗り替えた後、黒く塗られている頂点は 4,5,6,7 です。 頂点 4 と頂点 6 の距離は 4 であり、答えは 4 です。

上に挙げた例は最大値を達成する一例であることに注意してください。

Score : 550 points

Problem Statement

There is a tree with N vertices. The vertices are numbered 1, 2, \ldots, N, and the i-th edge connects vertices U_i and V_i.

Initially, all vertices are painted black.

Process Q queries of the following form in order and find the answer for each query.

  • An integer x (1 \leq x \leq N) is given. If vertex x is white, repaint it black; if vertex x is black, repaint it white. Then, find the maximum distance between two black vertices. Here, the distance between two vertices on a tree is the number of edges in the simple path between them.

In the given inputs, there are always at least two black vertices when processing the queries in order.

Constraints

  • 3 \leq N \leq 10^5
  • 1 \leq U_i, V_i \leq N
  • The given graph is a tree.
  • 1 \leq Q \leq 10^5
  • For each query, 1 \leq x \leq N.
  • There are always at least two black vertices.
  • All input values are integers.

Input

The input is given from Standard Input in the following format, where \text{query}_i denotes the i-th query:

N
U_1 V_1
U_2 V_2
\vdots
U_{N-1} V_{N-1}
Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

Each query is given in the following format:

x

Output

Output Q lines.

The i-th line should contain the answer for the i-th query when processed in order.


Sample Input 1

7
1 2
2 3
2 4
6 7
3 5
7 3
9
1
4
2
6
3
1
1
4
6

Sample Output 1

4
3
3
2
2
3
2
3
4
  • After the repainting in the 1st query, the black vertices are 2, 3, 4, 5, 6, 7. The distance between vertices 4 and 6 is 4, and the answer is 4.
  • After the repainting in the 2nd query, the black vertices are 2, 3, 5, 6, 7. The distance between vertices 2 and 6 is 3, and the answer is 3.
  • After the repainting in the 3rd query, the black vertices are 3, 5, 6, 7. The distance between vertices 5 and 6 is 3, and the answer is 3.
  • After the repainting in the 4th query, the black vertices are 3, 5, 7. The distance between vertices 5 and 7 is 2, and the answer is 2.
  • After the repainting in the 5th query, the black vertices are 5, 7. The distance between vertices 5 and 7 is 2, and the answer is 2.
  • After the repainting in the 6th query, the black vertices are 1, 5, 7. The distance between vertices 1 and 5 is 3, and the answer is 3.
  • After the repainting in the 7th query, the black vertices are 5, 7. The distance between vertices 5 and 7 is 2, and the answer is 2.
  • After the repainting in the 8th query, the black vertices are 4, 5, 7. The distance between vertices 4 and 5 is 3, and the answer is 3.
  • After the repainting in the 9th query, the black vertices are 4, 5, 6, 7. The distance between vertices 4 and 6 is 4, and the answer is 4.

Note that the examples given above are just one instance achieving the maximum value.

G - Vertex Flip Query

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 650

問題文

頂点に 1 から N の番号がついた N 頂点の木があります。i 本目の辺は頂点 a_i と頂点 b_i を結ぶ辺です。頂点 i には重み W_i と色 C_i \in \lbrace 0,1 \rbrace が設定されています。
クエリを Q 個処理してください。クエリは以下の 3 種類のいずれかです。

  • 1 v:頂点 v について C_v1-C_v に変更する。
  • 2 v x:頂点 v について W_vW_v + x に変更する。
  • 3 v :頂点 v の色を c とする。頂点 v から色が c である頂点のみを通って到達可能な頂点(頂点 v 自身も含む)全てについての重みの総和を出力する。

制約

  • 1 \leq N \leq 3 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq W_i \leq 10^9
  • C_i \in \lbrace 0,1 \rbrace
  • 1 \leq a_i \lt b_i \leq N
  • 入力されるグラフは木
  • 1 \leq v \leq N
  • 1 \leq x \leq 10^9
  • 入力される値は全て整数

入力

入力は以下の形式で標準入力から与えられる。ここで \mathrm{query}_ii 番目のクエリを意味する。

N Q
W_1 W_2 \dots W_N
C_1 C_2 \dots C_N
a_1 b_1
a_2 b_2
\vdots
a_{N-1} b_{N-1}
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

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

1 v
2 v x
3 v

出力

与えられた 3 種類目のクエリの個数を m として、m 行出力せよ。i 行目には i 番目に与えられた 3 種類目のクエリの答えを 1 行で出力せよ。


入力例 1

5 9
1 10 100 1000 10000
0 0 0 0 0
1 2
2 3
3 4
2 5
3 1
1 2
3 1
3 2
3 3
1 3
1 2
2 1 1
3 5

出力例 1

11111
1
10
1100
10012

例えば 1 番目のクエリでは、頂点 1 から色が 0 である頂点のみを通って到達可能な頂点の集合は \lbrace 1,2,3,4,5 \rbrace です。


入力例 2

10 25
1 10 100 1000 10000 100000 1000000 10000000 100000000 1000000000
0 1 0 0 0 1 0 0 0 0
1 2
1 3
2 4
4 5
4 6
2 7
5 8
8 9
6 10
3 4
1 8
2 6 100000
3 3
1 3
1 7
1 7
1 7
3 8
2 1 1
2 6 100000
2 8 10000000
1 5
2 5 10000
1 9
2 7 1000000
1 7
2 10 1000000000
1 9
1 6
1 9
1 1
3 7
2 5 10000
3 6

出力例 2

110011000
101
10000000
2000000
2000301000

Score : 650 points

Problem Statement

There is a tree with N vertices numbered 1 to N. The i-th edge connects vertices a_i and b_i. Each vertex i has a weight W_i and a color C_i \in \lbrace 0,1 \rbrace.
Process Q queries. There are three types of queries as follows.

  • 1 v: Change C_v to 1 - C_v for vertex v.
  • 2 v x: Change W_v to W_v + x for vertex v.
  • 3 v: Let c be the color of vertex v. Output the sum of weights of all vertices reachable from vertex v by traveling only through vertices of color c (including vertex v itself).

Constraints

  • 1 \leq N \leq 3 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq W_i \leq 10^9
  • C_i \in \lbrace 0,1 \rbrace
  • 1 \leq a_i \lt b_i \leq N
  • The input graph is a tree.
  • 1 \leq v \leq N
  • 1 \leq x \leq 10^9
  • All input values are integers.

Input

The input is given from Standard Input in the following format, where \mathrm{query}_i denotes the i-th query:

N Q
W_1 W_2 \dots W_N
C_1 C_2 \dots C_N
a_1 b_1
a_2 b_2
\vdots
a_{N-1} b_{N-1}
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

Each query is given in one of the following formats:

1 v
2 v x
3 v

Output

Let m be the number of type-3 queries given. Output m lines. The i-th line should contain the answer for the i-th type-3 query.


Sample Input 1

5 9
1 10 100 1000 10000
0 0 0 0 0
1 2
2 3
3 4
2 5
3 1
1 2
3 1
3 2
3 3
1 3
1 2
2 1 1
3 5

Sample Output 1

11111
1
10
1100
10012

For example, for the first query, the set of vertices reachable from vertex 1 by traveling only through vertices of color 0 is \lbrace 1,2,3,4,5 \rbrace.


Sample Input 2

10 25
1 10 100 1000 10000 100000 1000000 10000000 100000000 1000000000
0 1 0 0 0 1 0 0 0 0
1 2
1 3
2 4
4 5
4 6
2 7
5 8
8 9
6 10
3 4
1 8
2 6 100000
3 3
1 3
1 7
1 7
1 7
3 8
2 1 1
2 6 100000
2 8 10000000
1 5
2 5 10000
1 9
2 7 1000000
1 7
2 10 1000000000
1 9
1 6
1 9
1 1
3 7
2 5 10000
3 6

Sample Output 2

110011000
101
10000000
2000000
2000301000