Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
長さ N の整数列 A=(A_1,A_2,\ldots,A_N) が与えられます。
A の中に同じ要素が 3 つ以上連続する箇所が存在するか判定してください。
より厳密には、 1 以上 N-2 以下の整数 i であって A_i=A_{i+1}=A_{i+2} を満たすものが存在するか判定してください。
制約
- 3\le N\le 100
- 1\le A_i\le 100
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \ldots A_N
出力
A の中に同じ要素が 3 つ以上連続する箇所が存在するならば Yes を、存在しないならば No を出力せよ。
入力例 1
5 1 4 4 4 2
出力例 1
Yes
A=(1,4,4,4,2) です。 4 が 3 つ連続している箇所が存在するので、 Yes を出力してください。
入力例 2
6 2 4 4 2 2 4
出力例 2
No
A=(2,4,4,2,2,4) です。同じ要素が 3 つ以上連続している箇所は存在しないので、 No を出力してください。
入力例 3
8 1 4 2 5 7 7 7 2
出力例 3
Yes
入力例 4
10 1 2 3 4 5 6 7 8 9 10
出力例 4
No
入力例 5
13 1 1 1 1 1 1 1 1 1 1 1 1 1
出力例 5
Yes
Score : 100 points
Problem Statement
You are given an integer sequence of length N: A = (A_1,A_2,\ldots,A_N).
Determine whether there is a place in A where the same element appears three or more times in a row.
More formally, determine whether there exists an integer i with 1 \le i \le N-2 such that A_i = A_{i+1} = A_{i+2}.
Constraints
- 3 \le N \le 100
- 1 \le A_i \le 100
- 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
If there is a place in A where the same element appears three or more times in a row, print Yes. Otherwise, print No.
Sample Input 1
5 1 4 4 4 2
Sample Output 1
Yes
We have A=(1,4,4,4,2). There is a place where 4 appears three times in a row, so print Yes.
Sample Input 2
6 2 4 4 2 2 4
Sample Output 2
No
We have A=(2,4,4,2,2,4). There is no place where the same element appears three or more times in a row, so print No.
Sample Input 3
8 1 4 2 5 7 7 7 2
Sample Output 3
Yes
Sample Input 4
10 1 2 3 4 5 6 7 8 9 10
Sample Output 4
No
Sample Input 5
13 1 1 1 1 1 1 1 1 1 1 1 1 1
Sample Output 5
Yes
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
長さ N の数列 A = (A_1, A_2, \cdots , A_N) が与えられます。
その後に 1 以上 N 以下の整数 X が与えられます。
A_X の値を出力してください。
制約
- 1 \le X \le N \le 100
- 1 \le A_i \le 100
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \ldots A_N X
出力
A_X の値を出力せよ。
入力例 1
5 1 2 3 4 5 3
出力例 1
3
(A_1, A_2, A_3, A_4 , A_5) = (1, 2, 3, 4, 5) です。 A_3 = 3 のため 3 と出力してください。
入力例 2
10 6 6 9 6 10 5 7 2 8 2 4
出力例 2
6
入力例 3
10 4 4 4 3 4 2 1 1 2 1 10
出力例 3
1
Score : 100 points
Problem Statement
You are given a sequence A = (A_1, A_2, \cdots , A_N) of length N.
After that, an integer X between 1 and N, inclusive, is given.
Output the value of A_X.
Constraints
- 1 \le X \le N \le 100
- 1 \le A_i \le 100
- 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 X
Output
Output the value of A_X.
Sample Input 1
5 1 2 3 4 5 3
Sample Output 1
3
We have (A_1, A_2, A_3, A_4 , A_5) = (1, 2, 3, 4, 5). Since A_3 = 3, output 3.
Sample Input 2
10 6 6 9 6 10 5 7 2 8 2 4
Sample Output 2
6
Sample Input 3
10 4 4 4 3 4 2 1 1 2 1 10
Sample Output 3
1
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
N 人の人がおり、i 人目 (1\leq i\leq N) の現在の髪の長さは L_i です。
すべての人は 1 日経つごとに髪の長さが 1 ずつ増えます。
髪の長さが T 以上の人が初めて P 人以上になるのは現在から何日後か出力してください。
ただし、現在の時点ですでに髪の長さが T 以上の人が P 人以上にいる場合は 0 を出力してください。
制約
- 1\leq N\leq 100
- 1\leq L_i\leq 100
- 1\leq T\leq 100
- 1\leq P\leq N
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N T P L_1 L_2 \ldots L_N
出力
髪の長さが T 以上の人が初めて P 人以上になるのは現在から何日後か出力せよ。 ただし、現在の時点ですでに条件をみたしている場合は 0 を出力せよ。
入力例 1
5 10 3 3 11 1 6 2
出力例 1
7
5 人の人がおり、現在の時点で髪の長さはそれぞれ 3,11,1,6,2 であるため、髪の長さが 10 以上の人は 1 人です。
現在から 7 日後にはそれぞれの人の髪の長さは順に 10,18,8,13,9 となり、髪の長さが 10 以上の人は 3 人となります。
現在から 6 日後の時点では髪の長さが 10 以上の人は 2 人であるため条件をみたしておらず、よって 7 を出力します。
入力例 2
2 5 2 10 10
出力例 2
0
現在の時点ですでに髪の長さが 5 以上の人が 2 人いるため条件をみたしており、0 を出力します。
入力例 3
3 10 1 1 2 3
出力例 3
7
Score : 200 points
Problem Statement
There are N people, and the current hair length of the i-th person (1 \leq i \leq N) is L_i.
Each person's hair grows by 1 per day.
Print the number of days after which the number of people whose hair length is at least T becomes P or more for the first time.
If there are already P or more people whose hair length is at least T now, print 0.
Constraints
- 1 \leq N \leq 100
- 1 \leq L_i \leq 100
- 1 \leq T \leq 100
- 1 \leq P \leq N
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N T P L_1 L_2 \ldots L_N
Output
Print the number of days after which the number of people whose hair length is at least T becomes P or more for the first time. If this condition is already satisfied now, print 0.
Sample Input 1
5 10 3 3 11 1 6 2
Sample Output 1
7
There are five people, and their current hair lengths are 3, 11, 1, 6, 2, so there is one person whose hair length is at least 10.
After seven days, the hair lengths of the people will be 10, 18, 8, 13, 9, respectively, and there will be three people whose hair length is at least 10.
After six days, there are only two people whose hair length is at least 10, not satisfying the condition, so print 7.
Sample Input 2
2 5 2 10 10
Sample Output 2
0
Since there are already two people whose hair length is at least 5 now, satisfying the condition, so print 0.
Sample Input 3
3 10 1 1 2 3
Sample Output 3
7
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 150 点
問題文
整数 N が与えられます。
非負整数の組 (x,y,z) であって x+y+z\leq N を満たすものを辞書順で小さい方から順に全て出力してください。
非負整数の組の辞書順とは?
非負整数の組 (x,y,z) が (x',y',z') より辞書順で小さいとは、下記のいずれかが成り立つことを言います。
- x < x' である
- x=x' かつ y< y' である
- x=x' かつ y=y' かつ z< z' である
制約
- 0 \leq N \leq 21
- N は整数である
入力
入力は以下の形式で標準入力から与えられる。
N
出力
非負整数の組 (x,y,z) であって x+y+z\leq N を満たすものを、1 行に 1 組ずつ x,y,z を空白区切りで、辞書順で小さい方から順に全て出力せよ。
入力例 1
3
出力例 1
0 0 0 0 0 1 0 0 2 0 0 3 0 1 0 0 1 1 0 1 2 0 2 0 0 2 1 0 3 0 1 0 0 1 0 1 1 0 2 1 1 0 1 1 1 1 2 0 2 0 0 2 0 1 2 1 0 3 0 0
入力例 2
4
出力例 2
0 0 0 0 0 1 0 0 2 0 0 3 0 0 4 0 1 0 0 1 1 0 1 2 0 1 3 0 2 0 0 2 1 0 2 2 0 3 0 0 3 1 0 4 0 1 0 0 1 0 1 1 0 2 1 0 3 1 1 0 1 1 1 1 1 2 1 2 0 1 2 1 1 3 0 2 0 0 2 0 1 2 0 2 2 1 0 2 1 1 2 2 0 3 0 0 3 0 1 3 1 0 4 0 0
Score : 150 points
Problem Statement
You are given an integer N.
Print all triples of non-negative integers (x,y,z) such that x+y+z\leq N in ascending lexicographical order.
What is lexicographical order for non-negative integer triples?
A triple of non-negative integers (x,y,z) is said to be lexicographically smaller than (x',y',z') if and only if one of the following holds:
- x < x';
- x=x' and y< y';
- x=x' and y=y' and z< z'.
Constraints
- 0 \leq N \leq 21
- N is an integer.
Input
The input is given from Standard Input in the following format:
N
Output
Print all triples of non-negative integers (x,y,z) such that x+y+z\leq N in ascending lexicographical order, with x,y,z separated by spaces, one triple per line.
Sample Input 1
3
Sample Output 1
0 0 0 0 0 1 0 0 2 0 0 3 0 1 0 0 1 1 0 1 2 0 2 0 0 2 1 0 3 0 1 0 0 1 0 1 1 0 2 1 1 0 1 1 1 1 2 0 2 0 0 2 0 1 2 1 0 3 0 0
Sample Input 2
4
Sample Output 2
0 0 0 0 0 1 0 0 2 0 0 3 0 0 4 0 1 0 0 1 1 0 1 2 0 1 3 0 2 0 0 2 1 0 2 2 0 3 0 0 3 1 0 4 0 1 0 0 1 0 1 1 0 2 1 0 3 1 1 0 1 1 1 1 1 2 1 2 0 1 2 1 1 3 0 2 0 0 2 0 1 2 0 2 2 1 0 2 1 1 2 2 0 3 0 0 3 0 1 3 1 0 4 0 0
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
N + 1 個の部屋が一列に並んでおり、順に 0, 1, \ldots, N の番号が付けられています。
部屋の間には N 個のドアがあり、1, 2, \ldots, N の番号が付けられています。ドア i は部屋 i - 1 と部屋 i の間にあります。
各ドアについて鍵の状態を表す値 L_i が与えられ、L_i = 0 のときドア i の鍵は開いており、L_i = 1 のときドア i の鍵は閉まっています。
高橋君ははじめ部屋 R におり、ドア i の鍵が開いているときに限り、部屋 i - 1 と部屋 i の間を移動することができます。また、高橋君は部屋 i - 1 または部屋 i にいるときに限り、ドア i の鍵に対して 開閉操作 を行うことができます。ドア i の鍵に対して開閉操作を行ったとき、その鍵が開いているときは閉まり、閉まっているときは開きます。
すべてのドアの鍵が閉まった状態にするために行う鍵の開閉操作の回数として考えられる最小値を求めてください。
制約
- 2 \leq N \leq 2 \times 10^5
- 0 \leq R \leq N
- L_i \in \lbrace 0, 1 \rbrace
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N R L_1 L_2 \ldots L_N
出力
答えを出力せよ。
入力例 1
6 3 1 0 0 1 0 0
出力例 1
6
高橋君は以下のように行動することで 6 回の開閉操作ですべてのドアの鍵が閉まった状態にすることができます。
- 部屋 2 に移動する。
- ドア 2 に対して開閉操作を行い、ドア 2 の鍵を閉める。
- 部屋 3 に移動する。
- ドア 4 に対して開閉操作を行い、ドア 4 の鍵を開ける。
- ドア 3 に対して開閉操作を行い、ドア 3 の鍵を閉める。
- 部屋 4 に移動する。
- ドア 4 に対して開閉操作を行い、ドア 4 の鍵を閉める。
- 部屋 5 に移動する。
- ドア 5 に対して開閉操作を行い、ドア 5 の鍵を閉める。
- ドア 6 に対して開閉操作を行い、ドア 6 の鍵を閉める。
入力例 2
2 1 0 0
出力例 2
2
入力例 3
8 2 0 1 0 0 1 0 1 1
出力例 3
8
Score : 300 points
Problem Statement
There are N + 1 rooms arranged in a line, numbered 0, 1, \ldots, N in order.
Between the rooms, there are N doors numbered 1, 2, \ldots, N. Door i is between rooms i - 1 and i.
For each door, a value L_i representing the lock state is given. When L_i = 0, door i is unlocked, and when L_i = 1, door i is locked.
Takahashi is initially in room R, and can move between rooms i - 1 and i only when door i is unlocked. Also, he can perform a switching operation on door i only when he is in room i - 1 or room i. When a switching operation is performed on door i, if the door is unlocked, it becomes locked, and if it is locked, it becomes unlocked.
Find the minimum number of switching operations needed to make all doors locked.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 0 \leq R \leq N
- L_i \in \lbrace 0, 1 \rbrace
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N R L_1 L_2 \ldots L_N
Output
Output the answer.
Sample Input 1
6 3 1 0 0 1 0 0
Sample Output 1
6
Takahashi can make all doors locked with six switching operations by acting as follows:
- Move to room 2.
- Perform a switching operation on door 2 to lock door 2.
- Move to room 3.
- Perform a switching operation on door 4 to unlock door 4.
- Perform a switching operation on door 3 to lock door 3.
- Move to room 4.
- Perform a switching operation on door 4 to lock door 4.
- Move to room 5.
- Perform a switching operation on door 5 to lock door 5.
- Perform a switching operation on door 6 to lock door 6.
Sample Input 2
2 1 0 0
Sample Output 2
2
Sample Input 3
8 2 0 1 0 0 1 0 1 1
Sample Output 3
8
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
長さ N の正整数列 A=(A_1,A_2,\dots,A_N) が与えられます。
1\leq l\leq r\leq N を満たす整数の組 (l,r) であって、数列 (A_l,A_{l+1},\dots,A_r) が等差数列であるようなものが何通りあるか求めてください。
なお、数列 (x_1,x_2,\dots,x_{|x|}) が等差数列であるとは、ある d が存在して x_{i+1}-x_i=d\ (1\leq i < |x|) であることをいいます。 特に、長さ 1 の数列は常に等差数列です。
制約
- 1\leq N \leq 2\times 10^5
- 1\leq A_i \leq 10^9
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \dots A_N
出力
答えを出力せよ。
入力例 1
4 3 6 9 3
出力例 1
8
条件を満たす整数の組 (l,r) は (1,1),(2,2),(3,3),(4,4),(1,2),(2,3),(3,4),(1,3) の 8 通りです。
実際、(l,r)=(1,3) のとき (A_l,\dots,A_r)=(3,6,9) は等差数列なので条件を満たしますが、 (l,r)=(2,4) のとき (A_l,\dots,A_r)=(6,9,3) は等差数列ではないので条件を満たしません。
入力例 2
5 1 1 1 1 1
出力例 2
15
すべての整数の組 (l,r)\ (1\leq l\leq r\leq 5) が条件を満たします。
入力例 3
8 87 42 64 86 72 58 44 30
出力例 3
22
Score : 300 points
Problem Statement
You are given a sequence of N positive integers A=(A_1,A_2,\dots,A_N).
Find the number of pairs of integers (l,r) satisfying 1\leq l\leq r\leq N such that the subsequence (A_l,A_{l+1},\dots,A_r) forms an arithmetic progression.
A sequence (x_1,x_2,\dots,x_{|x|}) is an arithmetic progression if and only if there exists a d such that x_{i+1}-x_i=d\ (1\leq i < |x|). In particular, a sequence of length 1 is always an arithmetic progression.
Constraints
- 1\leq N \leq 2\times 10^5
- 1\leq A_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 \dots A_N
Output
Print the answer.
Sample Input 1
4 3 6 9 3
Sample Output 1
8
There are eight pairs of integers (l,r) satisfying the condition: (1,1),(2,2),(3,3),(4,4),(1,2),(2,3),(3,4),(1,3).
Indeed, when (l,r)=(1,3), (A_l,\dots,A_r)=(3,6,9) is an arithmetic progression, so it satisfies the condition. However, when (l,r)=(2,4), (A_l,\dots,A_r)=(6,9,3) is not an arithmetic progression, so it does not satisfy the condition.
Sample Input 2
5 1 1 1 1 1
Sample Output 2
15
All pairs of integers (l,r)\ (1\leq l\leq r\leq 5) satisfy the condition.
Sample Input 3
8 87 42 64 86 72 58 44 30
Sample Output 3
22
Time Limit: 3 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
H 行 W 列のグリッドがあります。上から i 行目、左から j 列目にあるマスをマス (i, j) で表します。
N 個のマス (r_1, c_1), (r_2, c_2), \ldots, (r_N, c_N) は壁になっています。
はじめ、高橋君はマス (r_\mathrm{s}, c_\mathrm{s}) にいます。
高橋君に Q 個の指示が与えられます。
i = 1, 2, \ldots, Q について、i 番目の指示は文字 d_i と正整数 l_i の組で表されます。d_i は L 、R 、U 、D のいずれかの文字であり、それぞれ左、右、上、下の方向を表します。
i 番目の指示に対して高橋君は下記の行動を l_i 回繰り返します。
現在いるマスに対して、d_i が表す向きに壁のないマスが隣接しているなら、そのマスに移動する。 そのようなマスが存在しない場合は、何もしない。
i = 1, 2, \ldots, Q について、i 番目までの指示を実行した直後に高橋君がいるマスを出力してください。
制約
- 2 \leq H, W \leq 10^9
- 1 \leq r_\mathrm{s} \leq H
- 1 \leq c_\mathrm{s} \leq W
- 0 \leq N \leq 2 \times 10^5
- 1 \leq r_i \leq H
- 1 \leq c_i \leq W
- i \neq j \Rightarrow (r_i, c_i) \neq (r_j, c_j)
- すべての i = 1, 2, \ldots, Nについて、(r_\mathrm{s}, c_\mathrm{s}) \neq (r_i, c_i)
- 1 \leq Q \leq 2 \times 10^5
- d_i は
L、R、U、Dのいずれかの文字 - 1 \leq l_i \leq 10^9
- d_i 以外の入力値は整数
入力
入力は以下の形式で標準入力から与えられる。
H W r_\mathrm{s} c_\mathrm{s}
N
r_1 c_1
r_2 c_2
\vdots
r_N c_N
Q
d_1 l_1
d_2 l_2
\vdots
d_Q l_Q
出力
Q 行出力せよ。 下記の形式にしたがい、i = 1, 2, \ldots, Q について、i 番目までの指示を実行した直後に高橋君がいるマス (R_i, C_i) を i 行目に出力せよ。
R_1 C_1 R_2 C_2 \vdots R_Q C_Q
入力例 1
5 5 4 4 3 5 3 2 2 1 4 4 L 2 U 3 L 2 R 4
出力例 1
4 2 3 2 3 1 3 5
与えられるグリッドと高橋君の初期位置は下記の通りです。
ここで、# は壁のマスを、T は高橋君がいるマスを表し、. がその他のマスを表します。
...#. .#... ..... ...T. ..#..
1 つ目の指示に対して高橋君は、左に 2 マス移動し、高橋君の位置は下記の通り、マス (4, 2) になります。
...#. .#... ..... .T... ..#..
2 つ目の指示に対して高橋君は、上に 1 マスに移動した後、次の移動先が壁であるために「何もしない」を 2 回行います。その結果、高橋君の位置は下記の通り、マス (3, 2) になります。
...#. .#... .T... ..... ..#..
3 つ目の指示に対して高橋君は、左に 1 マス移動した後、次の移動先となるマスが存在しないために「何もしない」を 1 回行います。その結果、高橋君の位置は下記の通り、マス (3, 1) になります。
...#. .#... T.... ..... ..#..
4 つ目の指示に対して高橋君は、右に 4 マス移動し、高橋君の位置は下記の通り、マス (3, 5) になります。
...#. .#... ....T ..... ..#..
入力例 2
6 6 6 3 7 3 1 4 3 2 6 3 4 5 5 1 1 3 2 10 D 3 U 3 L 2 D 2 U 3 D 3 U 3 R 3 L 3 D 1
出力例 2
6 3 5 3 5 1 6 1 4 1 6 1 4 1 4 2 4 1 5 1
Score : 400 points
Problem Statement
There is a grid with H horizontal rows and W vertical columns. (i, j) denotes the square at the i-th row from the top and j-th column from the left.
N squares, (r_1, c_1), (r_2, c_2), \ldots, (r_N, c_N), have walls.
Takahashi is initially at square (r_\mathrm{s}, c_\mathrm{s}).
Q instructions are given to Takahashi.
For i = 1, 2, \ldots, Q, the i-th instruction is represented by a pair of a character d_i and a positive integer l_i. d_i is one of L, R, U, and D, representing the directions of left, right, up, and down, respectively.
Given the i-th direction, Takahashi repeats the following action l_i times:
If a square without a wall is adjacent to the current square in the direction represented by d_i, move to that square; otherwise, do nothing.
For i = 1, 2, \ldots, Q, print the square where Takahashi will be after he follows the first i instructions.
Constraints
- 2 \leq H, W \leq 10^9
- 1 \leq r_\mathrm{s} \leq H
- 1 \leq c_\mathrm{s} \leq W
- 0 \leq N \leq 2 \times 10^5
- 1 \leq r_i \leq H
- 1 \leq c_i \leq W
- i \neq j \Rightarrow (r_i, c_i) \neq (r_j, c_j)
- (r_\mathrm{s}, c_\mathrm{s}) \neq (r_i, c_i) for all i = 1, 2, \ldots, N.
- 1 \leq Q \leq 2 \times 10^5
- d_i is one of the characters
L,R,U, andD. - 1 \leq l_i \leq 10^9
- All values in the input other than d_i are integers.
Input
The input is given from Standard Input in the following format:
H W r_\mathrm{s} c_\mathrm{s}
N
r_1 c_1
r_2 c_2
\vdots
r_N c_N
Q
d_1 l_1
d_2 l_2
\vdots
d_Q l_Q
Output
Print Q lines. For i = 1, 2, \ldots, Q, the i-th line should contain the square (R_i, C_i) where Takahashi will be after he follows the first i instructions, in the following format:
R_1 C_1 R_2 C_2 \vdots R_Q C_Q
Sample Input 1
5 5 4 4 3 5 3 2 2 1 4 4 L 2 U 3 L 2 R 4
Sample Output 1
4 2 3 2 3 1 3 5
The given grid and the initial position of Takahashi are as follows, where # denotes a square with a wall, T a square where Takahashi is, and . the other squares:
...#. .#... ..... ...T. ..#..
Given the 1-st instruction, Takahashi moves 2 squares to the left, ending up in square (4, 2) as follows:
...#. .#... ..... .T... ..#..
Given the 2-nd instruction, Takahashi first moves 1 square upward, then he "does nothing" twice because the adjacent square in his direction has a wall. As a result, he ends up in square (3, 2) as follows:
...#. .#... .T... ..... ..#..
Given the 3-rd instruction, Takahashi first moves 1 square to the left, then he "does nothing" once because there is no square in his direction. As a result, he ends up in square (3, 1) as follows:
...#. .#... T.... ..... ..#..
Given the 4-th instruction, Takahashi moves 4 squares to the right, ending up in square (3, 5) as follows:
...#. .#... ....T ..... ..#..
Sample Input 2
6 6 6 3 7 3 1 4 3 2 6 3 4 5 5 1 1 3 2 10 D 3 U 3 L 2 D 2 U 3 D 3 U 3 R 3 L 3 D 1
Sample Output 2
6 3 5 3 5 1 6 1 4 1 6 1 4 1 4 2 4 1 5 1
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
変数 X と、X の値を変更する N 種類の操作があります。操作 i は整数の組 (T_i,A_i) で表され、意味は次の通りです。
- T_i=1 のとき、X の値を X\ {\rm and}\ A_i に置き換える。
- T_i=2 のとき、X の値を X\ {\rm or}\ A_i に置き換える。
- T_i=3 のとき、X の値を X\ {\rm xor}\ A_i に置き換える。
変数 X を値 C で初期化した状態から、以下の処理を順に実行してください。
- 操作 1 を行い、操作後の X の値を出力する。
- 続けて、操作 1,2 を順に行い、操作後の X の値を出力する。
- 続けて、操作 1,2,3 を順に行い、操作後の X の値を出力する。
- \vdots
- 続けて、操作 1,2,\ldots,N を順に行い、操作後の X の値を出力する。
{\rm and}, {\rm or}, {\rm xor} とは
非負整数 A, B の {\rm and}, {\rm or}, {\rm xor} は、以下のように定義されます。- A\ {\rm and}\ B を二進表記した際の 2^k (k \geq 0) の位の数は、A, B を二進表記した際の 2^k の位の数のうち両方が 1 であれば 1、そうでなければ 0 である。
- A\ {\rm or}\ B を二進表記した際の 2^k (k \geq 0) の位の数は、A, B を二進表記した際の 2^k の位の数のうち少なくとも一方が 1 であれば 1、そうでなければ 0 である。
- A\ {\rm xor}\ B を二進表記した際の 2^k (k \geq 0) の位の数は、A, B を二進表記した際の 2^k の位の数のうちちょうど一方が 1 であれば 1、そうでなければ 0 である。
制約
- 1 \leq N \leq 2\times 10^5
- 1\leq T_i \leq 3
- 0\leq A_i \lt 2^{30}
- 0\leq C \lt 2^{30}
- 入力に含まれる値は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N C T_1 A_1 T_2 A_2 \vdots T_N A_N
出力
問題文中の指示に従って N 行出力せよ。
入力例 1
3 10 3 3 2 5 1 12
出力例 1
9 15 12
最初、X の値は 10 です。
- 操作 1 を行うと X の値は 9 になります。
- 続けて操作 1 を行うと X の値は 10 になり、さらに操作 2 を行うと 15 になります。
- 続けて操作 1 を行うと X の値は 12 になり、さらに操作 2 を行うと 13 に、さらに続けて操作 3 を行うと 12 になります。
入力例 2
9 12 1 1 2 2 3 3 1 4 2 5 3 6 1 7 2 8 3 9
出力例 2
0 2 1 0 5 3 3 11 2
Score : 500 points
Problem Statement
We have a variable X and N kinds of operations that change the value of X. Operation i is represented as a pair of integers (T_i,A_i), and is the following operation:
- if T_i=1, it replaces the value of X with X\ {\rm and}\ A_i;
- if T_i=2, it replaces the value of X with X\ {\rm or}\ A_i;
- if T_i=3, it replaces the value of X with X\ {\rm xor}\ A_i.
Initialize X with the value of C and execute the following procedures in order:
- Perform Operation 1, and then print the resulting value of X.
- Next, perform Operation 1, 2 in this order, and then print the value of X.
- Next, perform Operation 1, 2, 3 in this order, and then print the value of X.
- \vdots
- Next, perform Operation 1, 2, \ldots, N in this order, and then print the value of X.
What are {\rm and}, {\rm or}, {\rm xor}?
The {\rm and}, {\rm or}, {\rm xor} of non-negative integers A and B are defined as follows:
- When A\ {\rm and}\ B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if both of the digits in that place of A and B are 1, and 0 otherwise.
- When A\ {\rm or}\ B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if at least one of the digits in that place of A and B is 1, and 0 otherwise.
- When A\ {\rm xor}\ B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if exactly one of the digits in that place of A and B is 1, and 0 otherwise.
Constraints
- 1 \leq N \leq 2\times 10^5
- 1\leq T_i \leq 3
- 0\leq A_i \lt 2^{30}
- 0\leq C \lt 2^{30}
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N C T_1 A_1 T_2 A_2 \vdots T_N A_N
Output
Print N lines, as specified in the Problem Statement.
Sample Input 1
3 10 3 3 2 5 1 12
Sample Output 1
9 15 12
The initial value of X is 10.
- Operation 1 changes X to 9.
- Next, Operation 1 changes X to 10, and then Operation 2 changes it to 15.
- Next, Operation 1 changes X to 12, and then Operation 2 changes it to 13, and then Operation 3 changes it to 12.
Sample Input 2
9 12 1 1 2 2 3 3 1 4 2 5 3 6 1 7 2 8 3 9
Sample Output 2
0 2 1 0 5 3 3 11 2
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
2 次元平面上に N 頂点 0 辺のグラフ G があります。頂点には 1 から N までの番号が付いており、頂点 i は座標 (x_i,y_i) にあります。
G の頂点 u,v に対して、u,v の距離 d(u,v) をマンハッタン距離 d(u,v)=|x_u-x_v|+|y_u-y_v| で定義します。
また、G の 2 つの連結成分 A,B に対して、A,B の頂点集合をそれぞれ V(A),V(B) としたとき、A,B の距離 d(A,B) を d(A,B)=\min\lbrace d(u,v)\mid u \in V(A), v\in V(B)\rbrace で定義します。
以下で説明されるクエリを Q 個処理してください。クエリは次の 3 種類のいずれかです。
1 a b: G の頂点数を n としたとき、頂点 n+1 の座標を (x_{n+1},y_{n+1})=(a,b) として、G に頂点 n+1 を追加する。2: G の頂点数を n、連結成分数を m とする。- m=1 のとき、
-1を出力する。 - m\geq 2 のとき、距離が最小である連結成分をすべてマージし、その最小の距離の値を出力する。厳密には、G の連結成分を A_1,A_2,\ldots,A_m として、\displaystyle k=\min_{1\leq i\lt j\leq m} d(A_i,A_j) とする。同じ連結成分にない頂点の組 (u,v)\ (1\leq u\lt v\leq n) であって d(u,v)=k を満たすようなものすべてについて、頂点 u と v の間に辺を張る。その後、k を出力する。
- m=1 のとき、
3 u v: 頂点 u,v が同じ連結成分にあるならばYesを、そうでないならばNoを出力する。
制約
- 2\leq N\leq 1500
- 1\leq Q\leq 1500
- 0\leq x_i,y_i\leq 10^9
- 1 種類目のクエリについて、0\leq a,b\leq 10^9
- 3 種類目のクエリについて、そのクエリを処理する直前の G の頂点数を n としたとき、1\leq u\lt v\leq n
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。\mathrm{query}_i は i 番目に処理するクエリである。
N Q
x_1 y_1
x_2 y_2
\vdots
x_N y_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
各クエリは以下の 3 種類のいずれかの形式で与えられる。
1 a b
2
3 u v
出力
問題文の指示に従ってクエリへの答えを改行区切りで出力せよ。
入力例 1
4 11 3 4 3 3 7 3 2 2 3 1 2 2 3 1 2 1 6 4 2 3 2 5 2 3 2 5 2 1 2 2 2
出力例 1
No 1 Yes 2 No 3 Yes -1 0
はじめ、頂点 1,2,3,4 はそれぞれ座標 (3,4),(3,3),(7,3),(2,2) にあります。
- 1 番目のクエリについて、頂点 1 と 2 は連結でないため、
Noを出力します。 - 2 番目のクエリについて、連結成分は 4 つあり、各連結成分に含まれる頂点集合は \lbrace 1\rbrace, \lbrace 2\rbrace, \lbrace 3\rbrace, \lbrace 4\rbrace です。異なる連結成分間の距離の最小値は 1 であり、頂点 1 と 2 の間に辺が張られます。1 を出力します。
- 3 番目のクエリについて、頂点 1 と 2 は連結であるため、
Yesを出力します。 - 4 番目のクエリについて、座標 (6,4) に頂点 5 を追加します。
- 5 番目のクエリについて、連結成分は 4 つあり、各連結成分に含まれる頂点集合は \lbrace 1,2\rbrace,\lbrace 3\rbrace,\lbrace 4\rbrace,\lbrace 5\rbrace です。異なる連結成分間の距離の最小値は 2 であり、頂点 2 と 4 の間および頂点 3 と 5 の間に辺が張られます。2 を出力します。
- 6 番目のクエリについて、頂点 2 と 5 は連結でないため、
Noを出力します。 - 7 番目のクエリについて、連結成分は 2 つあり、各連結成分に含まれる頂点集合は \lbrace 1,2,4\rbrace,\lbrace 3,5\rbrace です。異なる連結成分間の距離の最小値は 3 であり、頂点 1 と 5 の間に辺が張られます。3 を出力します。
- 8 番目のクエリについて、頂点 2 と 5 は連結であるため、
Yesを出力します。 - 9 番目のクエリについて、連結成分は 1 つであるため -1 を出力します。
- 10 番目のクエリについて、座標 (2,2) に頂点 6 を追加します。
- 11 番目のクエリについて、連結成分は 2 つあり、各連結成分に含まれる頂点集合は \lbrace 1,2,3,4,5\rbrace,\lbrace 6\rbrace です。異なる連結成分間の距離の最小値は 0 であり、頂点 4 と 6 の間に辺が張られます。0 を出力します。
Score : 500 points
Problem Statement
There is a graph G with N vertices and 0 edges on a 2-dimensional plane. The vertices are numbered from 1 to N, and vertex i is located at coordinates (x_i,y_i).
For vertices u and v of G, the distance d(u,v) between u and v is defined as the Manhattan distance d(u,v)=|x_u-x_v|+|y_u-y_v|.
Also, for two connected components A and B of G, let V(A) and V(B) be the vertex sets of A and B, respectively. The distance d(A,B) between A and B is defined as d(A,B)=\min\lbrace d(u,v)\mid u \in V(A), v\in V(B)\rbrace.
Process Q queries as described below. Each query is one of the following three types:
1 a b: Let n be the number of vertices in G. Add vertex n+1 to G with coordinates (x_{n+1},y_{n+1})=(a,b).2: Let n be the number of vertices in G and m be the number of connected components.- If m=1, output
-1. - If m\geq 2, merge all connected components with the minimum distance and output the value of that minimum distance. Formally, let the connected components of G be A_1,A_2,\ldots,A_m and let \displaystyle k=\min_{1\leq i\lt j\leq m} d(A_i,A_j). For all pairs of vertices (u,v)\ (1\leq u\lt v\leq n) that are not in the same connected component and satisfy d(u,v)=k, add an edge between vertices u and v. Then, output k.
- If m=1, output
3 u v: If vertices u and v are in the same connected component, outputYes; otherwise, outputNo.
Constraints
- 2\leq N\leq 1500
- 1\leq Q\leq 1500
- 0\leq x_i,y_i\leq 10^9
- For queries of type 1, 0\leq a,b\leq 10^9.
- For queries of type 3, let n be the number of vertices in G just before processing that query, then 1\leq u\lt v\leq n.
- All input values are integers.
Input
The input is given from Standard Input in the following format, where \mathrm{query}_i is the i-th query to be processed.
N Q
x_1 y_1
x_2 y_2
\vdots
x_N y_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
Each query is given in one of the following three formats:
1 a b
2
3 u v
Output
Output the answers to the queries separated by newlines, following the instructions in the problem statement.
Sample Input 1
4 11 3 4 3 3 7 3 2 2 3 1 2 2 3 1 2 1 6 4 2 3 2 5 2 3 2 5 2 1 2 2 2
Sample Output 1
No 1 Yes 2 No 3 Yes -1 0
Initially, vertices 1,2,3,4 are located at coordinates (3,4),(3,3),(7,3),(2,2), respectively.
- For the 1st query, vertices 1 and 2 are not connected, so output
No. - For the 2nd query, there are 4 connected components, and the vertex set of each connected component is \lbrace 1\rbrace, \lbrace 2\rbrace, \lbrace 3\rbrace, \lbrace 4\rbrace. The minimum distance between different connected components is 1, and an edge is added between vertices 1 and 2. Output 1.
- For the 3rd query, vertices 1 and 2 are connected, so output
Yes. - For the 4th query, add vertex 5 at coordinates (6,4).
- For the 5th query, there are 4 connected components, and the vertex set of each connected component is \lbrace 1,2\rbrace,\lbrace 3\rbrace,\lbrace 4\rbrace,\lbrace 5\rbrace. The minimum distance between different connected components is 2, and edges are added between vertices 2 and 4 and between vertices 3 and 5. Output 2.
- For the 6th query, vertices 2 and 5 are not connected, so output
No. - For the 7th query, there are 2 connected components, and the vertex set of each connected component is \lbrace 1,2,4\rbrace,\lbrace 3,5\rbrace. The minimum distance between different connected components is 3, and an edge is added between vertices 1 and 5. Output 3.
- For the 8th query, vertices 2 and 5 are connected, so output
Yes. - For the 9th query, there is 1 connected component, so output -1.
- For the 10th query, add vertex 6 at coordinates (2,2).
- For the 11th query, there are 2 connected components, and the vertex set of each connected component is \lbrace 1,2,3,4,5\rbrace,\lbrace 6\rbrace. The minimum distance between different connected components is 0, and an edge is added between vertices 4 and 6. Output 0.