実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
長さ N の文字列 S が与えられます。
S のうち先頭に連続する o をすべて取り除いた文字列を出力してください。
なお、 S 中のすべての文字が o である場合は空文字列を出力してください。
制約
- N は 1 \le N \le 50 を満たす整数
- S は英小文字からなる長さ N の文字列
入力
入力は以下の形式で標準入力から与えられる。
N S
出力
答えを出力せよ。
入力例 1
7 ooparts
出力例 1
parts
ooparts のうち先頭に連続する o をすべて取り除くと parts となります。
入力例 2
6 abcooo
出力例 2
abcooo
先頭の文字が o でない場合もあります。
入力例 3
5 ooooo
出力例 3
すべての文字が o である場合もあります。
Score : 100 points
Problem Statement
You are given a string S of length N.
Output the string obtained by removing all leading consecutive os from S.
If all characters in S are o, output an empty string.
Constraints
- N is an integer satisfying 1 \le N \le 50.
- S is a string of length N consisting of lowercase English letters.
Input
The input is given from Standard Input in the following format:
N S
Output
Output the answer.
Sample Input 1
7 ooparts
Sample Output 1
parts
Removing all leading consecutive os from ooparts gives parts.
Sample Input 2
6 abcooo
Sample Output 2
abcooo
The first character may not be o.
Sample Input 3
5 ooooo
Sample Output 3
All characters may be o.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
ある測定では、時刻 0,1,\dots,T におけるセンサーの測定値を以下の規則で記録します。
- 時刻 0 では、測定値を保存する。
- 時刻 1,2,\dots,T では、「現時刻の測定値」と「直前に保存された測定値」との差の絶対値が X 以上であるとき、またその時に限り値を保存する。
時刻 i=0,1,\dots,T におけるセンサーの測定値は A_i でした。
測定値が保存された時刻と保存された値とを、時刻の昇順に出力してください。
制約
- 1 \le T \le 100
- 1 \le X \le 100
- 0 \le A_i \le 100
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
T X A_0 A_1 \dots A_T
出力
測定値が k 回保存され、そのうち時刻の昇順に並べた時に i 回目の時刻が t_i 、測定値が a_i であったとき、以下の形式で出力せよ。
t_1 a_1 t_2 a_2 \vdots t_k a_k
入力例 1
6 10 30 35 40 21 30 12 31
出力例 1
0 30 2 40 3 21 6 31
測定は以下の流れで進行します。
- 時刻 0 の測定値は 30 であった。これを保存する。
- 時刻 1 の測定値は 35 であった。最後に保存された測定値は 30 であり、この値との差の絶対値が 10 未満であるため、保存しない。
- 時刻 2 の測定値は 40 であった。最後に保存された測定値は 30 であり、この値との差の絶対値が 10 以上であるため、保存する。
- 時刻 3 の測定値は 21 であった。最後に保存された測定値は 40 であり、この値との差の絶対値が 10 以上であるため、保存する。
- 時刻 4 の測定値は 30 であった。最後に保存された測定値は 21 であり、この値との差の絶対値が 10 未満であるため、保存しない。
- 時刻 5 の測定値は 12 であった。最後に保存された測定値は 21 であり、この値との差の絶対値が 10 未満であるため、保存しない。
- 時刻 6 の測定値は 31 であった。最後に保存された測定値は 21 であり、この値との差の絶対値が 10 以上であるため、保存する。
Score : 200 points
Problem Statement
In a certain measurement, the sensor readings at times 0,1,\dots,T are recorded according to the following rules.
- At time 0, the reading is saved.
- At times 1,2,\dots,T, the reading is saved if and only if the absolute difference between the current reading and the most recently saved reading is at least X.
The sensor reading at time i=0,1,\dots,T was A_i.
Output the times at which readings were saved and the saved values, in ascending order of time.
Constraints
- 1 \le T \le 100
- 1 \le X \le 100
- 0 \le A_i \le 100
- All input values are integers.
Input
The input is given from Standard Input in the following format:
T X A_0 A_1 \dots A_T
Output
If k readings were saved, and the i-th saved reading in ascending order of time was at time t_i with value a_i, output in the following format:
t_1 a_1 t_2 a_2 \vdots t_k a_k
Sample Input 1
6 10 30 35 40 21 30 12 31
Sample Output 1
0 30 2 40 3 21 6 31
The measurement proceeds as follows.
- The reading at time 0 is 30. Save it.
- The reading at time 1 is 35. The most recently saved reading is 30, and the absolute difference is less than 10, so it is not saved.
- The reading at time 2 is 40. The most recently saved reading is 30, and the absolute difference is at least 10, so it is saved.
- The reading at time 3 is 21. The most recently saved reading is 40, and the absolute difference is at least 10, so it is saved.
- The reading at time 4 is 30. The most recently saved reading is 21, and the absolute difference is less than 10, so it is not saved.
- The reading at time 5 is 12. The most recently saved reading is 21, and the absolute difference is less than 10, so it is not saved.
- The reading at time 6 is 31. The most recently saved reading is 21, and the absolute difference is at least 10, so it is saved.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
数直線上の座標 0.5 に高橋君がいます。
高橋君はこれから N 回の移動を行います。
i 回目の移動では、「正の方向」「負の方向」のいずれかを選び、その方向に L_i 進みます。
高橋君は座標 0 を最大で何回通り過ぎることが出来るでしょうか?
なお、この問題の制約上、座標 0 で完了する移動が生じることはありません。
制約
- 1 \le N \le 20
- 1 \le L_i \le 10^9
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N L_1 L_2 \dots L_N
出力
答えを出力せよ。
入力例 1
5 2 5 2 2 1
出力例 1
4
例えば以下のように移動の方向を選択することで座標 0 を 4 回通り過ぎることができ、これが最大です。
- 1 回目の移動で負の方向を選び、 2 進む。高橋君は座標 0.5 から -1.5 へと移動し、座標 0 を通り過ぎる。
- 2 回目の移動で正の方向を選び、 5 進む。高橋君は座標 -1.5 から 3.5 へと移動し、座標 0 を通り過ぎる。
- 3 回目の移動で負の方向を選び、 2 進む。高橋君は座標 3.5 から 1.5 へと移動する。
- 4 回目の移動で負の方向を選び、 2 進む。高橋君は座標 1.5 から -0.5 へと移動し、座標 0 を通り過ぎる。
- 5 回目の移動で正の方向を選び、 1 進む。高橋君は座標 -0.5 から 0.5 へと移動し、座標 0 を通り過ぎる。
入力例 2
5 100 1 2 3 4
出力例 2
1
入力例 3
20 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
出力例 3
20
Score : 300 points
Problem Statement
Takahashi is at coordinate 0.5 on a number line.
He will make N moves.
In the i-th move, he chooses either the positive direction or the negative direction, and moves L_i in that direction.
What is the maximum number of times he can pass through coordinate 0?
Under the constraints of this problem, no move will end at coordinate 0.
Constraints
- 1 \le N \le 20
- 1 \le L_i \le 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N L_1 L_2 \dots L_N
Output
Output the answer.
Sample Input 1
5 2 5 2 2 1
Sample Output 1
4
For example, by choosing the directions of movement as follows, he can pass through coordinate 0 four times, which is the maximum.
- In the first move, choose the negative direction and move 2. He moves from coordinate 0.5 to -1.5, passing through coordinate 0.
- In the second move, choose the positive direction and move 5. He moves from coordinate -1.5 to 3.5, passing through coordinate 0.
- In the third move, choose the negative direction and move 2. He moves from coordinate 3.5 to 1.5.
- In the fourth move, choose the negative direction and move 2. He moves from coordinate 1.5 to -0.5, passing through coordinate 0.
- In the fifth move, choose the positive direction and move 1. He moves from coordinate -0.5 to 0.5, passing through coordinate 0.
Sample Input 2
5 100 1 2 3 4
Sample Output 2
1
Sample Input 3
20 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Sample Output 3
20
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 425 点
問題文
縦 H マス \times 横 W マスのマス目があり、高橋君はこのマス目を上下左右に移動します。
上から i 番目かつ左から j 番目 (1\leq i\leq H, 1\leq j\leq W) のマスの状態は文字 S_{i,j} で表されます。
S_{i,j} は #, ., o, x, S, G のいずれかです。
- S_{i,j}=
#のとき:このマスに立ち入ることはできない。 - S_{i,j}=
.のとき:このマスに自由に出入りすることができる。すなわち、このマスに立ち入った後、上下左右に隣接する好きなマスに(存在するならば)移動できる。 - S_{i,j}=
oのとき:このマスでは 直前の移動と同じ方向に移動しなければならない。すなわち、このマスに立ち入った後は、方向を変えずに次のマスへ移動しなければならない。 - S_{i,j}=
xのとき:このマスでは 直前の移動と同じ方向に移動することはできない。すなわち、このマスに立ち入った後は、必ず方向を変えて次のマスへ移動しなければならない。なお、180 度方向を変えて元のマスに戻ることは方向を変えて移動することに含まれる。 - S_{i,j}=
Sのとき:このマスは高橋君が最初にいる地点である。このマスは自由に出入りすることができる。 - S_{i,j}=
Gのとき:このマスは高橋君の目的地である。このマスは自由に出入りすることができる。
なお、S_{i,j}=S, G であるような (i,j) は 1\leq i\leq H, 1\leq j\leq W の範囲にちょうど 1 つずつ存在します。
高橋君は最初にいる地点から、上下左右に隣接するマスへの移動を繰り返して、目的地へと到達したいです。
そのような事が可能か判定し、可能ならば条件をみたす移動方法であって、隣接するマスへの移動回数が 5\times 10^6 以下であるようなものを一つ出力してください。
なお、問題の条件下において条件をみたす移動が可能ならば、そのような方法であって移動回数が 5\times 10^6 以下であるようなものが存在することが証明できます。
また、移動回数が 5\times 10^6 以下である限り、回数を最小化する必要はありません。
制約
- 1 \leq H,W\leq 1000
- S_{i,j} は
#,.,o,x,S,Gのいずれかである。 - S_{i,j}=
S,Gとなる (i,j) がちょうど一つずつ存在する。 - H,W は整数
入力
入力は以下の形式で標準入力から与えられる。
H W
S_{1,1} S_{1,2} \ldots S_{1,W}
S_{2,1} S_{2,2} \ldots S_{2,W}
\vdots
S_{H,1} S_{H,2} \ldots S_{H,W}
出力
条件をみたすように移動することが不可能ならば、1 行目に No を出力し、2 行目には何も出力してはならない。
条件をみたすように移動することが可能ならば、1 行目に Yes を出力せよ。
2 行目には移動方法を表す文字列 T を出力せよ。T は次の条件をすべてみたす必要がある。
- T の長さ \lvert T\rvert は 1 以上 5\times 10^6 以下である。
- T の各文字は
U,D,L,Rのいずれかである。- T の i 文字目が
U・D・L・Rであることはそれぞれ、高橋君が出発してから i 回目の移動で上・下・左・右に隣接するマスに移動することを表す。
- T の i 文字目が
- 1\leq i\leq \lvert T\rvert について、i 回目の移動の後で高橋君がマス目の外に出ることはない。
- 移動の途中で各マスの条件に違反しない。
- \lvert T\rvert 回目の移動の後で、高橋君は目的地のマスにいる。なお、この条件を満たしている限り、\lvert T\rvert 回目の移動より前に目的地のマスを通り過ぎていても良い。
入力例 1
3 5 .#... .Sooo ..x.G
出力例 1
Yes DRUUDDRR
上から i 番目かつ左から j 番目のマスをマス (i,j) で表します。
出力例にしたがって進むと、マス (2,2) \to マス (3,2) \to マス (3,3) \to マス (2,3) \to マス (1,3) \to マス (2,3) \to マス (3,3) \to マス (3,4) \to マス (3,5) のように移動し、目的地に到着します。
他に、DRUURLRRDD なども正解となります。
一方で、マス (3,3) を直進することはできないため、DRRR などは正解になりません。
入力例 2
3 3 #So xoG ..#
出力例 2
Yes DDLURR
入力例 3
2 2 So oG
出力例 3
No
条件をみたすように移動することはできません。
Score : 425 points
Problem Statement
There is a grid of H rows \times W columns, and Takahashi moves through this grid up, down, left, and right.
The state of the cell at the i-th row from the top and j-th column from the left (1\leq i\leq H, 1\leq j\leq W) is represented by the character S_{i,j}.
S_{i,j} is one of #, ., o, x, S, G.
- If S_{i,j}=
#: This cell cannot be entered. - If S_{i,j}=
.: This cell can be freely entered and exited. That is, after entering this cell, Takahashi can move to any adjacent cell (if it exists) in the up, down, left, or right direction. - If S_{i,j}=
o: In this cell, Takahashi must move in the same direction as the immediately preceding move. That is, after entering this cell, he must move to the next cell without changing direction. - If S_{i,j}=
x: In this cell, Takahashi cannot move in the same direction as the immediately preceding move. That is, after entering this cell, he must change direction to move to the next cell. Turning 180 degrees to return to the previous cell is considered as changing direction. - If S_{i,j}=
S: This cell is Takahashi's starting position. This cell can be freely entered and exited. - If S_{i,j}=
G: This cell is Takahashi's destination. This cell can be freely entered and exited.
There is exactly one (i,j) with 1\leq i\leq H, 1\leq j\leq W satisfying S_{i,j}=S, and exactly one satisfying S_{i,j}=G.
Takahashi wants to reach the destination by repeatedly moving to adjacent cells up, down, left, or right from his starting position.
Determine whether this is possible, and if so, output one valid sequence of moves with at most 5\times 10^6 moves between adjacent cells.
It can be proved that if a valid sequence of moves exists under the problem's conditions, then there exists one with at most 5\times 10^6 moves.
As long as the number of moves is at most 5\times 10^6, it is not necessary to minimize the number of moves.
Constraints
- 1 \leq H,W\leq 1000
- S_{i,j} is one of
#,.,o,x,S,G. - There is exactly one (i,j) satisfying S_{i,j}=
Sand exactly one satisfying S_{i,j}=G. - H and W are integers.
Input
The input is given from Standard Input in the following format:
H W
S_{1,1} S_{1,2} \ldots S_{1,W}
S_{2,1} S_{2,2} \ldots S_{2,W}
\vdots
S_{H,1} S_{H,2} \ldots S_{H,W}
Output
If it is impossible to reach the destination while satisfying the conditions, output No on the first line, and nothing on the second line.
If it is possible to reach the destination while satisfying the conditions, output Yes on the first line.
On the second line, output a string T representing a sequence of moves. T must satisfy all of the following conditions.
- The length \lvert T\rvert of T is between 1 and 5\times 10^6, inclusive.
- Each character of T is one of
U,D,L,R.- The i-th character of T being
U,D,L,Rmeans that in the i-th move after departing, Takahashi moves to the adjacent cell above, below, to the left, or to the right, respectively.
- The i-th character of T being
- For 1\leq i\leq \lvert T\rvert, Takahashi is not outside the grid after the i-th move.
- During the moves, the conditions of each cell are not violated.
- After the \lvert T\rvert-th move, Takahashi is at the destination cell. As long as this condition is satisfied, he may pass through the destination cell before the \lvert T\rvert-th move.
Sample Input 1
3 5 .#... .Sooo ..x.G
Sample Output 1
Yes DRUUDDRR
Let cell (i,j) denote the cell at the i-th row from the top and j-th column from the left.
Following the sample output, the path goes: cell (2,2) \to cell (3,2) \to cell (3,3) \to cell (2,3) \to cell (1,3) \to cell (2,3) \to cell (3,3) \to cell (3,4) \to cell (3,5), reaching the destination.
Other solutions such as DRUURLRRDD are also accepted.
On the other hand, since Takahashi cannot go straight through cell (3,3), solutions such as DRRR are not accepted.
Sample Input 2
3 3 #So xoG ..#
Sample Output 2
Yes DDLURR
Sample Input 3
2 2 So oG
Sample Output 3
No
It is impossible to reach the destination while satisfying the conditions.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 475 点
問題文
選手 1、選手 2、\ldots、選手 N の N 人を、次の条件をすべてみたすように 2 つの(区別できる)チーム A,B に分けます。
- それぞれのチームは 1 人以上の選手からなる。
- それぞれの選手はチーム A,B のちょうど一方に属する。
- 選手 i が所属するチームに属する人数は L_i 人以上 R_i 人以下である。
条件をみたすような分け方は何通りあるか求め、その値を 998244353 で割ったあまりを出力してください。
ただし、2 つの分け方が異なるとは、ある選手が存在してその選手の属するチームが異なることをさします。
制約
- 2\leq N\leq 2\times 10^5
- 1\leq L_i\leq R_i\leq N-1
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N L_1 R_1 L_2 R_2 \vdots L_N R_N
出力
条件をみたす分け方の個数を 998244353 で割ったあまりを出力せよ。
入力例 1
3 1 1 1 2 2 2
出力例 1
2
次の 2 通りの分け方が条件をみたします。
- 選手 1: チーム A, 選手 2: チーム B, 選手 3: チーム B
- 選手 1: チーム B, 選手 2: チーム A, 選手 3: チーム A
2 を 998244353 で割ったあまりは 2 であるため、2 を出力します。
入力例 2
6 1 5 1 5 2 5 1 3 3 5 2 5
出力例 2
30
条件をみたす分け方は 30 通りあります。
Score : 475 points
Problem Statement
Divide N players, player 1, player 2, \ldots, player N, into two (distinguishable) teams A and B satisfying all of the following conditions.
- Each team consists of at least one player.
- Each player belongs to either team A or team B, but not both.
- The number of players in the team that player i belongs to is at least L_i and at most R_i.
Find the number of ways to divide the players satisfying the conditions, and output the count modulo 998244353.
Two divisions are considered different if there exists a player who belongs to different teams in the two divisions.
Constraints
- 2\leq N\leq 2\times 10^5
- 1\leq L_i\leq R_i\leq N-1
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N L_1 R_1 L_2 R_2 \vdots L_N R_N
Output
Output the number of valid divisions, modulo 998244353.
Sample Input 1
3 1 1 1 2 2 2
Sample Output 1
2
The following two divisions satisfy the conditions.
- Player 1: Team A, Player 2: Team B, Player 3: Team B
- Player 1: Team B, Player 2: Team A, Player 3: Team A
Since 2 modulo 998244353 is 2, output 2.
Sample Input 2
6 1 5 1 5 2 5 1 3 3 5 2 5
Sample Output 2
30
There are 30 valid divisions.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 550 点
問題文
N 頂点の木が与えられます。
頂点は頂点 1、頂点 2、\ldots、頂点 N と番号づけられており、i 番目 (1\leq i\leq N-1) の辺は頂点 U_i と頂点 V_i を結んでいます。
高橋君は各頂点を色 1、色 2、\ldots、色 K のうちいずれか一色で塗ります。
色 i は最大で C_i 個の頂点を塗るのに使うことができます。
次の条件をみたすように頂点を塗ることが可能か判定し、可能な場合は条件をみたす塗り方を 1 つ出力してください。
- どの辺についても、ある i (1\leq i\leq K) が存在して、その辺で木を切って得られる 2 つの部分木の両方に色 i で塗られた頂点が存在する。
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
制約
- 1\leq T\leq 10^5
- 2\leq N\leq 3\times 10^5
- 1\leq K\leq N
- 1\leq U_i,V_i\leq N
- 与えられるグラフは木である。
- 1\leq C_i\leq N
- C_1+C_2+\cdots+C_K\geq N
- 入力はすべて整数
- 各入力のテストケースにわたる N の総和は 3\times 10^5 を超えない。
入力
入力は以下の形式で標準入力から与えられる。
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
\mathrm{case}_i は i 個目のテストケースを表す。
各テストケースは以下の形式で与えられる。
N K
U_1 V_1
U_2 V_2
\vdots
U_{N-1} V_{N-1}
C_1 C_2 \ldots C_K
出力
T 行出力せよ。
i 行目 (1\leq i\leq T) には、i 個目のテストケースに対する答えを以下のように出力せよ。
条件をみたすように頂点を塗ることが不可能ならば、-1 を 1 つだけ出力せよ。
そうでないならば、N 個の整数 X_1,X_2,\ldots,X_N (1\leq X_i\leq K) を空白区切りで出力せよ。 ここで、j=1,2,\ldots,N について頂点 i を色 X_i で塗った方法が条件をみたすようにせよ。
入力例 1
2 5 3 1 2 1 3 2 4 2 5 2 2 2 3 3 1 2 2 3 1 1 1
出力例 1
3 2 2 1 1 -1
1 個目のテストケースについて、出力例のように頂点を塗ったとします。
すなわち、頂点 1 に色 3 を、頂点 2 に色 2 を、頂点 3 に色 2 を、頂点 4 に色 1 を、頂点 5 に色 1 を塗ったとします。
このとき、頂点 1 と頂点 2 を結ぶ辺を切ったとすると、木は頂点 1,3 からなる部分木と頂点 2,4,5 からなる部分木に分かれますが、このときどちらにも色 2 で塗られた頂点(頂点 3 または頂点 2)が存在します。
与えられた木を他のどの辺で切ってもそのような色が存在するため、出力例の塗り方は条件をみたしています。
2 個目のテストケースについて、条件をみたすように頂点を塗る方法は存在しません。
Score : 550 points
Problem Statement
You are given a tree with N vertices.
The vertices are numbered vertex 1, vertex 2, \ldots, vertex N, and the i-th edge (1\leq i\leq N-1) connects vertices U_i and V_i.
Takahashi will color each vertex with one of colors 1, 2, \ldots, K.
Color i can be used to color at most C_i vertices.
Determine whether it is possible to color the vertices satisfying the following condition, and if so, output one valid coloring.
- For every edge, there exists some i (1\leq i\leq K) such that both of the two subtrees obtained by cutting the tree at that edge contain at least one vertex colored with color i.
You are given T test cases; solve each of them.
Constraints
- 1\leq T\leq 10^5
- 2\leq N\leq 3\times 10^5
- 1\leq K\leq N
- 1\leq U_i,V_i\leq N
- The given graph is a tree.
- 1\leq C_i\leq N
- C_1+C_2+\cdots+C_K\geq N
- All input values are integers.
- The sum of N over all test cases does not exceed 3\times 10^5.
Input
The input is given from Standard Input in the following format:
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
\mathrm{case}_i represents the i-th test case. Each test case is given in the following format:
N K
U_1 V_1
U_2 V_2
\vdots
U_{N-1} V_{N-1}
C_1 C_2 \ldots C_K
Output
Output T lines.
On the i-th line (1\leq i\leq T), output the answer for the i-th test case as follows.
If it is impossible to color the vertices satisfying the condition, output -1 alone.
Otherwise, output N integers X_1,X_2,\ldots,X_N (1\leq X_i\leq K) separated by spaces, such that coloring vertex i with color X_i for i=1,2,\ldots,N satisfies the condition.
Sample Input 1
2 5 3 1 2 1 3 2 4 2 5 2 2 2 3 3 1 2 2 3 1 1 1
Sample Output 1
3 2 2 1 1 -1
For the first test case, suppose the vertices are colored as in the sample output.
That is, vertex 1 is colored with color 3, vertex 2 with color 2, vertex 3 with color 2, vertex 4 with color 1, and vertex 5 with color 1.
If we cut the edge connecting vertices 1 and 2, the tree splits into the subtree consisting of vertices 1,3 and the subtree consisting of vertices 2,4,5; both subtrees contain a vertex colored with color 2 (vertex 3 or vertex 2, respectively).
Such a color exists no matter which edge of the given tree is cut, so the coloring in the sample output satisfies the condition.
For the second test case, there is no valid coloring satisfying the condition.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 600 点
問題文
N 個の長さ M の数列 A_1,A_2,\dots,A_N があります。最初は全ての数列の全ての要素が 0 です。
以降、数列 A_i の j 項目を A_{i,j} と表します。
以下の 3 種類のクエリを、与えられた順に合計 Q 個処理してください。
- タイプ 1 : 数列 A_{X_i} を数列 A_{Y_i} で上書きする。つまり、全ての整数 j (1 \le j \le M) について、 A_{X_i,j} を A_{Y_i,j} に変更する。
- タイプ 2 : 数列 A_{X_i} の Y_i 項目、すなわち A_{X_i,Y_i} を Z_i に変更する。
- タイプ 3 : 数列 A_{X_i} について、 L_i 項目から R_i 項目までの和、すなわち A_{X_i,L_i}+A_{X_i,L_i+1}+\dots+A_{X_i,R_i} を出力する。
制約
- 1 \le N,M \le 2 \times 10^5
- 1 \le Q \le 2 \times 10^5
- タイプ 1 のクエリは以下の制約を満たす
- 1 \le X_i,Y_i \le N
- タイプ 2 のクエリは以下の制約を満たす
- 1 \le X_i \le N
- 1 \le Y_i \le M
- 0 \le Z_i \le 10^9
- タイプ 3 のクエリは以下の制約を満たす
- 1 \le X_i \le N
- 1 \le L_i \le R_i \le M
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M Q
{\rm Query}_1
{\rm Query}_2
\vdots
{\rm Query}_Q
ただし、 {\rm Query}_i は i 個目のクエリを表す。
タイプ 1 のクエリは以下の形式で与えられる。
1 X_i Y_i
タイプ 2 のクエリは以下の形式で与えられる。
2 X_i Y_i Z_i
タイプ 3 のクエリは以下の形式で与えられる。
3 X_i L_i R_i
出力
タイプ 3 のクエリが与えられるごとに、そのクエリに対する答えを出力せよ。
タイプ 3 のクエリが存在しない場合、出力は空とせよ。
入力例 1
4 5 10 2 2 1 2 2 2 2 7 2 2 4 8 1 1 2 2 2 3 1 1 3 2 2 3 2 10 3 1 2 4 3 2 1 4 3 3 2 2
出力例 1
15 18 10
この入力では、 4 個の長さ 5 の数列を用意します。
- 1 個目のクエリで、 A_{2,1}=2 とする。この時点で数列 A_2=(2,0,0,0,0) である。
- 2 個目のクエリで、 A_{2,2}=7 とする。この時点で数列 A_2=(2,7,0,0,0) である。
- 3 個目のクエリで、 A_{2,4}=8 とする。この時点で数列 A_2=(2,7,0,8,0) である。
- 4 個目のクエリで、数列 A_1 を数列 A_2 で上書きする。この時点で数列 A_1=(2,7,0,8,0) である。
- 5 個目のクエリで、 A_{2,3}=1 とする。この時点で数列 A_2=(2,7,1,8,0) である。
- 6 個目のクエリで、数列 A_3 を数列 A_2 で上書きする。この時点で数列 A_3=(2,7,1,8,0) である。
- 7 個目のクエリで、 A_{3,2}=10 とする。この時点で数列 A_3=(2,10,1,8,0) である。
- 8 個目のクエリで、 A_{1,2}+A_{1,3}+A_{1,4}=7+0+8=15 を答える。
- 9 個目のクエリで、 A_{2,1}+A_{2,2}+A_{2,3}+A_{2,4}=2+7+1+8=18 を答える。
- 10 個目のクエリで、 A_{3,2}=10 を答える。
Score : 600 points
Problem Statement
There are N sequences of length M: A_1,A_2,\dots,A_N. Initially, all elements of all sequences are 0.
Hereafter, the j-th element of sequence A_i is denoted by A_{i,j}.
Process the following three types of queries in the given order, Q queries in total.
- Type 1: Overwrite sequence A_{X_i} with sequence A_{Y_i}. That is, for every integer j (1 \le j \le M), change A_{X_i,j} to A_{Y_i,j}.
- Type 2: Change the Y_i-th element of sequence A_{X_i}, that is, A_{X_i,Y_i}, to Z_i.
- Type 3: For sequence A_{X_i}, output the sum of elements from the L_i-th through the R_i-th, that is, A_{X_i,L_i}+A_{X_i,L_i+1}+\dots+A_{X_i,R_i}.
Constraints
- 1 \le N,M \le 2 \times 10^5
- 1 \le Q \le 2 \times 10^5
- Type 1 queries satisfy the following constraints:
- 1 \le X_i,Y_i \le N
- Type 2 queries satisfy the following constraints:
- 1 \le X_i \le N
- 1 \le Y_i \le M
- 0 \le Z_i \le 10^9
- Type 3 queries satisfy the following constraints:
- 1 \le X_i \le N
- 1 \le L_i \le R_i \le M
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M Q
{\rm Query}_1
{\rm Query}_2
\vdots
{\rm Query}_Q
Here, {\rm Query}_i represents the i-th query.
A type 1 query is given in the following format:
1 X_i Y_i
A type 2 query is given in the following format:
2 X_i Y_i Z_i
A type 3 query is given in the following format:
3 X_i L_i R_i
Output
For each type 3 query, output the answer to that query. If there are no type 3 queries, the output should be empty.
Sample Input 1
4 5 10 2 2 1 2 2 2 2 7 2 2 4 8 1 1 2 2 2 3 1 1 3 2 2 3 2 10 3 1 2 4 3 2 1 4 3 3 2 2
Sample Output 1
15 18 10
In this input, prepare four sequences of length 5.
- In the 1st query, set A_{2,1}=2. At this point, A_2=(2,0,0,0,0).
- In the 2nd query, set A_{2,2}=7. At this point, A_2=(2,7,0,0,0).
- In the 3rd query, set A_{2,4}=8. At this point, A_2=(2,7,0,8,0).
- In the 4th query, overwrite sequence A_1 with sequence A_2. At this point, A_1=(2,7,0,8,0).
- In the 5th query, set A_{2,3}=1. At this point, A_2=(2,7,1,8,0).
- In the 6th query, overwrite sequence A_3 with sequence A_2. At this point, A_3=(2,7,1,8,0).
- In the 7th query, set A_{3,2}=10. At this point, A_3=(2,10,1,8,0).
- In the 8th query, output A_{1,2}+A_{1,3}+A_{1,4}=7+0+8=15.
- In the 9th query, output A_{2,1}+A_{2,2}+A_{2,3}+A_{2,4}=2+7+1+8=18.
- In the 10th query, output A_{3,2}=10.