実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
N 人の人がいます。N 人の人には人 1, 人 2,\dots, 人 N と番号がついています。
人 i(2 \le i \le N) の親は人 P_i です。ここで、P_i < i が保証されます。
人 1 が人 N の何代前か求めてください。
制約
- 2 \le N \le 50
- 1 \le P_i < i(2 \le i \le N)
- 入力は全て整数。
入力
入力は以下の形式で標準入力から与えられる。
N P_2 P_3 \dots P_N
出力
答えを整数として出力せよ。
入力例 1
3 1 2
出力例 1
2
人 2 は人 3 の親であるため、人 3 の 1 代前です。
人 1 は人 2 の親であるため、人 3 の 2 代前です。
よって解は 2 です。
入力例 2
10 1 2 3 4 5 6 7 8 9
出力例 2
9
Score : 200 points
Problem Statement
There are N people, called Person 1, Person 2, \ldots, Person N.
The parent of Person i (2 \le i \le N) is Person P_i. Here, it is guaranteed that P_i < i.
How many generations away from Person N is Person 1?
Constraints
- 2 \le N \le 50
- 1 \le P_i < i(2 \le i \le N)
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N P_2 P_3 \dots P_N
Output
Print the answer as a positive integer.
Sample Input 1
3 1 2
Sample Output 1
2
Person 2 is a parent of Person 3, and thus is one generation away from Person 3.
Person 1 is a parent of Person 2, and thus is two generations away from Person 3.
Therefore, the answer is 2.
Sample Input 2
10 1 2 3 4 5 6 7 8 9
Sample Output 2
9
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
N 個のマスが左右一列に並んでおり、左から順にマス 1、マス 2、…、マス N と番号づけられています。
また、K 個のコマがあり、最初左から i 番目のコマはマス A_i に置かれています。
これらに対して、Q 回の操作を行います。
i 回目の操作では次の操作を行います。
- 左から L_i 番目のコマが一番右のマスにあるならば何も行わない。
- そうでない時、左から L_i 番目のコマがあるマスの 1 つ右のマスにコマが無いならば、左から L_i 番目のコマを 1 つ右のマスに移動させる。 1 つ右のマスにコマがあるならば、何も行わない。
Q 回の操作が終了した後の状態について、i=1,2,\ldots,K に対して左から i 番目のコマがあるマスの番号を出力してください。
制約
- 1\leq K\leq N\leq 200
- 1\leq A_1<A_2<\cdots<A_K\leq N
- 1\leq Q\leq 1000
- 1\leq L_i\leq K
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N K Q A_1 A_2 \ldots A_K L_1 L_2 \ldots L_Q
出力
K 個の整数を空白区切りで一行に出力せよ。 ここで、i 個目の整数は、 Q 回の操作が終了した後の状態について、左から i 番目のコマの番号を表す。
入力例 1
5 3 5 1 3 4 3 3 1 1 2
出力例 1
2 4 5
最初、コマはマス 1, 3, 4 にあります。これに対して以下のように操作が行われます。
- 左から 3 番目のコマはマス 4 にあります。 これは一番右のマスでなく、その 1 つ右のマスにもコマが置かれていないため、左から 3 番目のコマをマス 5 に動かします。 コマはマス 1, 3, 5 にある状態になります。
- 左から 3 番目のコマはマス 5 にあります。 これは一番右のマスなので、何も行いません。 コマはマス 1, 3, 5 にある状態のままです。
- 左から 1 番目のコマはマス 1 にあります。 これは一番右のマスでなく、その 1 つ右のマスにもコマが置かれていないため、左から 1 番目のコマをマス 2 に動かします。 コマはマス 2, 3, 5 にある状態になります。
- 左から 1 番目のコマはマス 2 にあります。 これは一番右のマスでありませんが、その 1 つ右のマス(マス 3 )にコマが置かれているため、何も行いません。 コマはマス 2, 3, 5 にある状態のままです。
- 左から 2 番目のコマはマス 3 にあります。 これは一番右のマスでなく、その右のマスにもコマが置かれていないため、左から 2 番目のコマをマス 4 に動かします。 コマはマス 2, 4, 5 にある状態になります。
よって、Q 回の操作が終わった後でコマはマス 2, 4, 5 に置かれているため、2,4,5 を空白区切りでこの順に出力します。
入力例 2
2 2 2 1 2 1 2
出力例 2
1 2
入力例 3
10 6 9 1 3 5 7 8 9 1 2 3 4 5 6 5 6 2
出力例 3
2 5 6 7 9 10
Score : 200 points
Problem Statement
There are N squares, indexed Square 1, Square 2, …, Square N, arranged in a row from left to right.
Also, there are K pieces. The i-th piece from the left is initially placed on Square A_i.
Now, we will perform Q operations against them.
The i-th operation is as follows:
- If the L_i-th piece from the left is on its rightmost square, do nothing.
- Otherwise, move the L_i-th piece from the left one square right if there is no piece on the next square on the right; if there is, do nothing.
Print the index of the square on which the i-th piece from the left is after the Q operations have ended, for each i=1,2,\ldots,K.
Constraints
- 1\leq K\leq N\leq 200
- 1\leq A_1<A_2<\cdots<A_K\leq N
- 1\leq Q\leq 1000
- 1\leq L_i\leq K
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K Q A_1 A_2 \ldots A_K L_1 L_2 \ldots L_Q
Output
Print K integers in one line, with spaces in between. The i-th of them should be the index of the square on which the i-th piece from the left is after the Q operations have ended.
Sample Input 1
5 3 5 1 3 4 3 3 1 1 2
Sample Output 1
2 4 5
At first, the pieces are on Squares 1, 3, and 4. The operations are performed against them as follows:
- The 3-rd piece from the left is on Square 4. This is not the rightmost square, and the next square on the right does not contain a piece, so move the 3-rd piece from the left to Square 5. Now, the pieces are on Squares 1, 3, and 5.
- The 3-rd piece from the left is on Square 5. This is the rightmost square, so do nothing. The pieces are still on Squares 1, 3, and 5.
- The 1-st piece from the left is on Square 1. This is not the rightmost square, and the next square on the right does not contain a piece, so move the 1-st piece from the left to Square 2. Now, the pieces are on Squares 2, 3, and 5.
- The 1-st piece from the left is on Square 2. This is not the rightmost square, but the next square on the right (Square 3) contains a piece, so do nothing. The pieces are still on Squares 2, 3, and 5.
- The 2-nd piece from the left is on Square 3. This is not the rightmost square, and the next square on the right does not contain a piece, so move the 2-nd piece from the left to Square 4; Now, the pieces are still on Squares 2, 4, and 5.
Thus, after the Q operations have ended, the pieces are on Squares 2, 4, and 5, so 2, 4, and 5 should be printed in this order, with spaces in between.
Sample Input 2
2 2 2 1 2 1 2
Sample Output 2
1 2
Sample Input 3
10 6 9 1 3 5 7 8 9 1 2 3 4 5 6 5 6 2
Sample Output 3
2 5 6 7 9 10
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
N 人の巨人がいます。巨人にはそれぞれ 1, 2, \ldots, N の名前がついており、巨人 i が地面に立ったとき、肩の高さは A_i、頭の高さは B_i となります。
あなたは (1, 2, \ldots, N) を並べ替えて得られる数列 (P_1, P_2, \ldots, P_N) を選び、以下の規則に従って N 人の巨人を積み上げることができます。
-
まず地面に巨人 P_1 を立たせる。巨人 P_1 の肩は地面を基準として A_{P_1}、頭は地面を基準として B_{P_1} の高さとなる。
-
i = 1, 2, \ldots, N - 1 の順に巨人 P_i の肩の上に巨人 P_{i + 1} を立たせる。巨人 P_i の肩が地面を基準として高さ t のとき、巨人 P_{i + 1} の肩は地面を基準として t + A_{P_{i + 1}}、頭は地面を基準として t + B_{P_{i + 1}} の高さとなる。
一番上に立っている巨人、すなわち巨人 P_N の地面を基準とした頭の高さとして実現できる最大値を求めてください。
制約
- 2 \leq N \leq 2 \times 10^5
- 1 \leq A_i \leq B_i \leq 10^9
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 B_1 A_2 B_2 \vdots A_N B_N
出力
答えを出力せよ。
入力例 1
3 4 10 5 8 2 9
出力例 1
18
(P_1, P_2, P_3) = (2, 1, 3) とすると、地面を基準として巨人 2 は肩の高さが 5、頭の高さが 8、巨人 1 は肩の高さが 9、頭の高さが 15、巨人 3 は肩の高さが 11、頭の高さが 18 となります。
一番上に立っている巨人の頭の高さが地面を基準として 18 より大きくなることはないため 18 を出力します。
入力例 2
5 1 1 1 1 1 1 1 1 1 1
出力例 2
5
入力例 3
10 690830957 868532399 741145463 930111470 612846445 948344128 540375785 925723427 723092548 925021315 928915367 973970164 563314352 832796216 562681294 868338948 923012648 954764623 691107436 891127278
出力例 3
7362669937
Score: 300 points
Problem Statement
There are N giants, named 1 to N. When giant i stands on the ground, their shoulder height is A_i, and their head height is B_i.
You can choose a permutation (P_1, P_2, \ldots, P_N) of (1, 2, \ldots, N) and stack the N giants according to the following rules:
-
First, place giant P_1 on the ground. The giant P_1's shoulder will be at a height of A_{P_1} from the ground, and their head will be at a height of B_{P_1} from the ground.
-
For i = 1, 2, \ldots, N - 1 in order, place giant P_{i + 1} on the shoulders of giant P_i. If giant P_i's shoulders are at a height of t from the ground, then giant P_{i + 1}'s shoulders will be at a height of t + A_{P_{i + 1}} from the ground, and their head will be at a height of t + B_{P_{i + 1}} from the ground.
Find the maximum possible height of the head of the topmost giant P_N from the ground.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq A_i \leq B_i \leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 B_1 A_2 B_2 \vdots A_N B_N
Output
Print the answer.
Sample Input 1
3 4 10 5 8 2 9
Sample Output 1
18
If (P_1, P_2, P_3) = (2, 1, 3), then measuring from the ground, giant 2 has a shoulder height of 5 and a head height of 8, giant 1 has a shoulder height of 9 and a head height of 15, and giant 3 has a shoulder height of 11 and a head height of 18.
The head height of the topmost giant from the ground cannot be greater than 18, so print 18.
Sample Input 2
5 1 1 1 1 1 1 1 1 1 1
Sample Output 2
5
Sample Input 3
10 690830957 868532399 741145463 930111470 612846445 948344128 540375785 925723427 723092548 925021315 928915367 973970164 563314352 832796216 562681294 868338948 923012648 954764623 691107436 891127278
Sample Output 3
7362669937
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : {400} 点
問題文
高橋君は AtCoder Land にある屋外アトラクションに K 回乗ろうとしています。このアトラクションの一回の所要時間は T です。
現在の時刻は 0 です。時刻 0 から N までの降水量が与えられます。時刻 i から時刻 i+1 までの降水量は P_i です (0\leq i\lt N)。アトラクションには時刻 0,1,…,N−T に乗ることができます。時刻 t にアトラクションに乗ると時刻 t+T にアトラクションから降りることができ、アトラクションに乗っている間に P_t+P_{t+1}+\dots+P_{t+T-1} だけ濡れます。
高橋君はアトラクションに乗っている間にできるだけ雨に濡れないようにしたいです。時刻 N までに K 回アトラクションに乗るとき、高橋君が濡れる量の総和の最小値を求めてください。ただし、乗り換えにかかる時間や待ち時間は 0 とし、アトラクションから降りた時刻に新たにアトラクションに乗ることができるものとします。
制約
- 1\leq N\leq 2\times 10^5
- 1\leq K\leq \min(N,100)
- 1\leq T\leq N/K
- 0\leq P_i\leq 10^{9}
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N K T
P_0 P_1 \ldots P_{N-1}
出力
答えを出力せよ。
入力例 1
8 3 2 3 5 10 4 1 7 3 9
出力例 1
23
次のようにアトラクションに 3 回乗ることで濡れる量を合計で 23 にすることができ、これが最小です。
- アトラクションに時刻 0 に乗り、時刻 2 に降りる。この間に P_0+P_1=3+5=8 濡れる。
- アトラクションに時刻 3 に乗り、時刻 5 に降りる。この間に P_3+P_4=4+1=5 濡れる。
- アトラクションに時刻 5 に乗り、時刻 7 に降りる。この間に P_5+P_6=7+3=10 濡れる。
入力例 2
5 1 3 1000 1 10000 100 10
出力例 2
10101
入力例 3
15 5 2 401054171 63773035 986525245 157986893 799814573 139201145 649233932 351289844 409065258 406122133 957285954 529460482 21179081 795984182 727882733
出力例 3
3522058414
実行時間制限: 3 sec / メモリ制限: 1024 MiB
配点 : 450 点
問題文
高橋くんは睡眠記録をつけています。 睡眠記録は奇数長の数列 A=(A _ 1(=0), A _ 2,\ldots,A _ N) で表され、奇数番目は起床時刻を、偶数番目は就寝時刻を表しています。 より厳密には、睡眠記録をつけている間に高橋くんは次のような睡眠をとりました。
- すべての 1\leq i\leq\dfrac{N-1}2 を満たす整数 i について、睡眠記録をつけ始めてから A _ {2i} 分後ちょうどに寝て、A _ {2i+1} 分後ちょうどに起きた。
- それ以外の時間に寝ることも起きることもなかった。
次の Q 個の質問に答えてください。 i 番目の質問では、0\leq l _ i\leq r _ i\leq A _ N を満たす整数の組 (l _ i,r _ i) が与えられます。
- 睡眠記録をつけ始めてから l _ i 分後ちょうどから r _ i 分後ちょうどまでの r _ i-l _ i 分のうち、高橋くんが寝ていたのは何分間ですか?
制約
- 3\leq N\lt2\times10^5
- N は奇数
- 0=A _ 1\lt A _ 2\lt\cdots\lt A _ N\leq10^9
- 1\leq Q\leq2\times10^5
- 0\leq l _ i\leq r _ i\leq A _ N\ (1\leq i\leq Q)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A _ 1 A _ 2 \ldots A _ N Q l _ 1 r _ 1 l _ 2 r _ 2 \vdots l _ Q r _ Q
出力
答えを Q 行で出力せよ。 i 行目には i 番目の質問の答えを整数として出力せよ。
入力例 1
7 0 240 720 1320 1440 1800 2160 3 480 1920 720 1200 0 2160
出力例 1
480 0 960
高橋くんは、以下の図のように睡眠をとりました。

それぞれの質問の答えは以下のようになります。
- 睡眠記録をつけ始めてから 480 分後から 1920 分後の間、高橋くんは 480 分後から 720 分後、1320 分後から 1440 分後、1800 分後から 1920 分後の 3 つの睡眠をとりました。睡眠時間の合計は 240+120+120=480 分です。
- 睡眠記録をつけ始めてから 720 分後から 1200 分後の間、高橋くんは睡眠をとりませんでした。睡眠時間の合計は 0 分です。
- 睡眠記録をつけ始めてから 0 分後から 2160 分後の間、高橋くんは 240 分後から 720 分後、1320 分後から 1440 分後、1800 分後から 2160 分後の 3 つの睡眠をとりました。睡眠時間の合計は 480+120+360=960 分です。
よって、それぞれの行に 480,0,960 と出力してください。
入力例 2
21 0 20 62 192 284 310 323 324 352 374 409 452 486 512 523 594 677 814 838 946 1000 10 77 721 255 541 478 970 369 466 343 541 42 165 16 618 222 592 730 983 338 747
出力例 2
296 150 150 49 89 20 279 183 61 177
Score : 450 points
Problem Statement
Takahashi keeps a sleep log. The log is represented as an odd-length sequence A=(A _ 1(=0), A _ 2,\ldots,A _ N), where odd-numbered elements represent times he got up, and even-numbered elements represent times he went to bed. More formally, he had the following sleep sessions after starting the sleep log.
- For every integer i such that 1\leq i\leq\dfrac{N-1}2, he fell asleep exactly A _ {2i} minutes after starting the sleep log and woke up exactly A _ {2i+1} minutes after starting the sleep log.
- He did not fall asleep or wake up at any other time.
Answer the following Q questions. For the i-th question, you are given a pair of integers (l _ i,r _ i) such that 0\leq l _ i\leq r _ i\leq A _ N.
- What is the total number of minutes for which Takahashi was asleep during the r _ i-l _ i minutes from exactly l _ i minutes to r _ i minutes after starting the sleep log?
Constraints
- 3\leq N\lt2\times10^5
- N is odd.
- 0=A _ 1\lt A _ 2\lt\cdots\lt A _ N\leq10^9
- 1\leq Q\leq2\times10^5
- 0\leq l _ i\leq r _ i\leq A _ N\ (1\leq i\leq Q)
- 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 Q l _ 1 r _ 1 l _ 2 r _ 2 \vdots l _ Q r _ Q
Output
Print the answer in Q lines. The i-th line should contain an integer answering to the i-th question.
Sample Input 1
7 0 240 720 1320 1440 1800 2160 3 480 1920 720 1200 0 2160
Sample Output 1
480 0 960
Takahashi slept as shown in the following figure.

The answers to each question are as follows.
- Between 480 minutes and 1920 minutes after starting the sleep log, Takahashi slept from 480 minutes to 720 minutes, from 1320 minutes to 1440 minutes, and from 1800 minutes to 1920 minutes in 3 sleep sessions. The total sleep time is 240+120+120=480 minutes.
- Between 720 minutes and 1200 minutes after starting the sleep log, Takahashi did not sleep. The total sleep time is 0 minutes.
- Between 0 minutes and 2160 minutes after starting the sleep log, Takahashi slept from 240 minutes to 720 minutes, from 1320 minutes to 1440 minutes, and from 1800 minutes to 2160 minutes in 3 sleep sessions. The total sleep time is 480+120+360=960 minutes.
Therefore, the three lines of the output should contain 480, 0, and 960.
Sample Input 2
21 0 20 62 192 284 310 323 324 352 374 409 452 486 512 523 594 677 814 838 946 1000 10 77 721 255 541 478 970 369 466 343 541 42 165 16 618 222 592 730 983 338 747
Sample Output 2
296 150 150 49 89 20 279 183 61 177