Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
1 以上 9 以下の数字 A,B,C が与えられます。
A,B,C を好きな順番で並べて繋げることで作れる 3 桁の整数のうち、値が最大のものを求めてください。
制約
- A,B,C は 1 以上 9 以下の数字
入力
入力は以下の形式で標準入力から与えられる。
A B C
出力
答えを出力せよ。
入力例 1
3 2 4
出力例 1
432
A,B,C を好きな順番で並べて繋げることで作れる 3 桁の整数は 324, 342, 234, 243, 432, 423 の 6 通りであり、 このうち値が最大のものは 432 です。
入力例 2
7 7 7
出力例 2
777
777 のみを作ることができます。
入力例 3
9 1 9
出力例 3
991
Score : 100 points
Problem Statement
You are given three digits A,B,C between 1 and 9, inclusive.
Find the maximum value among all 3-digit integers that can be formed by arranging A,B,C in any order and concatenating them.
Constraints
- A,B,C are digits between 1 and 9, inclusive.
Input
The input is given from Standard Input in the following format:
A B C
Output
Output the answer.
Sample Input 1
3 2 4
Sample Output 1
432
There are six 3-digit integers that can be formed by arranging A,B,C in any order and concatenating them: 324, 342, 234, 243, 432, 423; the maximum value among them is 432.
Sample Input 2
7 7 7
Sample Output 2
777
Only 777 can be formed.
Sample Input 3
9 1 9
Sample Output 3
991
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
整数 N が与えられるので、 2^N-2N の値を計算して出力してください。
制約
- N は 1 以上 11 以下の整数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
答えを出力せよ。
入力例 1
1
出力例 1
0
この入力で与えられる N の値は 1 なので、 2^1 - 2 \times 1 の値である 0 を出力してください。
入力例 2
2
出力例 2
0
この入力で与えられる N の値は 2 なので、 2^2 - 2 \times 2 の値である 0 を出力してください。
入力例 3
11
出力例 3
2026
この入力で与えられる N の値は 11 なので、 2^{11} - 2 \times 11 の値である 2026 を出力してください。
Score : 100 points
Problem Statement
You are given an integer N. Compute and output the value of 2^N-2N.
Constraints
- N is an integer between 1 and 11, inclusive.
Input
The input is given from Standard Input in the following format:
N
Output
Output the answer.
Sample Input 1
1
Sample Output 1
0
The value of N given in this input is 1, so output the value of 2^1 - 2 \times 1, which is 0.
Sample Input 2
2
Sample Output 2
0
The value of N given in this input is 2, so output the value of 2^2 - 2 \times 2, which is 0.
Sample Input 3
11
Sample Output 3
2026
The value of N given in this input is 11, so output the value of 2^{11} - 2 \times 11, which is 2026.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
数直線の原点に高橋君がいます。高橋君は座標 X にあるゴールに移動しようとしています。
座標 Y には壁があり、最初、高橋君は壁を超えて移動することができません。
座標 Z にあるハンマーを拾った後でなら、壁を破壊して通過できるようになります。
高橋君がゴールに到達することが可能か判定し、可能であれば移動距離の最小値を求めてください。
制約
- -1000 \leq X,Y,Z \leq 1000
- X,Y,Z は相異なり、いずれも 0 でない
- 入力に含まれる値は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
X Y Z
出力
高橋君がゴールに到達することが可能であれば、移動距離の最小値を出力せよ。不可能であれば、かわりに -1 と出力せよ。
入力例 1
10 -10 1
出力例 1
10
高橋君はまっすぐゴールに向かうことができます。
入力例 2
20 10 -10
出力例 2
40
ゴールは壁の向こう側にあります。まずハンマーを拾い、壁を壊すことでゴールに到達することができます。
入力例 3
100 1 1000
出力例 3
-1
Score : 200 points
Problem Statement
Takahashi is at the origin of a number line. He wants to reach a goal at coordinate X.
There is a wall at coordinate Y, which Takahashi cannot go beyond at first.
However, after picking up a hammer at coordinate Z, he can destroy that wall and pass through.
Determine whether Takahashi can reach the goal. If he can, find the minimum total distance he needs to travel to do so.
Constraints
- -1000 \leq X,Y,Z \leq 1000
- X, Y, and Z are distinct, and none of them is 0.
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
X Y Z
Output
If Takahashi can reach the goal, print the minimum total distance he needs to travel to do so. If he cannot, print -1 instead.
Sample Input 1
10 -10 1
Sample Output 1
10
Takahashi can go straight to the goal.
Sample Input 2
20 10 -10
Sample Output 2
40
The goal is beyond the wall. He can get there by first picking up the hammer and then destroying the wall.
Sample Input 3
100 1 1000
Sample Output 3
-1
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
N 行 N 列のマス目が与えられます。上から i 行目、左から j 列目のマスには整数 A_{i,j} が書かれています。ここで、A_{i,j} は 0 か 1 であることが保証されます。
マス目の外側のマスに書かれた整数を時計回りに 1 個ずつずらしたときのマス目を出力してください。
ただし外側のマスとは、1 行目、N 行目、1 列目、N 列目のいずれか 1 つ以上に属するマスの集合のことを指します。
制約
- 2 \le N \le 100
- 0 \le A_{i,j} \le 1(1 \le i,j \le N)
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N
A_{1,1}A_{1,2}\dots A_{1,N}
A_{2,1}A_{2,2}\dots A_{2,N}
\vdots
A_{N,1}A_{N,2}\dots A_{N,N}
出力
マス目の外側のマスに書かれた整数を時計回りに 1 個ずつずらしたときのマス目において、上から i 行目、左から j 列目のマスに書かれている整数を B_{i,j} と置く。このとき、以下の形式で出力せよ。
B_{1,1}B_{1,2}\dots B_{1,N}
B_{2,1}B_{2,2}\dots B_{2,N}
\vdots
B_{N,1}B_{N,2}\dots B_{N,N}
入力例 1
4 0101 1101 1111 0000
出力例 1
1010 1101 0111 0001
上から i 行目、左から j 列目のマスを (i,j) と呼ぶこととします。
外側のマスは (1,1) から時計回りに列挙すると (1,1),(1,2),(1,3),(1,4),(2,4),(3,4),(4,4),(4,3),(4,2),(4,1),(3,1),(2,1) の 12 個です。
これらのマスに書かれている整数を、時計回りに 1 個ずつ動かすと出力欄のようになります。
入力例 2
2 11 11
出力例 2
11 11
入力例 3
5 01010 01001 10110 00110 01010
出力例 3
00101 11000 00111 00110 10100
Score : 200 points
Problem Statement
You are given a grid with N rows and N columns. An integer A_{i, j} is written on the square at the i-th row from the top and j-th column from the left. Here, it is guaranteed that A_{i,j} is either 0 or 1.
Shift the integers written on the outer squares clockwise by one square each, and print the resulting grid.
Here, the outer squares are those in at least one of the 1-st row, N-th row, 1-st column, and N-th column.
Constraints
- 2 \le N \le 100
- 0 \le A_{i,j} \le 1(1 \le i,j \le N)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N
A_{1,1}A_{1,2}\dots A_{1,N}
A_{2,1}A_{2,2}\dots A_{2,N}
\vdots
A_{N,1}A_{N,2}\dots A_{N,N}
Output
Let B_{i,j} be the integer written on the square at the i-th row from the top and j-th column from the left in the grid resulting from shifting the outer squares clockwise by one square each. Print them in the following format:
B_{1,1}B_{1,2}\dots B_{1,N}
B_{2,1}B_{2,2}\dots B_{2,N}
\vdots
B_{N,1}B_{N,2}\dots B_{N,N}
Sample Input 1
4 0101 1101 1111 0000
Sample Output 1
1010 1101 0111 0001
We denote by (i,j) the square at the i-th row from the top and j-th column from the left.
The outer squares, in clockwise order starting from (1,1), are the following 12 squares: (1,1),(1,2),(1,3),(1,4),(2,4),(3,4),(4,4),(4,3),(4,2),(4,1),(3,1), and (2,1).
The sample output shows the resulting grid after shifting the integers written on those squares clockwise by one square.
Sample Input 2
2 11 11
Sample Output 2
11 11
Sample Input 3
5 01010 01001 10110 00110 01010
Sample Output 3
00101 11000 00111 00110 10100
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
1,2,\dots,N が 1 回ずつ現れる長さ N の数列を「長さ N の順列」と呼びます。
長さ N の順列 P = (p_1, p_2,\dots,p_N) が与えられるので、以下の条件を満たす長さ N の順列 Q = (q_1,\dots,q_N) を出力してください。
- 全ての i (1 \leq i \leq N) に対して Q の p_i 番目の要素が i である。
ただし、条件を満たす Q は必ずただ 1 つ存在することが証明できます。
制約
- 1 \leq N \leq 2 \times 10^5
- (p_1,p_2,\dots,p_N) は長さ N の順列である。
- 入力は全て整数である。
入力
入力は以下の形式で標準入力から与えられる。
N p_1 p_2 \dots p_N
出力
数列 Q を空白区切りで 1 行で出力せよ。
q_1 q_2 \dots q_N
入力例 1
3 2 3 1
出力例 1
3 1 2
以下に説明する通り、 Q=(3,1,2) は条件を満たす順列です。
- i = 1 のとき p_i = 2, q_2 = 1
- i = 2 のとき p_i = 3, q_3 = 2
- i = 3 のとき p_i = 1, q_1 = 3
入力例 2
3 1 2 3
出力例 2
1 2 3
全ての i (1 \leq i \leq N) に対して p_i = i が成り立つときは P = Q になります。
入力例 3
5 5 3 2 4 1
出力例 3
5 3 2 4 1
Score : 300 points
Problem Statement
We will call a sequence of length N where each of 1,2,\dots,N occurs once as a permutation of length N.
Given a permutation of length N, P = (p_1, p_2,\dots,p_N), print a permutation of length N, Q = (q_1,\dots,q_N), that satisfies the following condition.
- For every i (1 \leq i \leq N), the p_i-th element of Q is i.
It can be proved that there exists a unique Q that satisfies the condition.
Constraints
- 1 \leq N \leq 2 \times 10^5
- (p_1,p_2,\dots,p_N) is a permutation of length N (defined in Problem Statement).
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N p_1 p_2 \dots p_N
Output
Print the sequence Q in one line, with spaces in between.
q_1 q_2 \dots q_N
Sample Input 1
3 2 3 1
Sample Output 1
3 1 2
The permutation Q=(3,1,2) satisfies the condition, as follows.
- For i = 1, we have p_i = 2, q_2 = 1.
- For i = 2, we have p_i = 3, q_3 = 2.
- For i = 3, we have p_i = 1, q_1 = 3.
Sample Input 2
3 1 2 3
Sample Output 2
1 2 3
If p_i = i for every i (1 \leq i \leq N), we will have P = Q.
Sample Input 3
5 5 3 2 4 1
Sample Output 3
5 3 2 4 1
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
英大文字のみからなる文字列 S が与えられます。
S に対して、次の手順を行った後に得られる文字列を出力してください。
文字列が
WAを(連続する)部分文字列として含む限り、次の操作を繰り返す。
- 文字列中に登場する
WAのうち最も先頭のものをACに置換する。
なお、本問題の制約下で、この操作は高々有限回しか繰り返されないことが証明できます。
制約
- S は長さ 1 以上 3\times 10^5 以下の英大文字のみからなる文字列
入力
入力は以下の形式で標準入力から与えられる。
S
出力
S に対して、問題文中に記された手順を行った後に得られる文字列を出力せよ。
入力例 1
WACWA
出力例 1
ACCAC
最初、文字列は S=WACWA です。
この文字列には 1 文字目から 2 文字目、および、4 文字目から 5 文字目の 2 ヶ所に WA が部分文字列として含まれています。
1 回目の操作では、そのうち先頭のもの、すなわち 1 文字目から 2 文字目の部分を AC に置換し、文字列は ACCWA となります。
1 回目の操作後の文字列には 4 文字目から 5 文字目の 1 ヶ所にのみ WA が部分文字列として含まれているため、2 回目の操作ではこれを AC に置換し、文字列は ACCAC となります。
ACCAC は WA を部分文字列として含まないため、手順は終了します。よって、ACCAC を出力します。
入力例 2
WWA
出力例 2
ACC
最初、文字列は S=WWA です。
この文字列には 2 文字目から 3 文字目の 1 ヶ所にのみ WA が部分文字列として含まれているため、1 回目の操作ではこれを AC に置換し、文字列は WAC となります。
次に、1 回目の操作後の文字列は 1 文字目から 2 文字目の 1 ヶ所にのみ WA が部分文字列として含まれているため、2 回目の操作ではこれを AC に置換し、文字列は ACC となります。
ACC は WA を部分文字列として含まないため、手順は終了します。よって、ACC を出力します。
入力例 3
WWWWW
出力例 3
WWWWW
S には最初から WA が部分文字列として含まれてないため、操作は 1 回も行われず手順は終了します。よって、WWWWW を出力します。
Score : 300 points
Problem Statement
You are given a string S consisting of uppercase English letters.
Apply the following procedure to S, and then output the resulting string:
As long as the string contains
WAas a (contiguous) substring, repeat the following operation:
- Among all occurrences of
WAin the string, replace the leftmost one withAC.
It can be proved under the constraints of this problem that this operation is repeated at most a finite number of times.
Constraints
- S is a string of uppercase English letters with length between 1 and 3\times 10^5, inclusive.
Input
The input is given from Standard Input in the following format:
S
Output
Print the resulting string after performing the procedure described in the problem statement on S.
Sample Input 1
WACWA
Sample Output 1
ACCAC
Initially, the string is S= WACWA.
This string contains WA as a substring in two places: from the 1st to the 2nd character, and from the 4th to the 5th character.
In the first operation, we replace the leftmost occurrence (the substring from the 1st to the 2nd character) with AC, resulting in ACCWA.
After the first operation, the string contains WA as a substring in exactly one place: from the 4th to the 5th character.
In the second operation, we replace it with AC, resulting in ACCAC.
Since ACCAC does not contain WA as a substring, the procedure ends. Therefore, we output ACCAC.
Sample Input 2
WWA
Sample Output 2
ACC
Initially, the string is S= WWA.
This string contains WA as a substring in exactly one place: from the 2nd to the 3rd character.
In the first operation, we replace it with AC, resulting in WAC.
Then, after the first operation, the string contains WA in exactly one place: from the 1st to the 2nd character.
In the second operation, we replace it with AC, resulting in ACC.
Since ACC does not contain WA as a substring, the procedure ends. Therefore, we output ACC.
Sample Input 3
WWWWW
Sample Output 3
WWWWW
Since S does not contain WA as a substring from the start, no operations are performed and the procedure ends immediately. Therefore, we output WWWWW.
Time Limit: 4 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
N = 2^{20} 項からなる数列 A = (A_0, A_1, \dots, A_{N - 1}) があります。はじめ、全ての要素は -1 です。
Q 個のクエリを順番に処理してください。i \, (1 \leq i \leq Q) 個目のクエリは t_i = 1 または t_i = 2 を満たす整数 t_i および整数 x_i で表され、内容は以下の通りです。
- t_i = 1 のとき、以下の処理を順番に行う。
- 整数 h を h = x_i で定める。
- A_{h \bmod N} \neq -1 である間、h の値を 1 増やすことを繰り返す。この問題の制約下でこの操作が有限回で終了することは証明できる。
- A_{h \bmod N} の値を x_i で書き換える。
- t_i = 2 のとき、その時点での A_{x_i \bmod N} の値を出力する。
なお、整数 a, b に対し、a を b で割った余りを a \bmod b と表します。
制約
- 1 \leq Q \leq 2 \times 10^5
- t_i \in \{ 1, 2 \} \, (1 \leq i \leq Q)
- 0 \leq x_i \leq 10^{18} \, (1 \leq i \leq Q)
- t_i = 2 であるような i \, (1 \leq i \leq Q) が 1 つ以上存在する。
- 入力は全て整数である。
入力
入力は以下の形式で標準入力から与えられる。
Q
t_1 x_1
\vdots
t_{Q} x_{Q}
出力
t_i = 2 であるようなクエリに対し、それぞれ答えを 1 行に出力せよ。そのようなクエリが 1 つ以上存在することは保証される。
入力例 1
4 1 1048577 1 1 2 2097153 2 3
出力例 1
1048577 -1
x_1 \bmod N = 1 であるので、1 番目のクエリによって A_1 = 1048577 となります。
2 番目のクエリにおいて、はじめ h = x_2 ですが、A_{h \bmod N} = A_{1} \neq -1 であるので h の値を 1 増やします。すると A_{h \bmod N} = A_{2} = -1 となるので、このクエリによって A_2 = 1 となります。
3 番目のクエリにおいて、A_{x_3 \bmod N} = A_{1} = 1048577 を出力します。
4 番目のクエリにおいて、A_{x_4 \bmod N} = A_{3} = -1 を出力します。
この問題において N = 2^{20} = 1048576 は定数であり、入力では与えられないことに注意してください。
Score : 400 points
Problem Statement
There is a sequence A = (A_0, A_1, \dots, A_{N - 1}) with N = 2^{20} terms. Initially, every term is -1.
Process Q queries in order. The i-th query (1 \leq i \leq Q) is described by an integer t_i such that t_i = 1 or t_i = 2, and another integer x_i, as follows.
- If t_i = 1, do the following in order.
- Define an integer h as h = x_i.
- While A_{h \bmod N} \neq -1, keep adding 1 to h. We can prove that this process ends after finite iterations under the Constraints of this problem.
- Replace the value of A_{h \bmod N} with x_i.
- If t_i = 2, print the value of A_{x_i \bmod N} at that time.
Here, for integers a and b, a \bmod b denotes the remainder when a is divided by b.
Constraints
- 1 \leq Q \leq 2 \times 10^5
- t_i \in \{ 1, 2 \} \, (1 \leq i \leq Q)
- 0 \leq x_i \leq 10^{18} \, (1 \leq i \leq Q)
- There is at least one i (1 \leq i \leq Q) such that t_i = 2.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Q
t_1 x_1
\vdots
t_{Q} x_{Q}
Output
For each query with t_i = 2, print the response in one line. It is guaranteed that there is at least one such query.
Sample Input 1
4 1 1048577 1 1 2 2097153 2 3
Sample Output 1
1048577 -1
We have x_1 \bmod N = 1, so the first query sets A_1 = 1048577.
In the second query, initially we have h = x_2, for which A_{h \bmod N} = A_{1} \neq -1, so we add 1 to h. Now we have A_{h \bmod N} = A_{2} = -1, so this query sets A_2 = 1.
In the third query, we print A_{x_3 \bmod N} = A_{1} = 1048577.
In the fourth query, we print A_{x_4 \bmod N} = A_{3} = -1.
Note that, in this problem, N = 2^{20} = 1048576 is a constant and not given in input.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
整数 A, X, M が与えられます。\displaystyle \sum_{i = 0}^{X-1} A^i を M で割った余りを求めてください。
制約
- 1 \leq A, M \leq 10^9
- 1 \leq X \leq 10^{12}
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
A X M
出力
答えを出力せよ。
入力例 1
3 4 7
出力例 1
5
3^0 + 3^1 + 3^2 + 3^3 = 40 です。40 を 7 で割った余りは 5 であるため、5 を出力します。
入力例 2
8 10 9
出力例 2
0
入力例 3
1000000000 1000000000000 998244353
出力例 3
919667211
Score : 500 points
Problem Statement
Given integers A, X, and M, find \displaystyle \sum_{i = 0}^{X-1} A^i, modulo M.
Constraints
- 1 \leq A, M \leq 10^9
- 1 \leq X \leq 10^{12}
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
A X M
Output
Print the answer.
Sample Input 1
3 4 7
Sample Output 1
5
3^0 + 3^1 + 3^2 + 3^3 = 40, which equals 5 modulo 7, so 5 should be printed.
Sample Input 2
8 10 9
Sample Output 2
0
Sample Input 3
1000000000 1000000000000 998244353
Sample Output 3
919667211
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
N 頂点の木 T があり、
頂点および辺はそれぞれ頂点 1, 頂点 2, \ldots, 頂点 N、辺 1, 辺 2, \ldots, 辺 (N-1) と番号づけられています。
特に、辺 i (1\leq i\leq N-1) は頂点 U_i と頂点 V_i を結んでいます。
また、各頂点には重みが定められており、最初、各頂点の重みはすべて 1 です。
Q 個のクエリが与えられるので、順に処理してください。 各クエリは次の 2 種類のいずれかです。
1 x w: 頂点 x の重みを w 増加させる。2 y: 辺 y を削除すると、T は 2 つの部分木(連結成分)に分かれる。それぞれの部分木に含まれる頂点の重みの総和をその部分木の重みとするとき、2 つの部分木の重みの差を出力する。
2 種類目のクエリについて、T から任意の辺を 1 つ選んで削除したとき、T は必ず 2 つの部分木に分かれることが証明できます。
また、2 種類目のクエリでは、実際に辺を削除しているわけではないことに注意してください。
制約
- 2\leq N\leq 3\times 10^5
- 1\leq U_i,V_i\leq N
- 1\leq Q\leq 3\times 10^5
- 1\leq x\leq N
- 1\leq w\leq 1000
- 1\leq y\leq N-1
- 入力はすべて整数
- 与えられるグラフは木である。
- 2 種類目のクエリが少なくとも 1 つ存在する。
入力
入力は以下の形式で標準入力から与えられる。
N
U_1 V_1
U_2 V_2
\vdots
U_{N-1} V_{N-1}
Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
各クエリ \mathrm{query}_i (1\leq i\leq Q) は次の形式のいずれかで与えられる。
1 x w
2 y
出力
2 種類目のクエリの個数を K 個として、K 行出力せよ。i 行目 (1\leq i\leq K) には i 番目の 2 種類目のクエリに対する答えを出力せよ。
入力例 1
6 1 2 1 3 2 4 4 5 4 6 5 2 1 1 1 3 2 1 1 4 10 2 5
出力例 1
2 1 17
木 T の構造、頂点番号の対応は下図左の通りです。最初、各頂点の重みはすべて 1 です。
1 番目のクエリでは、辺 1 を削除することを考えます。
このとき、頂点 1 を含む部分木と頂点 2 を含む部分木に分かれます。
頂点 1 を含む部分木の重みは 2、頂点 2 を含む部分木の重みは 4 であるため、その差の 2 を出力します。(下図右)

2 番目のクエリでは、頂点 1 の重みを 3 増加させます。
3 番目のクエリでは、辺 1 を削除することを考えます。
頂点 1 を含む部分木の重みは 5、頂点 2 を含む部分木の重みは 4 であるため、その差の 1 を出力します。(下図左)
4 番目のクエリでは、頂点 4 の重みを 10 増加させます。
5 番目のクエリでは、辺 5 を削除することを考えます。
このとき、頂点 4 を含む部分木と頂点 6 のみからなる部分木に分かれます。
頂点 4 を含む部分木の重みは 18、頂点 6 のみからなる部分木の重みは 1 であるため、その差の 17 を出力します。(下図右)

よって、2 種類目のクエリの答えである 2,1,17 をこの順に改行区切りで出力します。
Score : 500 points
Problem Statement
There is a tree T with N vertices.
The vertices are labeled as vertex 1, vertex 2, \ldots, vertex N, and the edges are labeled as edge 1, edge 2, \ldots, edge (N-1).
Edge i (1\leq i\leq N-1) connects vertices U_i and V_i.
Each vertex has a weight; initially, the weight of every vertex is 1.
You are given Q queries to process in order. Each query is of one of the following two types:
1 x w: Increase the weight of vertex x by w.2 y: If edge y were removed, the tree T would be split into two subtrees (connected components). For each subtree, let its weight be the sum of the weights of its vertices. Output the difference between the weights of the two subtrees.
For a query of the second type, it can be proved that removing any edge of T always splits it into exactly two subtrees.
Note that queries of the second type do not actually delete remove the edge.
Constraints
- 2 \leq N \leq 3 \times 10^5
- 1 \leq U_i, V_i \leq N
- 1 \leq Q \leq 3 \times 10^5
- 1 \leq x \leq N
- 1 \leq w \leq 1000
- 1 \leq y \leq N-1
- All input values are integers.
- The given graph is a tree.
- There is at least one query of the second type.
Input
The input is given from Standard Input in the following format:
N
U_1 V_1
U_2 V_2
\vdots
U_{N-1} V_{N-1}
Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
Each query \mathrm{query}_i (1\leq i\leq Q) is given in one of the following formats:
1 x w
2 y
Output
Let K be the number of queries of the second type. Output K lines; the i-th line (1 \leq i \leq K) should contain the answer to the i-th query of the second type.
Sample Input 1
6 1 2 1 3 2 4 4 5 4 6 5 2 1 1 1 3 2 1 1 4 10 2 5
Sample Output 1
2 1 17
The structure of the tree T and the vertex numbering are as shown on the left of the figure below. Initially, the weight of every vertex is 1.
In the 1st query, consider deleting edge 1.
The tree splits into the subtree containing vertex 1 and the subtree containing vertex 2.
The subtree with vertex 1 has weight 2; the subtree with vertex 2 has weight 4. Output the difference, 2 (figure below, right).

The 2nd query increases the weight of vertex 1 by 3.
In the 3rd query, consider deleting edge 1.
The subtree with vertex 1 has weight 5; the subtree with vertex 2 has weight 4. Output the difference, 1 (figure below, left).
The 4th query increases the weight of vertex 4 by 10.
In the 5th query, consider deleting edge 5.
The tree splits into the subtree containing vertex 4 and the subtree consisting only of vertex 6.
The subtree with vertex 4 has weight 18; the subtree with vertex 6 has weight 1. Output the difference, 17 (figure below, right).

Thus, output the answers to the queries of second type in order: 2, 1, and 17, separated by newlines.