A - 伝説の団子食通 (Legendary Dango Eater)

実行時間制限: 2.5 sec / メモリ制限: 1024 MiB

Score: 100 points

Problem Statement

Bitaro bought a long skewer of dumplings as a snack. This skewer can be represented by N positive integers A_1, A_2, \cdots, A_N as follows. For each integer i with 0 \leq i \leq N, let s_i = A_1 + A_2 + \cdots + A_i. In particular, we define s_0 = 0.

  • The skewer consists of s_N dumplings arranged in a single column from top to bottom.
  • Each dumpling is either sweet or spicy. The taste of each dumpling can be described as follows.
    • If i is an odd integer satisfying 1 \leq i \leq N, then the dumplings from the (s_{i-1} + 1)-th to the s_i-th from the top are sweet.
    • If i is an even integer satisfying 1 \leq i \leq N, then the dumplings from the (s_{i-1} + 1)-th to the s_i-th from the top are spicy.

When eating this skewer of dumplings, Bitaro made Q possible plans. The j-th plan (1 \leq j \leq Q) is represented by integers L_j and R_j satisfying 1 \leq L_j \leq R_j \leq N, and in this plan he eats the dumplings from the (s_{L_j-1}+1)-th from the top through the s_{R_j}-th from the top.

Also, when eating this skewer, Bitaro decided to divide it into several bites in the following manner. Here, K is a positive integer representing Bitaro's preference for sweetness.

  • He eats the dumplings from top to bottom, and each dumpling is eaten exactly once.
  • In one bite, he may eat any positive number of consecutive dumplings on the skewer. If, among the dumplings eaten in that bite, the number of sweet dumplings minus the number of spicy dumplings is at least K, then Bitaro is pleased.

Given the information about the skewer of dumplings and the plans, write a program that, for each plan, computes the maximum possible number of times Bitaro can be pleased.


Input

Read the following data from the standard input.

N Q K
A_1 A_2 \cdots A_N
L_1 R_1
L_2 R_2
\vdots
L_Q R_Q

Output

Write Q lines to the standard output. In the j-th line (1 \leq j \leq Q), output the maximum number of times Bitaro can be pleased in the j-th plan.


Constraints

  • 1 \leq N \leq 500\,000.
  • 1 \leq Q \leq 500\,000.
  • 1 \leq K \leq 10^{14}.
  • 1 \leq A_i \leq 10^9 (1 \leq i \leq N).
  • 1 \leq L_j \leq R_j \leq N (1 \leq j \leq Q).
  • Given values are all integers.

Subtasks

  1. (6 points) Q \leq 10.
  2. (5 points) K \leq 2.
  3. (18 points) K \leq 10.
  4. (27 points) A_1 + A_2 + \cdots + A_N \leq 500\,000
  5. (17 points) N \leq 200\,000, Q \leq 200\,000
  6. (27 points) No additional constraints.

Sample Input 1

5 2 1
2 1 2 4 3
1 5
2 4

Sample Output 1

7
2

For the first plan, Bitaro eats the dumplings from the first through the 12th from the top. By repeatedly eating exactly one dumpling per bite from the top, Bitaro can be pleased 7 times. Since it is impossible to make him pleased 8 times or more, you should output 7.

For the second plan, Bitaro eats the dumplings from the third through the 9th from the top. By repeatedly eating exactly one dumpling per bite from the top, Bitaro can be pleased 2 times. Since it is impossible to make him pleased 3 times or more, you should output 2.

This sample input satisfies the constraints of all subtasks.


Sample Input 2

5 2 3
2 1 2 4 3
1 5
2 4

Sample Output 2

2
0

This differs from Sample Input 1 only in the value of K.

For the first plan, Bitaro can be pleased 2 times by eating the dumplings in the following four bites.

  • In the first bite, he eats the first through the 5th dumplings from the top. Since the number of sweet dumplings is 4 and the number of spicy dumplings is 1, Bitaro is pleased.
  • In the second bite, he eats only the 6th dumpling from the top. Since the number of sweet dumplings is 0 and the number of spicy dumplings is 1, Bitaro is not pleased.
  • In the third bite, he eats the 7th through the 9th dumplings from the top. Since the number of sweet dumplings is 0 and the number of spicy dumplings is 3, Bitaro is not pleased.
  • In the fourth bite, he eats the 10th through the 12th dumplings from the top. Since the number of sweet dumplings is 3 and the number of spicy dumplings is 0, Bitaro is pleased.

Since it is impossible to make him pleased 3 times or more, you should output 2.

For the second plan, it is impossible for Bitaro to eat the dumplings in such a way that he is pleased at least once, so you should output 0.

This sample input satisfies the constraints of subtasks 1, 3,4,5,6.


Sample Input 3

9 4 50
24 26 89 45 84 72 15 31 66
1 9
2 8
4 6
5 6

Sample Output 3

3
2
1
1

This sample input satisfies the constraints of subtasks 1,4,5,6.

配点: 100

問題文

ビ太郎は 1 本の長い串団子をおやつとして購入した.この串団子は N 個の正整数 A_1, A_2, \cdots, A_N を用いて次のように表現できる.以下,0 以上 N 以下の整数 i に対して,s_i=A_1+A_2+ \cdots +A_{i} と書く.ただし s_0=0 であると定める.

  • 串団子は s_N 個の団子が上から下に 1 列となっている形である.
  • それぞれの団子の味は甘いもしくは辛いのいずれかである.それぞれの団子の味について,以下のように表すことができる.
    • i1 \leqq i \leqq N を満たす奇数ならば,上から s_{i - 1} + 1 番目から s_i 番目までの団子は甘い味である.
    • i1 \leqq i \leqq N を満たす偶数ならば,上から s_{i - 1} + 1 番目から s_i 番目までの団子は辛い味である.

ビ太郎はこの串団子を食べるにあたり,Q 通りの計画を立てた.j 番目 (1 \leqq j \leqq Q) の計画は,1 \leqq L_j \leqq R_j \leqq N を満たす整数 L_jR_j によって表され,上から s_{L_j - 1} + 1 番目から s_{R_j} 番目までの団子を食べるというものである.

また,ビ太郎はこの串団子を食べるにあたり,以下のように何口かに分けて食べることにした.ここで,K はビ太郎の甘さの好みを表す正整数である.

  • 団子は上から順に食べ進め,どの団子もちょうど 1 回食べる.
  • 一口では,串上で連続する団子を好きな個数食べることができる.このとき,この一口で食べた団子について,甘い味のものの個数から辛い味のものの個数を引いた値が K 以上ならば,ビ太郎は喜ぶ.

串団子と計画の情報が与えられたとき,それぞれの計画について,ビ太郎が喜ぶ回数として考えられる最大値を求めるプログラムを作成せよ.


入力

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

N Q K
A_1 A_2 \cdots A_N
L_1 R_1
L_2 R_2
\vdots
L_Q R_Q

出力

Q 行に出力せよ.j 行目 (1 \leqq j \leqq Q) には j 番目の計画におけるビ太郎が喜ぶ回数として考えられる最大値を出力せよ.


制約

  • 1 \leqq N \leqq 500\,000
  • 1 \leqq Q \leqq 500\,000
  • 1 \leqq K \leqq 10^{14}
  • 1 \leqq A_i \leqq 10^9 (1 \leqq i \leqq N).
  • 1 \leqq L_j \leqq R_j \leqq N (1 \leqq j \leqq Q).
  • 入力される値はすべて整数である.

小課題

  1. (6 点) Q \leqq 10
  2. (5 点) K \leqq 2
  3. (18 点) K \leqq 10
  4. (27 点) A_1 + A_2 + \cdots + A_N \leqq 500\,000
  5. (17 点) N \leqq 200\,000Q \leqq 200\,000
  6. (27 点) 追加の制約はない.

入力例 1

5 2 1
2 1 2 4 3
1 5
2 4

出力例 1

7
2

1 番目の計画について,ビ太郎は上から 1 番目から 12 番目までの団子を食べる.ビ太郎は上から一口で 1 つの団子を食べることを繰り返すことで,ビ太郎が喜ぶ回数を 7 回にできる.8 回以上喜ぶようにすることはできないため,7 を出力する.

2 番目の計画について,ビ太郎は上から 3 番目から 9 番目までの団子を食べる.ビ太郎は上から一口で 1 つの団子を食べることを繰り返すことで,ビ太郎が喜ぶ回数を 2 回にできる.3 回以上喜ぶようにすることはできないため,2 を出力する.

この入力例はすべての小課題の制約を満たす.


入力例 2

5 2 3
2 1 2 4 3
1 5
2 4

出力例 2

2
0

入力例 1 とは K の値のみが異なる.

1 番目の計画について,ビ太郎は以下のように団子を四口で食べることで,ビ太郎が喜ぶ回数を 2 回にできる.

  • 一口目には上から 1 番目の団子から 5 番目までの団子を食べる.甘い味のものの個数は 4 であり,辛い味のものの個数は 1 であるため,ビ太郎は喜ぶ.
  • 二口目には上から 6 番目の団子のみを食べる.甘い味のものの個数は 0 であり,辛い味のものの個数は 1 であるため,ビ太郎は喜ばない.
  • 三口目には上から 7 番目の団子から 9 番目までの団子を食べる.甘い味のものの個数は 0 であり,辛い味のものの個数は 3 であるため,ビ太郎は喜ばない.
  • 四口目には上から 10 番目の団子から 12 番目までの団子を食べる.甘い味のものの個数は 3 であり,辛い味のものの個数は 0 であるため,ビ太郎は喜ぶ.

3 回以上喜ぶようにすることはできないため,2 を出力する.

2 番目の計画について,ビ太郎が 1 回以上喜ぶように食べることはできないため,0 を出力する.

この入力例は小課題 1, 3, 4, 5, 6 の制約を満たす.


入力例 3

9 4 50
24 26 89 45 84 72 15 31 66
1 9
2 8
4 6
5 6

出力例 3

3
2
1
1

この入力例は小課題 1, 4, 5, 6 の制約を満たす.

B - 庭園 3 (Garden 3)

実行時間制限: 4 sec / メモリ制限: 1024 MiB

Score: 100 points

Problem Statement

The JOI-Garden has a rectangular shape divided into a grid of H rows and W columns. The cell in the i-th row from the top and the j-th column from the left is called cell (i,j).

When rain falls on a cell, the moisture level of that cell increases. The moisture level of a cell never changes except when it rains. If the moisture level of a cell becomes at least X, the cell turns into mud, which is dangerous. Therefore, every morning, JOI-kun, the manager of the JOI-Garden, may designate at most one rectangular off-limits area that contains all cells whose moisture level is at least X. More precisely, JOI-kun chooses four integers u,d,l,r (1\leq u\leq d\leq H,1\leq l\leq r\leq W), and the rectangular region consisting of all cells (i,j) satisfying u\leq i\leq d and l\leq j\leq r becomes off-limits.

Initially, the moisture level of every cell in the JOI Garden is 0.

Starting today, it will rain once every evening for N days. On the evening of the (k - 1)-th day after (1\leq k\leq N), rain falls on every cell (i,j) satisfying U_k\leq i\leq D_k and L_k\leq j\leq R_k, and the moisture level of each such cell increases by C_k.

For each k=1,2,\ldots ,N, write a program that computes the minimum possible number of cells contained in the off-limits area that JOI-kun sets on the morning of the k-th day after.


Input

Read the following data from the standard input.

H W N X
U_1 D_1 L_1 R_1 C_1
U_2 D_2 L_2 R_2 C_2
\vdots
U_N D_N L_N R_N C_N

Output

Write N lines to the standard output. The k-th line (1 \leq k \leq N) of the output should contain the minimum possible number of cells contained in the off-limits area that JOI-kun sets on the morning of day k.


Constraints

  • 1 \leq H \leq 10^9.
  • 1 \leq W \leq 10^9.
  • 1 \leq N \leq 200\,000.
  • 1 \leq X \leq 2\times 10^{14}.
  • 1 \leq U_k \leq D_k \leq H (1 \leq k \leq N).
  • 1 \leq L_k \leq R_k \leq W (1 \leq k \leq N).
  • 1 \leq C_k \leq 10^9 (1 \leq k \leq N).
  • Given values are all integers.

Subtasks

  1. (3 points) X = 1.
  2. (24 points) W = 1.
  3. (15 points) N \leq 300.
  4. (30 points) N \leq 5\,000.
  5. (28 points) No additional constraints.

Sample Input 1

3 3 5 10
3 3 1 1 5
1 3 1 2 7
1 3 3 3 4
1 1 1 2 12
3 3 3 3 6

Sample Output 1

0
1
1
6
9

The following is one example of how the moisture levels increase each day and how the off-limits area can be chosen so that the number of contained cells is minimized.

  • On the evening of day 0, the moisture level of cell (3,1) increases by 5. On the morning of day 1, there are no cells whose moisture level of at least 10, so no off-limits area is set.
  • On the evening of day 1, the moisture level of cells (1,1), (1,2), (2,1), (2,2), (3,1), and (3,2) each increase by 7. On the morning of day 2, cell (3,1) has moisture level of at least 10. JOI-kun chooses u=d=3 and l=r=1, thereby setting a off-limits area that contains 1 cell.
  • On the evening of day 2, the moisture level of cells (1,3), (2,3), and (3,3) each increase by 4. On the morning of day 3, cell (3,1) has moisture level of at least 10. JOI-kun chooses u=d=3 and l=r=1, thereby setting a off-limits area that contains 1 cell.
  • On the evening of day 3, the moisture level of cells (1,1), and (1,2) each increase by 12. On the morning of day 4, cells (1,1),(1,2), and (3,1) has moisture level of at least 10. JOI-kun chooses u=1, d=3, l=1, r=2, thereby setting a off-limits area that contains 6 cells.
  • On the evening of day 4, the moisture level of cell (3,3) increases by 6. On the morning of day 5, cells (1,1), (1,2), (3,1), and (3,3) has moisture level of at least 10. JOI-kun chooses u=1, d=3, l=1, r=3, thereby setting a off-limits area that contains 9 cells.

This sample input satisfies the constraints of Subtasks 3, 4, and 5.


Sample Input 2

9 1 5 1
3 3 1 1 4
5 8 1 1 1
3 5 1 1 3
8 8 1 1 4
8 9 1 1 5

Sample Output 2

1
6
6
6
7

This sample input satisfies the constraints of all the subtasks.


Sample Input 3

4596 9794 15 141929907
600 3070 2222 8763 472026497
47 2644 3276 6033 930213777
638 945 304 1100 992702990
370 2211 2178 2977 783902937
277 2601 1559 8989 842013671
566 3272 3124 8456 254633541
91 4241 2655 8035 303526265
1342 3662 3909 7175 685435928
1176 4012 2827 8429 614977118
255 2461 1482 5835 794902067
982 2314 941 3952 342731056
1603 2215 6730 7105 332440107
2301 4568 6898 9561 591652619
124 2097 3520 8882 168525684
1845 3599 5592 7145 555656973

Sample Output 3

16165282
19783008
25583040
25583040
26266464
28021036
36437770
36437770
36437770
36437770
36437770
36437770
41864676
41864676
41864676

This sample input satisfies the constraints of Subtasks 3, 4, and 5.

配点: 100

問題文

JOI 庭園は縦 H 行,横 W 列のマス目状に区切られた長方形の形をしている. 上から数えて i 行目,左から数えて j 列目のマスは区画 (i,j) と呼ばれている.

区画に雨が降ることにより,その区画の水分量が増加する. 雨が降る以外に区画の水分量が変化することはない. 水分量が X 以上になった区画はぬかるみになり,危険である. そのため JOI 庭園の管理者である JOI 君は,朝が来るごとに,水分量が X 以上である区画をすべて含むような長方形の立ち入り禁止の領域を 1 つまで設定する. 正確には,4 つの整数 u,d,l,r (1 \leqq u \leqq d \leqq H,1 \leqq l \leqq r \leqq W) を選び,u \leqq i \leqq dl \leqq j \leqq r を両方満たす区画 (i,j) 全体からなる領域を立ち入り禁止にする.

現在,JOI 庭園のすべての区画の水分量は 0 である. 今日から N 日間,毎日夕方に 1 回ずつ,JOI 庭園に雨が降る. 今日から k-1 日後 (1 \leqq k \leqq N) の夕方には,U_k \leqq i \leqq D_kL_k \leqq j \leqq R_k を両方満たすような区画 (i,j) に雨が降り, それらの区画の水分量がそれぞれ C_k だけ増加する.

k=1,2,\ldots ,N のそれぞれについて,k 日後の朝に JOI 君が設定する立ち入り禁止の領域に含まれる区画の個数としてありうる最小の値を求めるプログラムを作成せよ.


入力

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

H W N X
U_1 D_1 L_1 R_1 C_1
U_2 D_2 L_2 R_2 C_2
\vdots
U_N D_N L_N R_N C_N

出力

標準出力に N 行出力せよ. k 行目 (1 \leqq k \leqq N) には,k 日後の朝に JOI 君が設定する立ち入り禁止の領域に含まれる区画の個数としてありうる最小の値を出力せよ.


制約

  • 1 \leqq H \leqq 10^9
  • 1 \leqq W \leqq 10^9
  • 1 \leqq N \leqq 200\,000
  • 1 \leqq X \leqq 2\times 10^{14}
  • 1 \leqq U_k \leqq D_k \leqq H (1 \leqq k \leqq N).
  • 1 \leqq L_k \leqq R_k \leqq W (1 \leqq k \leqq N).
  • 1 \leqq C_k \leqq 10^9 (1 \leqq k \leqq N).
  • 入力される値はすべて整数である.

小課題

  1. (3 点) X = 1
  2. (24 点) W = 1
  3. (15 点) N \leqq 300
  4. (30 点) N \leqq 5\,000
  5. (28 点) 追加の制約はない.

入力例 1

3 3 5 10
3 3 1 1 5
1 3 1 2 7
1 3 3 3 4
1 1 1 2 12
3 3 3 3 6

出力例 1

0
1
1
6
9

以下は,各日の水分量の増加と,含まれる区画の個数が最小となるような立ち入り禁止の領域の設定方法の一例である.

  • 0 日後の夕方に,区画 (3,1) の水分量が 5 だけ増加する. 1 日後の朝の時点では,水分量が 10 以上の区画はないため,立ち入り禁止の領域は設定されない.
  • 1 日後の夕方に,区画 (1,1)(1,2)(2,1)(2,2)(3,1)(3,2) の水分量がそれぞれ 7 だけ増加する. 2 日後の朝の時点では,水分量が 10 以上の区画は区画 (3,1) である.JOI 君は,u=d=3l=r=1 を選ぶことにより 1 つの区画を含む立ち入り禁止の領域を設定する.
  • 2 日後の夕方に,区画 (1,3)(2,3)(3,3) の水分量がそれぞれ 4 だけ増加する. 3 日後の朝の時点では,水分量が 10 以上の区画は区画 (3,1) である.JOI 君は,u=d=3l=r=1 を選ぶことにより 1 つの区画を含む立ち入り禁止の領域を設定する.
  • 3 日後の夕方に,区画 (1,1)(1,2) の水分量がそれぞれ 12 だけ増加する. 4 日後の朝の時点では,水分量が 10 以上の区画は区画 (1,1)(1,2)(3,1) である.JOI 君は,u=1d=3l=1r=2 を選ぶことにより 6 つの区画を含む立ち入り禁止の領域を設定する.
  • 4 日後の夕方に,区画 (3,3) の水分量がそれぞれ 6 だけ増加する. 5 日後の朝の時点では,水分量が 10 以上の区画は区画 (1,1)(1,2)(3,1)(3,3) である.JOI 君は,u=1d=3l=1r=3 を選ぶことにより 9 つの区画を含む立ち入り禁止の領域を設定する.

この入力例は小課題 3,4,5 の制約を満たす.


入力例 2

9 1 5 1
3 3 1 1 4
5 8 1 1 1
3 5 1 1 3
8 8 1 1 4
8 9 1 1 5

出力例 2

1
6
6
6
7

この入力例はすべての小課題の制約を満たす.


入力例 3

4596 9794 15 141929907
600 3070 2222 8763 472026497
47 2644 3276 6033 930213777
638 945 304 1100 992702990
370 2211 2178 2977 783902937
277 2601 1559 8989 842013671
566 3272 3124 8456 254633541
91 4241 2655 8035 303526265
1342 3662 3909 7175 685435928
1176 4012 2827 8429 614977118
255 2461 1482 5835 794902067
982 2314 941 3952 342731056
1603 2215 6730 7105 332440107
2301 4568 6898 9561 591652619
124 2097 3520 8882 168525684
1845 3599 5592 7145 555656973

出力例 3

16165282
19783008
25583040
25583040
26266464
28021036
36437770
36437770
36437770
36437770
36437770
36437770
41864676
41864676
41864676

この入力例は小課題 3,4,5 の制約を満たす.

C - マルチコミュニケーション 2 (Multi Communication 2)

実行時間制限: 3 sec / メモリ制限: 1024 MiB

Score: 100 points

Distributed Files

Problem Statement

President K has prepared a game for the contestants of the JOI Final Stage. President K secretly has an N \times N table A, each of whose cells contains a non-negative integer. Let A_{i,j} denote the integer written in the cell in the (i + 1)-st row from the top (0 \leq i \leq N - 1) and the (j + 1)-st column from the left (0 \leq j \leq N - 1). The table A satisfies the following conditions.

  • For each i such that 0 \leq i \leq N - 1, we have A_{i,i} = 0.
  • For each i, j such that 0 \leq i < j \leq N - 1, we have A_{i,j} = A_{j,i}.

Consider the weighted undirected graph with N vertices in which the edge between vertex i and vertex j (0 \leq i < j \leq N - 1) has weight A_{i,j}. Let X be the weight of a minimum spanning tree of this graph. In other words, X is the value obtained by the following procedure. The goal of the game is for all contestants to cooperate and determine the value of X.

  1. Set x = 0.
  2. Consider an undirected graph G with N vertices. The vertices of G are numbered 0, 1, \dots, N - 1. Initially, G has no edges.
  3. Repeat the following operation N - 1 times. Let X be the value of x after these N - 1 operations.
    1. Call the vertices of G that are currently reachable from vertex 0 by using some edges the near vertices, and call the other vertices the far vertices. Choose a pair (i, j) consisting of a near vertex i and a far vertex j so that the value A_{i,j} \times N^2 + i \times N + j is minimized. It can be proved that there is at least one near vertex and at least one far vertex, and that (i, j) is uniquely determined.
    2. Add the edge connecting vertex i and vertex j to G.
    3. Update x \gets x + A_{i,j}.

Define R = \lfloor 5120 / N \rfloor (where \lfloor x \rfloor denotes the greatest integer not exceeding x). There are R \times N contestants in the JOI Final Stage, divided into R groups of N contestants each. The groups are numbered 0 through R-1, and the contestants in group r (0 \leq r \leq R-1) are numbered (r, 0), (r, 1), \dots, (r, N-1).

The game proceeds by repeating the following rounds, in the order round 0, 1, 2, \dots, at most R times. During the game, the contestants are not allowed to communicate with one another, but they may share a strategy in advance.

Round r (0 \leq r \leq R-1) proceeds as follows.

  • For i = 0, 1, \dots, N-1 in this order, President K and contestant (r, i) perform the following interaction.
    1. President K gives contestant (r, i) the following information.
      • the contestant's number (r, i),
      • the information in the (i + 1)-st row of the table A, namely A_{i,0}, A_{i,1}, \dots, A_{i,N-1},
      • the integers B_{r,i,0}, B_{r,i,1}, \dots, B_{r,i,N-1}, each of which is an integer between 0 and 2^{64}-1, inclusive, sent to contestant (r, i) from the contestants in the previous round.
        • If r > 0, these integers are determined in interaction 3 described below.
        • If r = 0, then for convenience we define B_{r,i,0} = B_{r,i,1} = \dots = B_{r,i,N-1} = 0.
    2. If contestant (r, i) is able to determine the value of X, they answer President K with that value. If any contestant gives an answer, the game ends immediately.
    3. For each j = 0, 1, \dots, N-1, contestant (r, i) determines an integer B_{r+1,j,i}, which must be between 0 and 2^{64}-1, inclusive, to send to contestant (r+1, j), and tells it to President K. Even when r = R - 1, so that contestant (r+1, j) does not exist, contestant (r, i) must still determine B_{r+1,j,i} and tell it to President K.

If an answered value of X is incorrect, or if nobody answers the value of X by the end of round R - 1, the game fails. If the correct value of X is answered by the end of round R - 1, the game succeeds. A smaller number of rounds used yields a higher score (if an answer is given in round r, the number of rounds is counted as r + 1).

Implement a strategy for the contestants so that the game succeeds in as few rounds as possible.

Implementation Details

Your submission must include multi.h using a #include preprocessing directive, and must implement the following function.

  • std::vector<unsigned long long> strategy(int N, int r, int i, std::vector<unsigned long long> A, std::vector<unsigned long long> B)
    • The argument N represents the number of rows and columns of the table A.
    • The arguments r and i represent the contestant number (r, i).
    • The argument A is a sequence of non-negative integers of length N, where A[j] (0 \leq j \leq N - 1) represents the integer A_{i,j} written in the cell in the (i + 1)-st row from the top and the (j + 1)-st column from the left of the table A.
    • The argument B is a sequence of non-negative integers of length N, where B[j] (0 \leq j \leq N - 1) represents the integer B_{r,i,j} sent from contestant (r-1, j) to contestant (r, i). If r = 0, then \texttt{B[j]} = 0.
    • This function must return a sequence of non-negative integers of length 1 or N, representing the action taken by contestant (r, i) given the information in the arguments A, B. If the returned sequence has length other than 1 or N, the submission is judged as Wrong Answer [1].
      • To answer that the value of X is x, this function must return a sequence of length 1, namely (x). If the answered value is incorrect, the submission is judged as Wrong Answer [2].
      • If no answer is given, this function must return a sequence B' of length N.
        • For each j = 0, 1, \dots, N-1, B'[j] represents the integer B_{r+1,j,i} sent by contestant (r, i) to contestant (r+1, j). It must be an integer between 0 and 2^{64}-1, inclusive. Note that even when r = R - 1 and contestant (r+1, j) does not exist, the length of B' must still be N.
      • By the end of round R - 1, at least one contestant must give an answer. If no contestant gives an answer, the submission is judged as Wrong Answer [3].
    • The return value of this function must be determined solely by its arguments. In particular, note that the return value must not depend on previous calls to strategy or on runtime randomness. If this function returns different values for the same arguments, the submission is judged as Wrong Answer [4].
    • It is guaranteed that the given arguments can actually occur when playing the game using some table A and the submitted function strategy. However, note that calls are not necessarily made in the order of rounds 0, 1, \dots, R - 1.
    • The grader will play one or more games in a single execution. In one execution, this function is called at most 10\,240 times.

Important Notes

  • You may freely implement other functions or declare global variables for internal use.
  • Your submitted program must not communicate in any way with standard input, standard output, or any other files. However, outputting debugging information and the like to standard error output is allowed.

Constraints

  • 2 \leq N \leq 256.
  • 0 \leq A_{i,j} < 2^{48} (0 \leq i \leq N - 1, 0 \leq j \leq N - 1).
  • A_{i,i} = 0 (0 \leq i \leq N - 1).
  • A_{i,j} = A_{j,i} (0 \leq i < j \leq N - 1).
  • N and A_{i,j} (0 \leq i \leq N - 1, 0 \leq j \leq N - 1) are integers.

Subtasks

  1. (5 points) N \leq 64, A_{i,j} \leq 1 (0 \leq i \leq N - 1, 0 \leq j \leq N - 1).
  2. (10 points) A_{i,j} \leq 1 (0 \leq i \leq N - 1, 0 \leq j \leq N - 1).
  3. (15 points) N \leq 64.
  4. (40 points) A_{i,j} < 2^{20} (0 \leq i \leq N - 1, 0 \leq j \leq N - 1).
  5. (15 points) A_{i,j} < 2^{40} (0 \leq i \leq N - 1, 0 \leq j \leq N - 1).
  6. (15 points) There are no additional constraints.

Scoring

If, among the test cases of a subtask, there is even one that is judged as Wrong Answer [1]–[4], Time Limit Exceeded, Memory Limit Exceeded, or Runtime Error, then the score for that subtask is 0. Otherwise, let S be the maximum number of rounds used over all games in that subtask. Then the score for that subtask is determined as follows.

In the case of Subtask 3

  • Regardless of the value of S, the score for that subtask is 100\% of the points for the subtask. If S > 6, the contest site may display "Output is partially correct", but this does not affect the score.

In the case of subtasks other than Subtask 3

  • If S \leq 6, the score for that subtask is 100\% of the points for the subtask.
  • If 7 \leq S \leq 9, the score for that subtask is (100 - 20 \cdot (S - 6))\% of the points for the subtask.
  • If 10 \leq S \leq 19, the score for that subtask is (40 - S)\% of the points for the subtask.
  • If 20 \leq S, the score for that subtask is 20\% of the points for the subtask.

Compilation and Test Run

A sample grader for testing your program is included in the archive downloadable from the contest site. This archive also contains a sample file that you must submit.

The sample grader consists of a single file. That file is grader.cpp. To test your program, place the files grader.cpp, multi.cpp, and multi.h in the same Presidenty, and execute the following command.

g++ -std=gnu++20 -O2 -o grader grader.cpp multi.cpp

Alternatively, you may execute the file compile.sh included in the archive. In that case, run the following command.

./compile.sh

If the compilation succeeds, an executable file named grader is generated.

Note that the actual grader used for evaluation differs from the sample grader. The sample grader runs as a single process. This program reads input from standard input and writes the results to standard output.


Input for the Sample Grader

The sample grader plays one or more games in a single execution.

First, the sample grader reads the number of games T from standard input. Then, for each of the T games, it reads the information of the table A in the following format.

N 
A_{0,1} A_{0,2} A_{0,3} \cdots A_{0,N-1} 
A_{1,2} A_{1,3} \cdots A_{1,N-1} 
\vdots 
A_{N-2,N-1} 

Output for the Sample Grader

The sample grader outputs T lines to standard output. On the t-th line (1 \leq t \leq T), it outputs the result of the t-th game in the following format (the quotation marks are not actually printed).

  • If the game succeeds, the number of rounds used in that game is output as in Accepted: 22.
  • If any of the Wrong Answer conditions applies, the type of Wrong Answer is output as in Wrong Answer [4].

If the program being executed satisfies more than one Wrong Answer condition, only one of them will be displayed.

Sample Communication

Below is an example of input read by the sample grader and the corresponding sequence of function calls.

Sample Input 1

1
3
1 2
3

In round 0, the following interactions take place.

  • Contestant (0, 0) does not answer the value of X, and sends B_{1,0,0} = 0 to contestant (1, 0), B_{1,1,0} = 1 to contestant (1, 1), and B_{1,2,0} = 2 to contestant (1, 2).
  • Contestant (0, 1) does not answer the value of X, and sends B_{1,0,1} = 3 to contestant (1, 0), B_{1,1,1} = 4 to contestant (1, 1), and B_{1,2,1} = 5 to contestant (1, 2).
  • Contestant (0, 2) does not answer the value of X, and sends B_{1,0,2} = 6 to contestant (1, 0), B_{1,1,2} = 7 to contestant (1, 1), and B_{1,2,2} = 8 to contestant (1, 2).

Since no contestant answered the value of X, the game proceeds to the next round.

In round 1, the following interaction takes place.

  • Contestant (1, 0) receives B_{1,0,0} = 0 from contestant (0, 0), B_{1,0,1} = 3 from contestant (0, 1), and B_{1,0,2} = 6 from contestant (0, 2), and answers that X = 3. Since the value of X has been answered, the game ends at this point.

Because the answered value of X is correct, the game succeeds. The number of rounds used in this game is 2.

This sample input satisfies the constraints of Subtasks 3, 4, 5, and 6.

Among the files downloadable from the contest site, sample-01-in.txt corresponds to Sample Input 1. The archive downloadable from the contest site also contains further sample inputs: sample-02-in.txt, sample-03-in.txt, and sample-04-in.txt. sample-02-in.txt satisfies the constraints of all subtasks, sample-03-in.txt satisfies the constraints of Subtasks 3, 4, 5, and 6, and sample-04-in.txt satisfies the constraints of Subtasks 4, 5, and 6.

配点: 100

配布ファイル

問題文

K 理事長は JOI ファイナルステージの参加者にゲームを用意した. K 理事長は各マスに非負整数が書かれた N \times N マスの表 A を隠し持っている. A の上から i + 1 行目 (0 \leqq i \leqq N - 1),左から j + 1 列目 (0 \leqq j \leqq N - 1) のマスに書かれた非負整数を A_{i,j} とする. ここで,A は以下の条件を満たす.

  • 0 \leqq i \leqq N - 1 を満たす各 i に対して,A_{i,i} = 0
  • 0 \leqq i < j \leqq N - 1 を満たす各 i, j に対して,A_{i,j} = A_{j,i}

頂点 i と頂点 j の間 (0 \leqq i < j \leqq N - 1) の辺の重みが A_{i,j} であるような N 頂点の重み付き無向グラフの最小全域木の重みを X とする. すなわち,X は以下の手順で求められる値である.参加者全員で協力して X の値を求めることがこのゲームの目標である.

  1. x = 0 とする.
  2. N 頂点の無向グラフ G を考える.G の頂点には 0, 1, \dots, N - 1 の番号が付けられている.はじめ,G には辺が張られていない.
  3. 以下の操作を N - 1 回繰り返す.N - 1 回の操作が終了した後の x の値を X とする.
    1. 現在いくつかの辺を通って頂点 0 から到達可能である G の頂点を近い頂点, それ以外の G の頂点を遠い頂点と呼ぶ. 近い頂点 i と遠い頂点 j の組 (i, j) を,A_{i,j} \times N^2 + i \times N + j の値が最小になるように選ぶ. 近い頂点と遠い頂点がそれぞれ 1 つ以上存在し,(i, j) が一意に定まることが証明できる.
    2. 頂点 i と頂点 j を結ぶ辺を G に追加する.
    3. x \gets x + A_{i,j} と更新する.

R = \lfloor 5120 / N \rfloor と定義する (\lfloor x \rfloorx を超えない最大の整数を表す). JOI ファイナルステージには R \times N 人の参加者がおり,N 人ずつ R 個のグループに分けられている. 各グループには 0 から R-1 までの番号が付けられており,グループ r (0 \leqq r \leqq R-1) の参加者にはそれぞれ (r, 0), (r, 1), \dots, (r, N-1) の番号が付けられている.

ゲームは以下のラウンドをラウンド 0, 1, 2, \dots の順に最大 R 回繰り返すことで行われる. ゲーム中は参加者同士でコミュニケーションを取ることはできないが,事前に戦略を共有することができる.

ラウンド r (0 \leqq r \leqq R-1) は以下の手順で行われる.

  • i = 0, 1, \dots, N-1 の順に,K 理事長と参加者 (r, i) は以下のやりとりを行う.
    1. K 理事長は,参加者 (r, i) に以下の情報を与える.
      • 参加者の番号 (r, i)
      • Ai + 1 行目の情報 A_{i,0}, A_{i,1}, \dots, A_{i,N-1}
      • 前のラウンドの各参加者から参加者 (r, i) に送られた,0 以上 2^{64} 未満の整数 B_{r,i,0}, B_{r,i,1}, \dots, B_{r,i,N-1}
        • r > 0 の場合,これらの整数は以下で説明するやりとり 3 において決定される.
        • r = 0 の場合,便宜上 B_{r,i,0} = B_{r,i,1} = \dots = B_{r,i,N-1} = 0 とする.
    2. 参加者 (r, i)X の値を求めることができたなら,K 理事長に X の値を回答する.ある参加者が回答を行ったら,その時点でゲームは終了する.
    3. 参加者 (r, i) は,各 j = 0, 1, \dots, N-1 について,参加者 (r+1, j) に送る 0 以上 2^{64} 未満の整数 B_{r+1,j,i} を決定し,K 理事長に伝える. r = R - 1 のとき参加者 (r+1, j) は存在しないが,その場合でも B_{r+1,j,i} を決定し,K 理事長に伝える必要がある.

回答した X の値が間違っている場合や,ラウンド R - 1 までに誰も X の値を回答しなかった場合,ゲームは失敗となる. ラウンド R - 1 までに正しい X の値を回答した場合ゲームは成功となり,行われたラウンド数 (ラウンド r に回答を行った場合,ラウンド数は r + 1) が少ないほど高い評価を得られる.

できるだけ少ないラウンド数でゲームを成功させるような,参加者たちの戦略を実装せよ.

実装の詳細

あなたの回答プログラムは,multi.h#include プリプロセッサ指令で読み込み,以下の関数を実装しなければならない.

  • std::vector<unsigned long long> strategy(int N, int r, int i, std::vector<unsigned long long> A, std::vector<unsigned long long> B)
    • 引数 N は表 A の行および列の個数 N を表す.
    • 引数 r, i は参加者の番号 (r, i) を表す.
    • 引数 A は長さ N の非負整数列であり,A[j] (0 \leqq j \leqq N - 1) は表 A の上から i + 1 行目,左から j + 1 列目のマスに書かれた非負整数 A_{i,j} を表す.
    • 引数 B は長さ N の非負整数列であり,B[j] (0 \leqq j \leqq N - 1) は参加者 (r-1, j) から参加者 (r, i) に送られた 0 以上 2^{64} 未満の整数 B_{r,i,j} を表す. r = 0 の場合は \texttt{B[j]} = 0 である.
    • この関数は,引数 A, B の情報が与えられたときに参加者 (r, i) のする行動を表す,長さが 1 または N である非負整数列を返さなければならない. 返り値の非負整数列の長さが 1 でも N でもない場合,不正解[1] と判定される.
      • X の値が x であると回答する場合,この関数は長さ 1 の非負整数列 (x) を返さなければならない.回答した値が間違っている場合,不正解[2] と判定される.
      • 回答を行わない場合,この関数は長さ N の非負整数列 B' を返さなければならない.
        • j = 0, 1, \dots, N-1 について,B'[j] は参加者 (r, i) が参加者 (r+1, j) に送る整数 B_{r+1,j,i} を表す. これは 0 以上 2^{64} 未満の整数でなければならない. r = R - 1 のとき参加者 (r+1, j) は存在しないが,その場合でも B' の長さは N でなければならないことに注意せよ.
      • ラウンド R - 1 が終わるまでに,いずれかの参加者が回答を行わなければならない.どの参加者も回答を行わなかった場合,不正解[3] と判定される.
    • この関数の返り値は引数のみから決定しなければならない. 特に,以前の strategy の呼び出しや実行時の乱数によって返り値が変化してはならないことに注意せよ. この関数が同じ引数に対して異なる値を返した場合,不正解[4] と判定される.
    • 与えられる引数は,ある表 A と提出された関数 strategy を使用してゲームを行った際に実際に表れるものであることが保証される. ただし,ラウンド 0, 1, \dots, R - 1 の順に呼び出されるとは限らないことに注意せよ.
    • 採点プログラムは 1 回の実行で 1 回以上のゲームを行う.1 回の実行において,この関数は最大 10\,240 回呼び出される.

重要な注意

  • 内部での使用のために他の関数を実装したり,グローバル変数を宣言するのは自由である.
  • あなたの提出したプログラムは,標準入力・標準出力,あるいは他のファイルといかなる方法でもやりとりしてはならない. ただし,標準エラー出力にデバッグ情報等を出力することは許される.

制約

  • 2 \leqq N \leqq 256
  • 0 \leqq A_{i,j} < 2^{48} (0 \leqq i \leqq N - 10 \leqq j \leqq N - 1).
  • A_{i,i} = 0 (0 \leqq i \leqq N - 1).
  • A_{i,j} = A_{j,i} (0 \leqq i < j \leqq N - 1).
  • N, A_{i,j} (0 \leqq i \leqq N - 10 \leqq j \leqq N - 1) は整数である.

小課題

  1. (5 点) N \leqq 64A_{i,j} \leqq 1 (0 \leqq i \leqq N - 10 \leqq j \leqq N - 1).
  2. (10 点) A_{i,j} \leqq 1 (0 \leqq i \leqq N - 10 \leqq j \leqq N - 1).
  3. (15 点) N \leqq 64
  4. (40 点) A_{i,j} < 2^{20} (0 \leqq i \leqq N - 10 \leqq j \leqq N - 1).
  5. (15 点) A_{i,j} < 2^{40} (0 \leqq i \leqq N - 10 \leqq j \leqq N - 1).
  6. (15 点) 追加の制約はない.

採点基準

ある小課題のテストケースの中で,1 つでも 不正解[1] 〜 [4] と判定されたものや, 実行時間制限超過,メモリ制限超過,実行時エラーと判定されたものがあった場合,その小課題の得点は 0 点となる. それ以外の場合,その小課題のすべてのゲームにわたる,行われたラウンド数の最大値を S として,その小課題の得点は以下のように決定される.

小課題 3 の場合

  • S にかかわらず,その小課題の配点の 100\%S > 6 である場合,コンテストサイトにおいて「出力は部分的に正しい」と表記されることがあるが,得点には影響しない.

小課題 3 以外の場合

  • S \leqq 6 のとき,その小課題の配点の 100\%
  • 7 \leqq S \leqq 9 のとき,その小課題の配点の (100 - 20 \cdot (S - 6))\%
  • 10 \leqq S \leqq 19 のとき,その小課題の配点の (40 - S)\%
  • 20 \leqq S のとき,その小課題の配点の 20\%

コンパイル・実行の方法

作成したプログラムをテストするための,採点プログラムのサンプルが, コンテストサイトからダウンロードできるアーカイブの中に含まれている. このアーカイブには,提出しなければならないファイルのサンプルも含まれている.

採点プログラムのサンプルは 1 つのファイルからなる. そのファイルは grader.cpp である. 作成したプログラムをテストするには, これらのファイル grader.cpp, multi.cpp, multi.h を同じディレクトリに置き,次のようにコマンドを実行する.

g++ -std=gnu++20 -O2 -o grader grader.cpp multi.cpp

なお,アーカイブの中に含まれている compile.sh というファイルを代わりに実行してもよい.その場合,次のようにコマンドを実行する.

./compile.sh

コンパイルが成功すれば,grader という実行ファイルが生成される.

実際の採点プログラムは,採点プログラムのサンプルとは異なることに注意すること. 採点プログラムのサンプルは単一のプロセスとして起動する. このプログラムは,標準入力から入力を読み込み,標準出力に結果を出力する.

採点プログラムのサンプルの入力

採点プログラムのサンプルは 1 回の実行で 1 回以上のゲームを行う.

まず,採点プログラムのサンプルは標準入力からゲームの回数 T を受け取る. その後,T 回にわたって,以下の形式で表 A の情報を読み込む.

N 
A_{0,1} A_{0,2} A_{0,3} \cdots A_{0,N-1} 
A_{1,2} A_{1,3} \cdots A_{1,N-1} 
\vdots 
A_{N-2,N-1} 

採点プログラムのサンプルの出力

採点プログラムのサンプルは標準出力へ T 行出力する. t 行目 (1 \leqq t \leqq T) には t 回目のゲームの結果を以下の形式で出力する (引用符は実際には出力されない).

  • ゲームが成功した場合,そのゲームで行われたラウンド数が Accepted: 22 のように出力される.
  • いずれかの不正解の条件に当てはまった場合,不正解の種類が Wrong Answer [4] のように出力される.

実行するプログラムが複数の不正解の条件を満たした場合,表示される不正解の種類はそれらのうち 1 つのみである.

やりとりの例

採点プログラムのサンプルが読み込む入力の例と,それに対応する関数の呼び出しの例を以下に示す.

入力例 1

1
3
1 2
3

ラウンド 0 では以下のやりとりが行われる.

  • 参加者 (0, 0)X の値を回答せず,参加者 (1, 0)B_{1,0,0} = 0 を,参加者 (1, 1)B_{1,1,0} = 1 を,参加者 (1, 2)B_{1,2,0} = 2 を送る.
  • 参加者 (0, 1)X の値を回答せず,参加者 (1, 0)B_{1,0,1} = 3 を,参加者 (1, 1)B_{1,1,1} = 4 を,参加者 (1, 2)B_{1,2,1} = 5 を送る.
  • 参加者 (0, 2)X の値を回答せず,参加者 (1, 0)B_{1,0,2} = 6 を,参加者 (1, 1)B_{1,1,2} = 7 を,参加者 (1, 2)B_{1,2,2} = 8 を送る.

どの参加者も X の値を回答しなかったので,ゲームは次のラウンドに進む.

ラウンド 1 では以下のやりとりが行われる.

  • 参加者 (1, 0) は参加者 (0, 0) から B_{1,0,0} = 0 を,参加者 (0, 1) から B_{1,0,1} = 3 を,参加者 (0, 2) から B_{1,0,2} = 6 を受け取り,X = 3 であると回答する. X の値を回答したため,この時点でゲームは終了する.

正しい X の値を回答したため,ゲームは成功となる.このゲームで行われたラウンド数は 2 である.

この入力例は小課題 3, 4, 5, 6 の制約を満たす.

コンテストサイトからダウンロードできるファイルのうち,sample-01-in.txt は入力例 1 に対応する. また,コンテストサイトからダウンロードできるアーカイブの中には,さらなる入力例 sample-02-in.txt, sample-03-in.txt, sample-04-in.txt が存在する. sample-02-in.txt はすべての小課題の制約を満たし, sample-03-in.txt は小課題 3, 4, 5, 6 の制約を満たし, sample-04-in.txt は小課題 4, 5, 6 の制約を満たす.

D - カジノ (Casino)

実行時間制限: 5 sec / メモリ制限: 1024 MiB

Score: 100 points

Distributed Files

How to Submit to AtCoder

  • Include Azzurro.h and Bordeaux.h, and implement the functions specified in the statement.
  • Do not use standard input/output or file input/output.

Problem Statement

Azzurro and Bordeaux, a pair visiting a casino in Italy, decided to play a game proposed by the dealer Chiaro.

In this game, information is transmitted using an N \times N grid (N = 8)). The rows of the grid are numbered from 0 to N - 1 from top to bottom, and the columns of the grid are numbered from 0 to N - 1 from left to right. A cell with row number r and column number c is denoted by (r, c).

In this game, Azzurro and Bordeaux are isolated in separate rooms. They will play Q turns. The i-th turn (1 \leq i \leq Q) proceeds as follows.

  1. Azzurro receives from Chiaro an integer N, an integer L_i (1 \leq L_i \leq 51), a card on which a string S_i of length L_i consisting of 'A' and 'B' is written, and an N \times N grid whose cells are all colored white.
  2. Azzurro colors each of the N^2 cells either blue or red. He then hands the grid to Chiaro.
  3. Chiaro performs the following operations out of sight of both Azzurro and Bordeaux.
    1. He selects one path from (0, 0) to (N - 1, N - 1) by repeatedly moving only to an adjacent cell either down or to the right.
    2. For every cell on the path, if the cell is colored blue, he repaints it red; if it is colored red, he repaints it blue.
  4. Bordeaux receives from Chiaro a card with integers N and L_i written on it, along with the grid.
  5. Bordeaux writes a string of length L_i consisting of A and B on a sheet of paper. If the written string matches S_i, Azzurro and Bordeaux win.

Write programs which implement the strategies of Azzurro and Bordeaux to win this game. For the grading of this task, see Grading.

Implementation Details

You need to submit two files.

The first file is Azzurro.cpp. It should implement Azzurro's strategy. It should implement the following functions. The program should include Azzurro.h using the preprocessing directive #include.

  • std::vector<std::vector<int>> Azzurro(int N, int L, std::string S) This function is called Q times. The i-th call (1 \leq i \leq Q) corresponds to the procedures 1., 2.\ of the i-th turn of the game.
    • The parameter N is the integer N written on the card given to Azzurro in the procedure 1.\ of the i-th turn.
    • The parameter L is the integer L_i written on the card given to Azzurro in the procedure 1.\ of the i-th turn.
    • The parameter S is the string S_i written on the card given to Azzurro in the procedure 1.\ of the i-th turn.
    For each call to the function Azzurro, your program must return an N \times N two-dimensional array x, each of whose elements is either 0 or 1. If this condition is not satisfied, your program is judged as Wrong Answer [1].
    • If \texttt{x}[\texttt{r}][\texttt{c}] = 0 (0 \leq \texttt{r} \leq N - 1, 0 \leq \texttt{c} \leq N - 1), it indicates that the cell (\texttt{r}, \texttt{c}) is colored blue.
    • If \texttt{x}[\texttt{r}][\texttt{c}] = 1 (0 \leq \texttt{r} \leq N - 1, 0 \leq \texttt{c} \leq N - 1), it indicates that the cell (\texttt{r}, \texttt{c}) is colored red.

The second file is Bordeaux.cpp. It should implement Bordeaux's strategy. It should implement the following function. The program should include Bordeaux.h using the preprocessing directive #include.

  • std::string Bordeaux(int N, int L, std::vector<std::vector<int>> T) This function is called every time when Azzurro finishes painting the grid. This function is called Q times in total. The i-th call (1 \leq i \leq Q) corresponds to the procedures 4., 5.\ of the i-th turn of the game.
    • The parameter N is the integer N written on the card given to Bordeaux in the procedure 4.\ of the i-th turn.
    • The parameter L is the integer L_i written on the card given to Bordeaux in the procedure 4.\ of the i-th turn.
    • The parameter T is the N \times N two-dimensional array corresponding to the grid of cells given to Bordeaux in the procedure 4.\ of the i-th turn. The color of the cell (\texttt{r}, \texttt{c}) (0 \leq \texttt{r} \leq N - 10 \leq \texttt{c} \leq N - 1) is blue if \texttt{T[a][b]} = 0, and red if \texttt{T[a][b]} = 1.
    For each call to the function Bordeaux, your program must return a string s of length L_i consisting of 'A' and 'B'. If this condition is not satisfied, your program is judged as Wrong Answer [2].

Important Notices

  • Your program can implement other functions for internal use, or use global variables. Submitted files will be compiled with the grader, and become a single executable file. All global variables and internal functions should be declared in an unnamed namespace to avoid confliction with other files. When it is graded, it will be executed as two processes of Azzurro and Bordeaux. The process of Azzurro and the process of Bordeaux cannot share global variables.
  • Your program must not use the standard input and the standard output. Your program must not communicate with other files by any methods. However, your program may output debugging information to the standard error.

Compilation and Test Run

You can download an archive file from the contest webpage which contains the sample grader to test your program. The archive file also contains a sample source file of your program.

The sample grader is the file grader.cpp. In order to test your program, put grader.cppAzzurro.cppBordeaux.cppAzzurro.hBordeaux.h in the same directory, and run the following command to compile your programs.

g++ -std=gnu++20 -O2 -o grader grader.cpp Azzurro.cpp Bordeaux.cpp

Instead, you may run compile.sh contained in the archive file. In this case, run the following command to compile your programs.

./compile.sh

When the compilation succeeds, the executable file grader is generated.

Note that the actual grader is different from the sample grader. The sample grader will be executed as a single process, which will read input data from the standard input and write the results to the standard output.

In the actual judging grader, the path chosen by Chiaro is fixed in advance. That is, the path selected by Chiaro is determined before the functions Azzurro and Bordeaux in your submitted program are called.


Input for the Sample Grader

The sample grader reads the following data from the standard input.

Q N 
L_1 
S_1 
R_1 
L_2 
S_2 
R_2 
\vdots 
L_Q 
S_Q 
R_Q

Here, R_i (1 \leq i \leq Q) is a string of length 2(N - 1) consisting of exactly N - 1 occurrences of 'D' and N - 1 occurrences of 'R'. This string represents the path chosen by Chiaro in the i-th turn, which starts from (0, 0) and reaches (N - 1, N - 1) by repeatedly moving only to an adjacent cell either down or to the right. Specifically, starting from (0, 0), for each j = 1, 2, \cdots, 2(N - 1), if the j-th character of R_i is 'D', the move is to the adjacent cell below; if it is 'R', the move is to the adjacent cell to the right. Repeating this process results in reaching (N - 1, N - 1).

Output of the Sample Grader

The sample grader outputs the following information to the standard output (quotes for clarity).

  • If your program is judged as correct, it writes the value of L^{*} as "Accepted: 26". For the value of L^{*}, see Grading.
  • If your program is judged as any type of Wrong Answer, the sample grader writes its type as "Wrong Answer [1]".

If your program satisfies the conditions of several types of Wrong Answer, the sample grader reports only one of them. The sample grader may terminate the execution when one of the conditions for wrong answer is met.


Constraints

All the input data satisfy the following conditions.

  • 1 \leq Q \leq 30\,000.
  • N = 8.
  • 1 \leq L_i \leq 51 (1 \leq i \leq Q).
  • Q, L_i (1 \leq i \leq Q) are integers.
  • S_i (1 \leq i \leq Q) is a string of length L_i consisting of 'A' and 'B'.
  • R_i (1 \leq i \leq Q) is a string of length 2(N - 1) consisting of exactly N - 1 occurrences of 'D' and N - 1 occurrences of 'R'.

Grading

If your program is judged as any type of Wrong Answer [1] or Wrong Answer [2] (see Implementation Details), Time Limit Exceeded, Memory Limit Exceeded, or Runtime Error, in any testcase, your score is 0 points.

Otherwise, let L^{*} be the minimum of the following values for all test cases of this task. Your score is calculated as in the following table.

  • The maximum value of L such that Azzurro and Bordeaux win all the turns satisfying L_i \leq L. However, if they win all the turns in the test case, we set L = 51.

Sample Communication

Here is a sample input for the sample grader and corresponding function calls.

Sample Input 1

2 2
1
B
RD
3
ABB
DR

This sample input consists of Q \ (= 2) turns, and in each turn an N \times N grid (N = 2) is used. In this example, the first turn proceeds as follows.

  1. Azzurro colors (0, 1) and (1, 0) blue, and (0, 0) and (1, 1) red. He then hands the grid to Chiaro.
  2. Chiaro performs the following operations out of sight of Azzurro and Bordeaux.
    1. He selects the path (0, 0) \rightarrow (0, 1) \rightarrow (1, 1) as a path from (0, 0) to (N - 1, N - 1) by repeatedly moving only to an adjacent cell either down or to the right.
    2. For the three cells (0, 0), (0, 1), (1, 1) on this path, he changes their colors. As a result, the colors of (0, 0), (0, 1), (1, 1) become blue, red, and blue, respectively.
  3. Bordeaux can win this turn by writing "B" on the paper.

The second turn proceeds as follows.

  1. Azzurro colors all cells blue. He then hands the grid to Chiaro.
  2. Chiaro performs the following operations out of sight of Azzurro and Bordeaux.
    1. He selects the path (0, 0) \rightarrow (1, 0) \rightarrow (1, 1) as a path from (0, 0) to (N - 1, N - 1) by repeatedly moving only to an adjacent cell either down or to the right.
    2. For the three cells (0, 0), (1, 0), (1, 1) on this path, he changes their colors. As a result, the colors of (0, 0), (1, 0), (1, 1) all become red.
  3. Bordeaux can win this turn by writing "ABB" on the paper.

Note that this sample input does not satisfy the constraints of the problem. The file sample-01-in.txt, which can be downloaded from the contest site, corresponds to Sample Input 1. The file sample-02-in.txt, which can be downloaded from the contest site, is a sample input that satisfies the constraints.

配点: 100

配布ファイル

AtCoder での提出方法

  • Azzurro.h, Bordeaux.h を include し,問題文で指定された関数を実装してください.
  • 標準入出力やファイルへの入出力を使用しないでください.

問題文

イタリアのカジノを訪れた Azzurro と Bordeaux の 2 人組は,ディーラーの Chiaro に提案されたゲームを遊ぶことにした.

このゲームでは,NN 列 (N = 8) のマス目を介して情報を伝える.マス目の各行には上から順に 0 から N - 1 までの行番号が,各列には左から順に 0 から N - 1 までの列番号が付けられている.行番号が r であり,列番号が c であるマスを (r, c) と表記する.

このゲームでは,Azzurro と Bordeaux が別々の部屋に隔離された状態で Q 回のターンが行われる.i 回目 (1 \leqq i \leqq Q) のターンは次のように進行する.

  1. Azzurro は Chiaro から,整数 N, L_i (1 \leqq L_i \leqq 51) および 'A' と 'B' からなる L_i 文字の文字列 S_i が書かれたカードと,すべてのマスが白色で塗られた NN 列のマス目を受け取る.
  2. Azzurro は,N^2 個のマスについて,各マスを青色か赤色で塗る.その後,Chiaro にマス目を渡す.
  3. Chiaro は,以下の操作を Azzurro と Bordeaux から見えない場所で行う.
    1. 下または右に隣接するマスへの移動のみを繰り返して (0, 0) から (N - 1, N - 1) まで到達する経路を 1 つ選ぶ.
    2. 経路上にあるすべてのマスについて,そのマスが青色で塗られているならば赤色で塗り直し,赤色で塗られているならば青色で塗り直す.
  4. Bordeaux は Chiaro から,整数 N, L_i が書かれたカードとマス目を受け取る.
  5. Bordeaux は 'A' と 'B' からなる L_i 文字の文字列を紙に書く.書いた文字列が S_i と一致していれば,Azzurro と Bordeaux の勝利となる.

Azzurro と Bordeaux がこのゲームで勝利するための戦略を実装せよ.なお,この課題の採点方法については,採点基準の項を参照すること.

実装の詳細

あなたは 2 つのファイルを提出しなければならない.

1 つ目のファイルは Azzurro.cpp という名前である.このファイルは Azzurro の戦略を実装したファイルであり,以下の関数を実装していなければならない.また,#include プリプロセッサ指令によって Azzurro.h を読み込むこと.

  • std::vector<std::vector<int>> Azzurro(int N, int L, std::string S) この関数は合計 Q 回呼び出される.i 回目 (1 \leqq i \leqq Q) の呼び出しは,ゲームにおける i 回目のターンの手順 1.,手順 2. に相当する.
    • 引数 Ni 回目のターンの手順 1. で Azzurro が受け取るカードに書かれた整数 N である.
    • 引数 Li 回目のターンの手順 1. で Azzurro が受け取るカードに書かれた整数 L_i である.
    • 引数 Si 回目のターンの手順 1. で Azzurro が受け取るカードに書かれた文字列 S_i である.
    関数 Azzurro1 回の呼び出しについて,各要素が 0 または 1 である N \times N2 次元配列 \texttt{x} を返さなければならない.これが満たされない場合,不正解 [1] と判定される.
    • \texttt{x}[\texttt{r}][\texttt{c}] = 0 (0 \leqq \texttt{r} \leqq N - 10 \leqq \texttt{c} \leqq N - 1) のとき,マス (\texttt{r}, \texttt{c}) を青色で塗ることを表す.
    • \texttt{x}[\texttt{r}][\texttt{c}] = 1 (0 \leqq \texttt{r} \leqq N - 10 \leqq \texttt{c} \leqq N - 1) のとき,マス (\texttt{r}, \texttt{c}) を赤色で塗ることを表す.

2 つ目のファイルは Bordeaux.cpp という名前である.このファイルは Bordeaux の戦略を実装したファイルであり,以下の関数を実装していなければならない.また,#include プリプロセッサ指令によって Bordeaux.h を読み込むこと.

  • std::string Bordeaux(int N, int L, std::vector<std::vector<int>> T) この関数は Azzurro がマス目を塗り終わるたびに 1 回,合計で Q 回呼び出される.i 回目 (1 \leqq i \leqq Q) の呼び出しは,ゲームにおける i 回目のターンの手順 4.,手順 5. に相当する.
    • 引数 N は,i 回目のターンの手順 4. で Bordeaux が受け取るカードに書かれた整数 N である.
    • 引数 L は,i 回目のターンの手順 4. で Bordeaux が受け取るカードに書かれた整数 L_i である.
    • 引数 T は,i 回目のターンの手順 4. で Bordeaux が受け取るマス目の各マスの色を表す N \times N2 次元配列である.マス (\texttt{r}, \texttt{c}) (0 \leqq \texttt{r} \leqq N - 10 \leqq \texttt{c} \leqq N - 1) の色は,\texttt{T[r][c]} = 0 であれば青色,\texttt{T[r][c]} = 1 であれば赤色である.
    関数 Bordeaux1 回の呼び出しについて,'A' と 'B' からなる L_i 文字の文字列 s を返さなければならない.これが満たされない場合,不正解 [2] と判定される.

重要な注意

  • 内部での使用のために他の関数を実装したり,グローバル変数を宣言するのは自由である. ただし,提出された 2 つのプログラムは,採点プログラムとまとめてリンクされて 1 つの実行ファイルになるので, 各ファイル内のすべてのグローバル変数と内部関数を無名名前空間内で宣言して,他のファイルとの干渉を避ける必要がある. 採点時には,このプログラムは Azzurro 側,Bordeaux 側として 2 個のプロセスとして実行されるので, Azzurro 側と Bordeaux 側でプログラム中のグローバル変数を共有することはできない.
  • あなたの提出したプログラムは,標準入力・標準出力,あるいは他のファイルといかなる方法でもやりとりしてはならない. ただし,標準エラー出力にデバッグ情報等を出力することは許される.

コンパイル・実行の方法

作成したプログラムをテストするための,採点プログラムのサンプルが,コンテストサイトからダウンロードできるアーカイブの中に含まれている.このアーカイブには,提出しなければならないファイルのサンプルも含まれている.

採点プログラムのサンプルは 1 つのファイルからなる.そのファイルは grader.cpp である.作成したプログラムをテストするには,grader.cppAzzurro.cppBordeaux.cppAzzurro.hBordeaux.h を同じディレクトリに置き,次のようにコマンドを実行する.

g++ -std=gnu++20 -O2 -o grader grader.cpp Azzurro.cpp Bordeaux.cpp

なお,アーカイブの中に含まれている compile.sh というファイルを代わりに実行してもよい.その場合,次のようにコマンドを実行する.

./compile.sh

コンパイルが成功すれば,grader という実行ファイルが生成される.

実際の採点プログラムは,採点プログラムのサンプルとは異なることに注意すること.採点プログラムのサンプルは単一のプロセスとして起動する.このプログラムは,標準入力から入力を読み込み,標準出力に結果を出力する.

なお,実際の採点プログラムにおいて,Chiaro の選ぶ経路はあらかじめ定まっている.すなわち,あなたの提出したプログラムにおける関数 Azzurro や関数 Bordeaux が呼び出される前に,Chiaro の選ぶ経路は確定している.

採点プログラムのサンプルの入力

採点プログラムのサンプルは標準入力から以下の形式で入力を読み込む.

Q N 
L_1 
S_1 
R_1 
L_2 
S_2 
R_2 
\vdots 
L_Q 
S_Q 
R_Q

ここで,R_i (1 \leqq i \leqq Q) は,'D' と 'R' を N - 1 文字ずつ含む 2(N - 1) 文字の文字列である.この文字列は Chiaro が i 回目のターンで選ぶ,下または右に隣接するマスへの移動のみを繰り返して (0, 0) から (N - 1, N - 1) まで到達する経路を表す.その経路は,(0, 0) からスタートして,j = 1, 2, \cdots , 2(N - 1) の順に,R_ij 文字目が 'D' であれば下に隣接するマスに,'R' であれば右に隣接するマスに移動する,という操作を繰り返すことで最終的に (N - 1, N - 1) に到達する経路である.

採点プログラムのサンプルの出力

採点プログラムのサンプルは標準出力へ以下の情報を出力する(引用符は実際には出力されない).

  • 正解の場合,L^{*} の値が "Accepted: 26" のように出力される.L^{*} の値については採点基準の項を参照せよ.
  • 不正解の場合,不正解の種類が "Wrong Answer [1]" のように出力される.

実行するプログラムが複数の不正解の条件を満たした場合,表示される不正解の種類はそれらのうち 1 つのみである.採点プログラムのサンプルは,不正解の条件を満たした場合,途中で実行を打ち切ることがある.


制約

すべての入力データは以下の条件を満たす.

  • 1 \leqq Q \leqq 30\,000
  • N = 8
  • 1 \leqq L_i \leqq 51 (1 \leqq i \leqq Q).
  • Q, L_i (1 \leqq i \leqq Q) は整数である.
  • S_i (1 \leqq i \leqq Q) は 'A' と 'B' からなる L_i 文字の文字列である.
  • R_i (1 \leqq i \leqq Q) は 'D' と 'R' を N - 1 文字ずつ含む 2(N - 1) 文字の文字列である.

採点基準

この課題のテストケースの中で,1 つでも不正解 [1] または不正解 [2](実装の詳細を参照)と判定されたものや,実行時エラー(実行時間制限超過,メモリ制限違反,異常終了など)と判定されたものがあった場合,他のテストケースでどのターンに勝利したかにかかわらず無条件で 0 点となる.

そうでない場合,この課題のすべてのテストケースに対する以下の値の最小値を L^{*} とするとき,下表のように得点が与えられる.

  • L_i \leqq L を満たすすべてのターンについて勝利したような最大の整数 L.ただし,テストケース内のすべてのターンに勝利した場合は L = 51 とする.

やりとりの例

採点プログラムのサンプルが読み込む入力の例と,それに対応する関数の呼び出しの例を以下に示す.

入力例 1

2 2
1
B
RD
3
ABB
DR

この入力例は Q \ (= 2) 回のターンからなり,2 回のターンでは NN 列 (N = 2) のマス目を使用する.この例では,1 回目のターンは次のように進行する.

  1. Azzurro は (0, 1)(1, 0) を青色に,(0, 0)(1, 1) を赤色に塗る.その後,Chiaro にマス目を渡す.
  2. Chiaro は,以下の操作を Azzurro と Bordeaux から見えない場所で行う.
    1. 下または右に隣接するマスへの移動のみを繰り返して (0, 0) から (N - 1, N - 1) まで到達する経路として,(0, 0) \rightarrow (0, 1) \rightarrow (1, 1) を選ぶ.
    2. この経路上にある 3 つのマス (0, 0), (0, 1), (1, 1) について,そのマスに塗られた色を変更する.これにより,(0, 0), (0, 1), (1, 1) の色はそれぞれ青色,赤色,青色に変更される.
  3. Bordeaux は "B" と紙に書くことで,このターンでは勝利できる.

また,2 回目のターンは次のように進行する.

  1. Azzurro はすべてのマスを青色に塗る.その後,Chiaro にマス目を渡す.
  2. Chiaro は,以下の操作を Azzurro と Bordeaux から見えない場所で行う.
    1. 下または右に隣接するマスへの移動のみを繰り返して (0, 0) から (N - 1, N - 1) まで到達する経路として,(0, 0) \rightarrow (1, 0) \rightarrow (1, 1) を選ぶ.
    2. この経路上にある 3 つのマス (0, 0), (1, 0), (1, 1) について,そのマスに塗られた色を変更する.これにより,(0, 0), (1, 0), (1, 1) の色はすべて赤色に変更される.
  3. Bordeaux は "ABB" と紙に書くことで,このターンでは勝利できる.

この入力例は制約を満たさないことに注意すること.コンテストサイトからダウンロードできるファイルのうち,sample-01-in.txt は入力例 1 に対応する.コンテストサイトからダウンロードできるファイルのうち,sample-02-in.txtは制約を満たす.

E - JOI ツアー 2 (JOI Tour 2)

実行時間制限: 7 sec / メモリ制限: 1024 MiB

Score: 100 points

Problem Statement

There are N towns in the country of JOI, numbered from 1 to N. Also, there are N-1 roads in the country of JOI, numbered from 1 to N-1. Road j (1 \leq j \leq N-1) connects town U_j and town V_j in both directions. It is possible to travel from any town to any other town by using some number of roads.

There is one shop in each town in the country of JOI. In the shop in town i (1 \leq i \leq N), a souvenir is sold for price A_i.

This year, M tours are planned in the country of JOI. The k-th tour (1 \leq k \leq M) starts from town S_k and travels by roads to town T_k without visiting the same town twice. Note that the k-th tour visits both towns S_k and T_k. It is guaranteed that S_k \neq T_k. Note that, from the structure of the country of JOI, the sequence of towns visited by a tour is uniquely determined.

You are planning to participate in one of these tours and buy one souvenir in each of exactly two of the towns visited on the tour. Moreover, you want to use up exactly the entire budget prepared for souvenirs, so for each of Q candidate budgets, you decided to investigate in how many ways this can be done.

Given the roads in the country of JOI, the prices of the souvenirs, the information on the tours, and the candidate budgets B_1, B_2, \ldots, B_Q, write a program that computes the number of ways to choose a tour and the towns in which to buy souvenirs. More formally, for each q (1 \leq q \leq Q), write a program that computes the number of triples of integers (k,u,v) satisfying all of the following conditions.

  • 1\leq k\leq M.
  • 1\leq u < v \leq N.
  • The k-th tour visits towns u and v.
  • A_u+A_v=B_q.

Input

Read the following data from the standard input.

N
A_1 A_2 \cdots A_N
U_1 V_1
U_2 V_2
\vdots
U_{N-1} V_{N-1}
M
S_1 T_1
S_2 T_2
\vdots
S_M T_M
Q
B_1 B_2 \cdots B_Q

Output

Write Q lines to the standard output. The q-th line (1\leq q\leq Q) should contain the number of ways to choose a tour and the towns in which to buy souvenirs so that the budget B_q is used up exactly.


Constraints

  • 2 \leq N \leq 100\,000.
  • 1\leq A_i\leq N (1 \leq i \leq N).
  • 1\leq U_j\leq N (1 \leq j \leq N-1).
  • 1\leq V_j\leq N (1 \leq j \leq N-1).
  • It is possible to travel from any town to any other town by using some number of roads.
  • 1\leq M \leq 200\,000.
  • 1\leq S_k\leq N (1\leq k\leq M).
  • 1\leq T_k\leq N (1\leq k\leq M).
  • S_k\neq T_k (1\leq k\leq M).
  • 1\leq Q\leq 2\,000.
  • 1\leq B_1 < B_2 < \cdots < B_Q\leq 2N.
  • All input values are integers.

Subtasks

  1. (3 points) N\leq 100, M\leq 100, Q\leq 100.
  2. (4 points) N\leq 5\,000, U_j=j (1\leq j\leq N-1), V_j=j+1 (1\leq j\leq N-1).
  3. (5 points) N\leq 5\,000.
  4. (6 points) Q= 1, U_j=j (1\leq j\leq N-1), V_j=j+1 (1\leq j\leq N-1).
  5. (10 points) Q= 1.
  6. (7 points) M\leq 1000, U_j=j (1\leq j\leq N-1), V_j=j+1 (1\leq j\leq N-1).
  7. (12 points) M\leq 1000.
  8. (10 points) N\leq 50\,000, M\leq 50\,000, U_j=j (1\leq j\leq N-1), V_j=j+1 (1\leq j\leq N-1).
  9. (15 points) N\leq 50\,000, M\leq 50\,000.
  10. (11 points) U_j=j (1\leq j\leq N-1), V_j=j+1 (1\leq j\leq N-1).
  11. (17 points) No additional constraints.

Sample Input 1

8
1 2 3 2 1 2 3 2
2 3
7 8
4 3
1 2
7 3
2 5
6 1
4
1 4
1 6
2 5
3 8
7
1 2 3 4 5 6 16

Sample Output 1

0
0
4
2
4
1
0

First, the towns visited by each tour are as follows.

  • The 1st tour visits towns 1, 2, 3, 4.
  • The 2nd tour visits towns 1, 6.
  • The 3rd tour visits towns 2, 5.
  • The 4th tour visits towns 3, 7, 8.

Represent a method of participating in the k-th tour and buying souvenirs in towns u and v by (k,u,v). Then, for each candidate budget, the ways to use up the budget exactly are as follows.

  • There are 0 ways to use up budget 1.
  • There are 0 ways to use up budget 2.
  • There are 4 ways to use up budget 3: (1,1,2), (1,1,4), (2,1,6), (3,2,5).
  • There are 2 ways to use up budget 4: (1,1,3), (1,2,4).
  • There are 4 ways to use up budget 5: (1,2,3), (1,3,4), (4,3,8), (4,7,8).
  • There is 1 way to use up budget 6: (4,3,7).
  • There are 0 ways to use up budget 16.

This input example satisfies the constraints of subtasks 1, 3, 7, 9, 11.


Sample Input 2

8
8 2 3 6 1 4 1 7
1 2
2 3
3 4
4 5
5 6
6 7
7 8
1
1 8
5
2 4 5 10 15

Sample Output 2

1
2
3
3
1

This input example satisfies the constraints of subtasks 1, 2, 3, 6, 7, 8, 9, 10, 11.

配点: 100

問題文

JOI 国には N 個の街があり,1 から N までの番号が付けられている.また,JOI 国には N-1 本の道路があり,1 から N-1 までの番号が付けられている. 道路 j (1\leqq j\leqq N-1) は街 U_j と街 V_j を双方向に結んでいる.どの街からどの街へも何本かの道路を通ることによって移動することができる.

JOI 国のそれぞれの街には店が 1 つあり,街 i (1\leqq i\leqq N) の店ではお土産を値段 A_i で売っている.

JOI 国では今年,M 個のツアーが計画されている. k 番目 (1\leqq k\leqq M) のツアーは,街 S_k を出発し,街 T_k まで同じ街を 2 回訪れることなく道路を通って移動するものである.ただし k 番目のツアーは街 S_k, T_k も訪れる.また,S_k\neq T_k であることが保証される. JOI 国の構造から,ツアーがどの街を訪れるかが 1 通りに定まることに注意せよ.

あなたは,これらのツアーのうちひとつに参加して,訪れる街のうちちょうど 2 つの街でお土産を 1 つずつ購入することを計画している. さらに,お土産のために用意した予算をちょうど使い切るようにしたいと考えているため,Q 通りの予算の候補についてそのような方法が何通りあるのかを調べることにした.

JOI 国の道路とお土産の値段,ツアーの情報,および予算の候補 B_1, B_2, \ldots, B_Q が与えられたとき, ツアーおよびお土産を購入する街を選ぶ方法が何通りあるかを求めるプログラムを作成せよ. より形式的には各 q (1\leqq q\leqq Q) について,整数の組 (k,u,v) であって以下の条件をすべて満たすものの個数を求めるプログラムを作成せよ.

  • 1\leqq k\leqq M
  • 1\leqq u < v \leqq N
  • k 番目のツアーは街 u, v を訪れる.
  • A_u+A_v=B_q

入力

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

N
A_1 A_2 \cdots A_N
U_1 V_1
U_2 V_2
\vdots
U_{N-1} V_{N-1}
M
S_1 T_1
S_2 T_2
\vdots
S_M T_M
Q
B_1 B_2 \cdots B_Q

出力

標準出力に Q 行出力せよ.q 行目 (1\leqq q\leqq Q) には,予算 B_q をちょうど使い切るようにツアーおよびお土産を購入する街を選ぶ方法が何通りあるかを出力せよ.


制約

  • 2 \leqq N \leqq 100\,000
  • 1\leqq A_i\leqq N (1 \leqq i \leqq N).
  • 1\leqq U_j\leqq N (1 \leqq j \leqq N-1).
  • 1\leqq V_j\leqq N (1 \leqq j \leqq N-1).
  • どの 2 つの街の間も,いくつかの道路を経由して移動することができる.
  • 1\leqq M \leqq 200\,000
  • 1\leqq S_k\leqq N (1\leqq k\leqq M).
  • 1\leqq T_k\leqq N (1\leqq k\leqq M).
  • S_k\neq T_k (1\leqq k\leqq M).
  • 1\leqq Q\leqq 2\,000
  • 1\leqq B_1 < B_2 < \cdots < B_Q\leqq 2N
  • 入力される値はすべて整数である.

小課題

  1. (3 点) N\leqq 100M\leqq 100Q\leqq 100
  2. (4 点) N\leqq 5\,000U_j=j (1\leqq j\leqq N-1),V_j=j+1 (1\leqq j\leqq N-1).
  3. (5 点) N\leqq 5\,000
  4. (6 点) Q= 1U_j=j (1\leqq j\leqq N-1),V_j=j+1 (1\leqq j\leqq N-1).
  5. (10 点) Q= 1
  6. (7 点) M\leqq 1000U_j=j (1\leqq j\leqq N-1),V_j=j+1 (1\leqq j\leqq N-1).
  7. (12 点) M\leqq 1000
  8. (10 点) N\leqq 50\,000, M\leqq 50\,000U_j=j (1\leqq j\leqq N-1),V_j=j+1 (1\leqq j\leqq N-1).
  9. (15 点) N\leqq 50\,000, M\leqq 50\,000
  10. (11 点) U_j=j (1\leqq j\leqq N-1),V_j=j+1 (1\leqq j\leqq N-1).
  11. (17 点) 追加の制約はない.

入力例 1

8
1 2 3 2 1 2 3 2
2 3
7 8
4 3
1 2
7 3
2 5
6 1
4
1 4
1 6
2 5
3 8
7
1 2 3 4 5 6 16

出力例 1

0
0
4
2
4
1
0

まずそれぞれのツアーが訪れる街は次の通りである.

  • 1 番目のツアーは街 1, 2, 3, 4 を訪れる.
  • 2 番目のツアーは街 1, 6 を訪れる.
  • 3 番目のツアーは街 2, 5 を訪れる.
  • 4 番目のツアーは街 3, 7, 8 を訪れる.

k 番目のツアーに参加して街 u, v でお土産を購入するという方法を (k,u,v) と表すとき,それぞれの予算の候補について,予算を使い切る方法は次の通りである.

  • 予算 1 を使い切る方法は 0 通りである.
  • 予算 2 を使い切る方法は 0 通りである.
  • 予算 3 を使い切る方法は (1,1,2), (1,1,4), (2,1,6), (3,2,5)4 通りである.
  • 予算 4 を使い切る方法は (1,1,3), (1,2,4)2 通りである.
  • 予算 5 を使い切る方法は (1,2,3),(1,3,4),(4,3,8),(4,7,8)4 通りである.
  • 予算 6 を使い切る方法は (4,3,7)1 通りである.
  • 予算 16 を使い切る方法は 0 通りである.

この入力例は小課題 1, 3, 7, 9, 11 の制約を満たす.


入力例 2

8
8 2 3 6 1 4 1 7
1 2
2 3
3 4
4 5
5 6
6 7
7 8
1
1 8
5
2 4 5 10 15

出力例 2

1
2
3
3
1

この入力例は小課題 1, 2, 3, 6, 7, 8, 9, 10, 11 の制約を満たす.

F - テレポーター 2 (Teleporter 2)

実行時間制限: 3.5 sec / メモリ制限: 1024 MiB

Score: 100 points

Problem Statement

There are N points on a straight path, numbered 1, 2, \ldots, N from left to right. The path is one-way from left to right.

There are also M teleporter devices numbered 1, 2, \ldots, M. By using device i (1 \leq i \leq M), one can warp from point S_i to point T_i (S_i < T_i).

Bitaro is currently at point 1 and wants to reach point N. When Bitaro is at point j (1 \leq j \leq N-1), he can take one of the following actions:

  • Move on foot to point j+1.
  • Choose an i (1 \leq i \leq M) satisfying S_i = j, use device i, and warp to point T_i.

It is known that warp travel places stress on the body. You are concerned about Bitaro's safety, so you decide to destroy zero or more teleporter devices so that, no matter which route Bitaro takes, the number of warp travels is at most K. Device i can be destroyed by paying cost C_i; if so, Bitaro can no longer use that device.

Find the minimum possible total cost you need to pay when destroying zero or more teleporter devices so that, regardless of Bitaro's route, the number of warp travels is at most K.


Input

Read the following data from the standard input.

N M K
S_1 T_1 C_1
S_2 T_2 C_2
\vdots
S_M T_M C_M

Output

Write one line to the standard output containing the minimum possible total cost.


Constraints

  • 2 \leq N \leq 100\,000.
  • 1 \leq K \leq M \leq 100\,000.
  • 1 \leq S_i < T_i \leq N (1 \leq i \leq M).
  • 1 \leq C_i \leq 10^9 (1 \leq i \leq M).
  • Given values are all integers.

Subtasks

  1. (5 points) K=1.
  2. (3 points) N \leq 20, M \leq 20.
  3. (29 points) N \leq 500, M \leq 500.
  4. (23 points) N \leq 4\,000, M \leq 4\,000.
  5. (24 points) N \leq 40\,000, M \leq 40\,000.
  6. (16 points) No additional constraints.

Sample Input 1

8 4 1
1 4 3
2 3 5
3 6 2
5 8 2

Sample Output 1

4

Consider the case where devices 3 and 4 are destroyed.

Then Bitaro can use only devices 1 and 2. When moving from point 1 to point 8, Bitaro will always make at most one warp travel, so the condition is satisfied.

In this case, the total paid cost is 4. Since it is impossible to make the cost at most 3, the correct output is 4.

This input example satisfies the constraints of all subtasks.


Sample Input 2

12 7 2
1 5 3
4 8 2
2 4 5
2 4 8
7 9 4
9 11 7
3 10 5

Sample Output 2

6

Destroying devices 2 and 5 is optimal.

This input example satisfies the constraints of subtasks 2,3,4,5,6.


Sample Input 3

6 3 2
1 4 2
2 5 4
3 6 3

Sample Output 3

0

In this case, there is no need to destroy any device.

This input example satisfies the constraints of subtasks 2,3,4,5,6.

配点 : 100

問題文

一本道に N 個の地点があり,左から順に 1, 2, \ldots, N の番号が付けられている.道は左から右への一方通行である.

また,1, 2, \ldots, M の番号が付いた M 個のテレポーター装置がある. 装置 i (1 \leqq i \leqq M) を使用すると地点 S_i から地点 T_i (S_i < T_i) にワープ移動することができる.

ビ太郎は現在地点 1 におり,地点 N に向かおうとしている. ビ太郎が地点 j (1 \leqq j \leqq N-1) にいるときにとることができる行動は次のいずれかである.

  • 地点 j+1 に歩いて移動する.
  • S_i=j を満たす i (1 \leqq i \leqq M) を選ぶ.装置 i を使用し,地点 T_i にワープ移動する.

ワープ移動をすると体に負担がかかることが知られている. ビ太郎の体の安全について心配するあなたは,ビ太郎がどのような経路をとったとしてもワープ移動の回数が K 以下となるよう,0 個以上のテレポーター装置を破壊することにした. テレポーター装置 i はコスト C_i を支払うことで破壊することができ,その場合ビ太郎はその装置を使用することが不可能となる.

ビ太郎がどのような経路をとったとしてもワープ移動の回数が K 以下となるように 0 個以上のテレポーター装置を破壊するとき,支払うコストの総和としてありうる最小値を求めよ.


入力

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

N M K
S_1 T_1 C_1
S_2 T_2 C_2
\vdots
S_M T_M C_M

出力

標準出力に,支払うコストの総和としてありうる最小値を 1 行で出力せよ.


制約

  • 2 \leqq N \leqq 100\,000
  • 1 \leqq K \leqq M \leqq 100\,000
  • 1 \leqq S_i < T_i \leqq N (1 \leqq i \leqq M).
  • 1 \leqq C_i \leqq 10^9 (1\leqq i \leqq M).
  • 入力される値はすべて整数である.

小課題

  1. (5 点) K=1
  2. (3 点) N\leqq 20M\leqq 20
  3. (29 点) N\leqq 500M\leqq 500
  4. (23 点) N\leqq 4\,000M\leqq 4\,000
  5. (24 点) N\leqq 40\,000M\leqq 40\,000
  6. (16 点) 追加の制約はない.

入力例 1

8 4 1
1 4 3
2 3 5
3 6 2
5 8 2

出力例 1

4

装置 3, 4 を破壊した場合を考える.

ビ太郎が使用可能な装置は 1, 2 のみとなる.地点 1 から地点 8 に移動するときにビ太郎がワープ移動をする回数は必ず 1 回以下となるため,条件を満たす.

このとき,支払うコストの総和は 4 である. コストを 3 以下にすることはできないので,4 を出力する.

この入力例はすべての小課題の制約を満たす.


入力例 2

12 7 2
1 5 3
4 8 2
2 4 5
2 4 8
7 9 4
9 11 7
3 10 5

出力例 2

6

装置 2, 5 を破壊するのが最適である.

この入力例は小課題 2,3,4,5,6 の制約を満たす.


入力例 3

6 3 2
1 4 2
2 5 4
3 6 3

出力例 3

0

この場合,装置を破壊する必要はない.

この入力例は小課題 2,3,4,5,6 の制約を満たす.

G - 三角形降雨 (Triangular Rainfall)

実行時間制限: 4 sec / メモリ制限: 1024 MiB

Score: 100 points

Problem Statement

The country of JOI is an equilateral triangle with side length L whose vertices are points A, B, and C. Here, L is a positive integer. Side AB connects vertices A and B in the east-west direction, and vertex A is the westernmost point of the country of JOI, while vertex B is the easternmost point. Vertex C is the northernmost point of the country of JOI.

The country of JOI is divided into L^2 regions, each of which is an equilateral triangle with side length 1. A point that is a vertex of some region is called a lattice point. For integers x, y satisfying 0\leq y\leq L and 0\leq x\leq L-y, the lattice point that is the (1+y)-st from the south and the (1+x)-st from the west is denoted by (x,y). In particular, A, B, and C are denoted by (0,0), (L,0), and (0,L), respectively. For example, the following figure shows the regions and the lattice points when L=5.

In the country of JOI, weather forecasts for the next N days have been announced. On day i, rain is forecast to fall in the triangular region T_i whose vertices are lattice points (X_i, Y_i), (X_i + Z_i, Y_i), and (X_i, Y_i + Z_i). An region is said to be forecast to receive rain on day i if the entire region is contained in T_i.

In order to prepare for disasters caused by rainfall, it is necessary to determine, for each k=1,2,\ldots,K, the number of regions that are forecast to receive rain on at least k days.

Given the size of the country of JOI, the weather forecasts, and K, write a program which, for each k=1,2,\ldots,K, computes the number of regions that are forecast to receive rain on at least k days.


Input

Input is given from Standard Input in the following format:

L N K
X_1 Y_1 Z_1
X_2 Y_2 Z_2
\vdots
X_N Y_N Z_N

Output

Print K lines to Standard Output. The k-th line (1\leq k\leq K) should contain the number of regions that are forecast to receive rain on at least k days.


Constraints

  • 2\leq L\leq 10^9.
  • 2 \leq N \leq 200\,000.
  • 1 \leq K \leq 5.
  • 0\leq X_i\leq L (1\leq i\leq N).
  • 0\leq Y_i\leq L (1\leq i\leq N).
  • 1 \leq Z_i\leq L (1\leq i\leq N).
  • X_i+Y_i+Z_i\leq L (1\leq i\leq N).
  • All input values are integers.

Subtasks

  1. (4 points) N = 2, K = 2.
  2. (5 points) L\leq 100, N\leq 100.
  3. (5 points) L\leq 1\,000.
  4. (7 points) N\leq 2\,000.
  5. (10 points) X_i = 0 (1\leq i\leq N), K = 1.
  6. (10 points) X_i = 0 (1\leq i\leq N).
  7. (23 points) K = 1.
  8. (18 points) K \leq 2.
  9. (18 points) There are no additional constraints.

Sample Input 1

5 2 2
1 0 3
0 1 4

Sample Output 1

21
4

If we illustrate, for each region, the number of days on which rain is forecast to fall, we obtain the following figure.

This sample input satisfies the constraints for Subtasks 1, 2, 3, 4, 8, and 9.


Sample Input 2

5 4 5
1 0 4
0 1 3
2 0 2
1 2 2

Sample Output 2

21
10
2
0
0

If we illustrate, for each region, the number of days on which rain is forecast to fall, we obtain the following figure.

This sample input satisfies the constraints for Subtasks 2, 3, 4, and 9.

配点: 100

問題文

JOI 国は,点 A, B, C を頂点とする一辺の長さが L の正三角形である.ここで L は正整数である.辺 AB は頂点 A, B を東西方向に結んでおり,頂点 A は JOI 国の最西端,頂点 B は JOI 国の最東端である.頂点 C は JOI 国の最北端である.

JOI 国は,一辺の長さが 1 の正三角形の区画 L^2 個に区切られている.ある区画の頂点であるような点は格子点と呼ばれている.0\leqq y\leqq L0\leqq x\leqq L-y を満たす整数 x,y に対して,南側から 1+y 番目,西側から 1+x 番目の格子点は (x,y) と表される.特に A(0,0)B(L,0)C(0,L) と表される.例えば次の図は L=5 の場合の区画,格子点を表している.

JOI 国ではこれから N 日の間の天気予報が発表された. i 日目には,格子点 (X_i, Y_i), (X_i + Z_i, Y_i), (X_i, Y_i + Z_i) を頂点とする正三角形状の地域 T_i に雨が降ると予報されている. i 日目に雨が降ると予報されている区画とは,区画全体が T_i に含まれるような区画のことである.

降雨による災害に備えるため,k=1,2,\ldots,K について,k 日以上雨が降ると予報されている区画の個数を調べる必要がある.

JOI 国の大きさ,天気予報の情報,K が与えられたときに,k=1,2,\ldots,K について,k 日以上雨が降ると予報されている区画の個数を求めるプログラムを作成せよ.


入力

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

L N K
X_1 Y_1 Z_1
X_2 Y_2 Z_2
\vdots
X_N Y_N Z_N

出力

標準出力に K 行出力せよ.k 行目 (1\leqq k\leqq K) には,k 日以上雨が降ると予報されている区画の個数を出力せよ.


制約

  • 2\leqq L\leqq 10^9
  • 2 \leqq N \leqq 200\,000
  • 1 \leqq K \leqq 5
  • 0\leqq X_i\leqq L (1\leqq i\leqq N).
  • 0\leqq Y_i\leqq L (1\leqq i\leqq N).
  • 1 \leqq Z_i\leqq L (1\leqq i\leqq N).
  • X_i+Y_i+Z_i\leqq L (1\leqq i\leqq N).
  • 入力される値はすべて整数である.

小課題

  1. (4 点) N = 2K = 2
  2. (5 点) L\leqq 100N\leqq 100
  3. (5 点) L\leqq 1\,000
  4. (7 点) N\leqq 2\,000
  5. (10 点) X_i = 0 (1\leqq i\leqq N),K = 1
  6. (10 点) X_i = 0 (1\leqq i\leqq N).
  7. (23 点) K = 1
  8. (18 点) K \leqq 2
  9. (18 点) 追加の制約はない.

入力例 1

5 2 2
1 0 3
0 1 4

出力例 1

21
4

それぞれの区画について,雨が降ると予報されている日数を図示すると,次の図のようになる.

この入力例は小課題 1, 2, 3, 4, 8, 9 の制約を満たす.


入力例 2

5 4 5
1 0 4
0 1 3
2 0 2
1 2 2

出力例 2

21
10
2
0
0

それぞれの区画について,雨が降ると予報されている日数を図示すると,次の図のようになる.

この入力例は小課題 2, 3, 4, 9 の制約を満たす.

H - かかし 2 (Scarecrows 2)

実行時間制限: 2.5 sec / メモリ制限: 1024 MiB

Score: 100 points

Problem Statement

There is a vast field in JOI Village. The field is represented by the infinite xy-plane, where the positive direction of the x-axis is East and the positive direction of the y-axis is North.

The mayor of JOI Village plans to place several scarecrows in the field in order to protect it from enemies. Each scarecrow protects a certain region of the plane depending on its position and direction.

Currently, N placement plans have been proposed, numbered from 1 to N. Executing plan i (1 \leq i \leq N) requires a cost of C_i. Each plan is described by three integers T_i, X_i, Y_i as follows.

  • If T_i = 1, a scarecrow is placed at the point (X_i, Y_i) facing West. This scarecrow protects all points on the plane satisfying x \leq X_i.
  • If T_i = 2, a scarecrow is placed at the point (X_i, Y_i) facing East. This scarecrow protects all points on the plane satisfying x \geq X_i.
  • If T_i = 3, a scarecrow is placed at the point (X_i, Y_i) facing South. This scarecrow protects all points on the plane satisfying y \leq Y_i.
  • If T_i = 4, a scarecrow is placed at the point (X_i, Y_i) facing North. This scarecrow protects all points on the plane satisfying y \geq Y_i.

The mayor wants to select and execute some of these N plans so that every point on the plane is protected by at least K scarecrows. Among all such choices, the total cost should be minimized. It is guaranteed that the coordinates (X_i, Y_i) are pairwise distinct among the N plans.

Given the information of the N plans, determine whether it is possible to select plans so that every point on the plane is protected by at least K scarecrows. If it is possible, output the minimum possible total cost of the selected plans.


Input

Read the following data from the standard input.

N K
T_1 X_1 Y_1 C_1
T_2 X_2 Y_2 C_2
\vdots
T_{N} X_{N} Y_{N} C_{N}

Output

Output the minimum total cost required so that every point on the plane is protected by at least K scarecrows. If there is no way to choose the plans so that the condition is satisfied, output -1.


Constraints

  • 1 \leq K \leq N \leq 200\,000
  • T_i is one of 1, 2, 3, or 4 (1 \leq i \leq N).
  • 0 \leq X_i \leq 10^9 (1 \leq i \leq N).
  • 0 \leq Y_i \leq 10^9 (1 \leq i \leq N).
  • (X_i, Y_i) \neq (X_j, Y_j) (1 \leq i < j \leq N).
  • 0 \leq C_i \leq 10^9 (1 \leq i \leq N).
  • Given values are all integers.

Subtasks

  1. (4 points) K = 1
  2. (6 points) K \leq 2
  3. (11 points) N \leq 500K \leq 300
  4. (27 points) N \leq 6\,000
  5. (19 points) N \leq 75\,000
  6. (33 points) No additional constraints.

Sample Input 1

7 1
2 45 21 96
1 5 85 70
1 36 73 78
1 28 12 80
2 15 49 21
1 45 11 96
2 63 26 19

Sample Output 1

99

For example, suppose that plans 3 and 5 are executed. Then the scarecrows are placed as follows.

  • In plan 3, one scarecrow is placed at the point (36,73) facing West. The cost is 78.
  • In plan 5, one scarecrow is placed at the point (15,49) facing East. The cost is 21.

In this case, every point on the coordinate plane is protected by at least one scarecrow. For example, the point (0,0) is protected by the scarecrow placed at (36,73) facing West in plan 3. The total cost is 78 + 21 = 99. It is impossible to protect every point on the plane with at least one scarecrow with a smaller total cost, so the output should be 99.

This sample input satisfies the constraints of all subtasks.


Sample Input 2

7 3
2 45 21 96
1 5 85 70
1 36 73 78
1 28 12 80
2 15 49 21
1 45 11 96
2 63 26 19

Sample Output 2

-1

This example differs from Sample Input 1 only in the value of K. Since it is impossible to protect every point on the coordinate plane with at least 3 scarecrows, the output should be -1.

This sample input satisfies the constraints of subtasks 3,4,5,6.


Sample Input 3

19 5
2 36 42 64
2 7 89 74
1 0 15 82
1 10 63 55
2 58 28 19
2 45 91 3
2 2 34 97
1 7 55 82
1 17 12 17
2 59 76 82
1 7 4 68
2 51 98 47
1 51 21 38
2 19 0 72
1 73 73 11
2 62 19 74
1 45 7 94
1 79 32 21
1 85 50 21

Sample Output 3

315

This sample input satisfies the constraints of subtasks 3,4,5,6.


Sample Input 4

8 3
4 4 21 80
2 59 65 69
4 63 36 3
2 29 13 23
1 37 45 95
2 79 14 89
3 91 54 76
1 85 46 62

Sample Output 4

328

This sample input satisfies the constraints of subtasks 3,4,5,6.

配点: 100

問題文

JOI 村には,広大な畑がある. この畑は無限に広がる xy 座標平面で表され,x 軸正の向きが東方向,y 軸正の向きが北方向である.

JOI 村の村長は,畑を外敵から守るために,畑にいくつかのかかしを配置しようと考えている. 配置されたそれぞれのかかしは,その場所と向きに応じて,平面上の特定の領域を守ることができる.

現在,かかしを配置するための N 個の計画が提案されており,1 から N までの番号が付けられている. 計画 i (1\leqq i\leqq N) を実行するために必要なコストは C_i であり,その内容は整数 T_i, X_i, Y_i を用いて以下のように表される.

  • T_i = 1 のとき,かかし 1 体を点 (X_i,Y_i) に西向きに配置する.このかかしは平面上の x \leqq X_i の領域を守る.
  • T_i = 2 のとき,かかし 1 体を点 (X_i,Y_i) に東向きに配置する.このかかしは平面上の x \geqq X_i の領域を守る.
  • T_i = 3 のとき,かかし 1 体を点 (X_i,Y_i) に南向きに配置する.このかかしは平面上の y \leqq Y_i の領域を守る.
  • T_i = 4 のとき,かかし 1 体を点 (X_i,Y_i) に北向きに配置する.このかかしは平面上の y \geqq Y_i の領域を守る.

村長は,これらの N 個の計画のうちいくつかを選んで実行することで,できるだけ少ない合計コストで,平面上のどの点も K 体以上のかかしによって守られているようにしたいと考えている.ただし,N 個の計画においてかかしを配置する点の座標はすべて相異なることが保証される.

かかしを配置する計画の情報が与えられたとき,いくつかの計画を選んで実行することで平面上のどの点も K 体以上のかかしによって守られているようにすることが可能かどうか判定し,可能な場合は実行する計画の合計コストの最小値を求めるプログラムを作成せよ.


入力

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

N K
T_1 X_1 Y_1 C_1
T_2 X_2 Y_2 C_2
\vdots
T_{N} X_{N} Y_{N} C_{N}

出力

平面上のどの点も K 体以上のかかしによって守られているように配置するために必要な合計コストの最小値を出力せよ. ただし,条件を満たす計画の選び方が存在しない場合は -1 を出力せよ.


制約

  • 1 \leqq K \leqq N \leqq 200\,000
  • T_i1, 2, 3, 4 のいずれかである (1 \leqq i \leqq N).
  • 0 \leqq X_i \leqq 10^9 (1 \leqq i \leqq N).
  • 0 \leqq Y_i \leqq 10^9 (1 \leqq i \leqq N).
  • (X_i, Y_i) \neq (X_j, Y_j) (1 \leqq i < j \leqq N).
  • 0 \leqq C_i \leqq 10^9 (1 \leqq i \leqq N).
  • 入力される値はすべて整数である.

小課題

  1. (4 点) K = 1
  2. (6 点) K \leqq 2
  3. (11 点) N \leqq 500K \leqq 300
  4. (27 点) N \leqq 6\,000
  5. (19 点) N \leqq 75\,000
  6. (33 点) 追加の制約はない.

入力例 1

7 1
2 45 21 96
1 5 85 70
1 36 73 78
1 28 12 80
2 15 49 21
1 45 11 96
2 63 26 19

出力例 1

99

例えば計画 3,5 を実行すると,以下のようにかかしが配置される.

  • 計画 3 では,かかし 1 体を点 (36,73) に西向きに配置する.コストは 78 である.
  • 計画 5 では,かかし 1 体を点 (15,49) に東向きに配置する.コストは 21 である.

このとき,座標平面上のどの点も 1 体以上のかかしで守られている.例えば,点 (0, 0) は計画 3 で点 (36,73) に西向きに配置したかかしによって守られている. また,合計コストは 78+21=99 である. これより少ない合計コストで平面上のすべての点を 1 体以上のかかしで守ることはできないので,99 を出力する.

この入力例はすべての小課題の制約を満たす.


入力例 2

7 3
2 45 21 96
1 5 85 70
1 36 73 78
1 28 12 80
2 15 49 21
1 45 11 96
2 63 26 19

出力例 2

-1

入力例 1 とは K の値のみが異なる.

座標平面上のすべての点を 3 体以上のかかしによって守ることはできないため,-1 を出力する.

この入力例は小課題 3,4,5,6 の制約を満たす.


入力例 3

19 5
2 36 42 64
2 7 89 74
1 0 15 82
1 10 63 55
2 58 28 19
2 45 91 3
2 2 34 97
1 7 55 82
1 17 12 17
2 59 76 82
1 7 4 68
2 51 98 47
1 51 21 38
2 19 0 72
1 73 73 11
2 62 19 74
1 45 7 94
1 79 32 21
1 85 50 21

出力例 3

315

この入力例は小課題 3,4,5,6 の制約を満たす.


入力例 4

8 3
4 4 21 80
2 59 65 69
4 63 36 3
2 29 13 23
1 37 45 95
2 79 14 89
3 91 54 76
1 85 46 62

出力例 4

328

この入力例は小課題 3,4,5,6 の制約を満たす.

I - スタンプラリー 5 (Collecting Stamps 5)

実行時間制限: 3 sec / メモリ制限: 1024 MiB

Score: 100 points

Problem Statement

There are N towns in the country of IOI where JOI-kun lives, numbered from 1 to N. Also, there are N-1 roads in the country of IOI, numbered from 1 to N-1. Road j (1 \leq j \leq N-1) connects town U_j and town V_j in both directions. It is possible to travel from any town to any other town by using some number of roads.

A stamp rally will now be held in the country of IOI. One stamp station will be installed in each town. The stamp station in town i (1 \leq i \leq N) will be installed at time T_i.

JOI-kun decides to participate in the stamp rally. At time 0, JOI-kun starts from one of the towns. Also, JOI-kun has stamina D at time 0.

When JOI-kun is in town i at time t, he takes the following actions.

  1. First, if a stamp station has already been installed in the current town, he presses the stamp. That is, he presses the stamp if T_i \leq t.
  2. Next, he chooses either to finish the stamp rally or to move to another town. However, he can choose to move to another town only if there exists an adjacent town connected to town i by a road that he has not visited yet, and his current stamina is at least 1.
  3. If JOI-kun chooses to move to another town, he chooses an unvisited town j among the towns connected to town i by a road, and moves there. His stamina decreases by 1, and he arrives at town j at time t+1.
  4. If JOI-kun chooses to finish the stamp rally, the rally is successful if he has pressed a stamp at least once up to that point, and he can receive a present there. Otherwise, the rally is unsuccessful.

Assume that all times other than travel time between towns are negligible. Note that JOI-kun cannot stay in the same town.

You are an event organizer. If JOI-kun succeeds in the stamp rally, you need to prepare presents in the corresponding towns. However, the number of presents is limited, so you want to prepare presents in as few towns as possible. Unfortunately, you do not know from which town JOI-kun will start. Therefore, for each s (1 \leq s \leq N), you want to find the number of towns in which presents must be prepared when JOI-kun starts from town s. In other words, you need to count the number of g (1 \leq g \leq N) such that it is possible for the stamp rally to be successful when JOI-kun finishes at town g.

Given the information about the towns and roads in the country of IOI, JOI-kun's stamina, and the installation times of the stamp stations, write a program that computes, for each town, the number of towns in which presents must be prepared when JOI-kun starts from that town.


Input

Read the following data from the standard input.

N D
T_1 T_2 \cdots T_N
U_1 V_1
U_2 V_2
\vdots
U_{N-1} V_{N-1}

Output

Write N lines to the standard output. The s-th line (1\leq s \leq N) should contain the number of towns in which presents must be prepared when JOI-kun starts from town s.


Constraints

  • 2 \leq N \leq 400\,000.
  • 0 \leq D \leq N-1.
  • 0 \leq T_i \leq N (1 \leq i \leq N).
  • 1 \leq U_j < V_j \leq N (1 \leq j \leq N-1).
  • It is possible to travel from any town to any other town by using some number of roads.
  • Given values are all integers.

Subtasks

  1. (3 points) D\leq 1.
  2. (7 points) N\leq 3\,000, (U_j,V_j)=(j,j+1) (1\leq j \leq N-1).
  3. (10 points) N\leq 3\,000.
  4. (11 points) (U_j,V_j)=(j,j+1) (1\leq j \leq N-1).
  5. (41 points) D=N-1, N \leq 150\,000.
  6. (28 points) No additional constraints.

Sample Input 1

5 2
2 2 0 1 3
1 2
2 3
2 4
4 5

Sample Output 1

2
3
4
2
2

We show one example of JOI-kun's actions when s=1.

  • At time 0, JOI-kun is in town 1 and takes the following actions.
    • The stamp station in town 1 has not been installed yet, so JOI-kun does not press a stamp.
    • JOI-kun's current stamina is 2. He moves to town 2, which is one of the unvisited towns connected to town 1 by a road.
    • JOI-kun's stamina decreases by 1, and he arrives at town 2 at time 1.
  • At time 1, JOI-kun is in town 2 and takes the following actions.
    • The stamp station in town 2 has not been installed yet, so JOI-kun does not press a stamp.
    • JOI-kun's current stamina is 1. He moves to town 3, which is one of the unvisited towns connected to town 2 by a road.
    • JOI-kun's stamina decreases by 1, and he arrives at town 3 at time 2.
  • At time 2, JOI-kun is in town 3 and takes the following actions.
    • The stamp station in town 3 has already been installed, so JOI-kun presses a stamp.
    • JOI-kun chooses to finish the stamp rally here. Since he has pressed a stamp at least once, the stamp rally is successful. He receives a present there.

Therefore, when JOI-kun starts from town 1 and finishes the stamp rally at town 3, the stamp rally can be successful, so it is necessary to prepare a present in town 3. When JOI-kun starts from town 1, presents must be prepared only in towns 3 and 4, so the first line of the output should be 2.

Also, when JOI-kun starts from town 2, presents must be prepared only in towns 3, 4, and 5, so the second line of the output should be 3.

This input example satisfies the constraints of subtasks 3,6.


Sample Input 2

5 1
0 1 2 1 2
1 2
2 3
3 4
4 5

Sample Output 2

2
1
2
0
1

This input example satisfies the constraints of subtasks 1,2,3,4,6.


Sample Input 3

7 6
2 3 0 4 1 3 4
1 2
2 3
2 4
1 5
1 6
6 7

Sample Output 3

2
2
7
5
1
2
5

This input example satisfies the constraints of subtasks 3,5,6.

配点: 100

問題文

JOI 君が住む IOI 国には N 個の街があり,1 から N までの番号が付けられている. また,IOI 国には N-1 本の道路があり,1 から N-1 までの番号が付けられている. 道路 j (1 \leqq j \leqq N-1) は街 U_j と街 V_j を双方向に結んでいる. どの街からどの街へも何本かの道路を通ることによって移動することができる.

これから IOI 国でスタンプラリーが開催される. それぞれの街には 1 つのスタンプ台が設置される予定である. 街 i (1 \leqq i \leqq N) のスタンプ台は時刻 T_i に設置される.

JOI 君はスタンプラリーに参加することにした. JOI 君は時刻 0 にいずれかの街から行動を開始する. また,JOI 君は時刻 0 の時点で体力が D である.

JOI 君は時刻 t に街 i にいるとき,次の行動をとる.

  1. まず,現在いる街に既にスタンプ台が設置されている場合はスタンプを押す. すなわち,T_i \leqq t の場合はスタンプを押す.
  2. 次に,スタンプラリーを終了するか別の街に移動するかを選ぶ. ただし,街 i と道路で結ばれた街で,まだ訪れたことのない街が存在し,かつ,現在の体力が 1 以上であるときに限り,別の街に移動することを選ぶことができる.
  3. JOI 君が別の街に移動することを選んだ場合,JOI 君は街 i と道路で結ばれた街のうち,まだ訪れたことのない街 j を選び,移動する. 体力が 1 減少し,時刻 t+1 に街 j に到着する.
  4. JOI 君がスタンプラリーを終了することを選んだ場合,それまでに一度以上スタンプを押した場合はスタンプラリー成功となり,その場でプレゼントを受け取ることができる.そうでない場合はスタンプラリー失敗となる.

街の移動にかかる時間以外は無視できるものとする. JOI 君が同じ街に留まることはできないことに注意せよ.

大会の運営者であるあなたは,JOI 君がスタンプラリーに成功した場合のためにそれぞれの街にプレゼントを用意しておく必要があるが,プレゼントの数には限りがあるため,必要最小限の街にプレゼントを用意したい. しかしながら,あなたは JOI 君がどの街から行動を開始するかについての情報を持っていない. そこで,あなたはそれぞれの s (1 \leqq s \leqq N) について,JOI 君が街 s から行動を開始したときに,プレゼントを用意しておく必要のある街の数,すなわち,JOI 君が街 g でスタンプラリーを終了をしたときにスタンプラリー成功となる可能性があるような g (1\leqq g\leqq N) の数を求めたい.

IOI 国の街と道路の情報,JOI 君の体力,およびスタンプ台の設置時刻が与えられたとき,それぞれの街について, その街から JOI 君が行動を開始したときにプレゼントを用意しておく必要のある街の数を求めるプログラムを作成せよ.


入力

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

N D
T_1 T_2 \cdots T_N
U_1 V_1
U_2 V_2
\vdots
U_{N-1} V_{N-1}

出力

標準出力に,N 行出力せよ. s 行目 (1\leqq s \leqq N) には,JOI 君が街 s から行動を開始したときにプレゼントを用意しておく必要のある街の数を出力せよ.


制約

  • 2 \leqq N \leqq 400\,000
  • 0 \leqq D \leqq N-1
  • 0 \leqq T_i \leqq N (1 \leqq i \leqq N).
  • 1 \leqq U_j < V_j \leqq N (1 \leqq j \leqq N-1).
  • どの街からどの街へも何本かの道路を通ることによって移動することができる.
  • 入力される値はすべて整数である.

小課題

  1. (3 点) D\leqq 1
  2. (7 点) N\leqq 3\,000(U_j,V_j)=(j,j+1) (1\leqq j \leqq N-1).
  3. (10 点) N\leqq 3\,000
  4. (11 点) (U_j,V_j)=(j,j+1) (1\leqq j \leqq N-1).
  5. (41 点) D=N-1N \leqq 150\,000
  6. (28 点) 追加の制約はない.

入力例 1

5 2
2 2 0 1 3
1 2
2 3
2 4
4 5

出力例 1

2
3
4
2
2

s=1 のとき,JOI 君の行動の一例を示す.

  • JOI 君は時刻 0 に街 1 にいる状態で,次の行動をとる.
    • 1 にはまだスタンプ台が設置されていないため,JOI 君はスタンプを押さない.
    • JOI 君の現在の体力は 2 である.街 1 と道路で結ばれた街のうち,まだ訪れたことのない街の一つである街 2 に移動する.
    • JOI 君の体力が 1 減少し,時刻 1 に街 2 に到着する.
  • JOI 君は時刻 1 に街 2 にいる状態で,次の行動をとる.
    • 2 にはまだスタンプ台が設置されていないため,JOI 君はスタンプを押さない.
    • JOI 君の現在の体力は 1 である.街 2 と道路で結ばれた街のうち,まだ訪れたことのない街の一つである街 3 に移動する.
    • JOI 君の体力が 1 減少し,時刻 2 に街 3 に到着する.
  • JOI 君は時刻 2 に街 3 にいる状態で,次の行動をとる.
    • 3 には既にスタンプ台が設置されているため,JOI 君はスタンプを押す.
    • JOI 君はここでスタンプラリーを終了することを選ぶ.これまでに一度以上スタンプを押しているため,スタンプラリー成功となる.その場でプレゼントを受け取る.

よって,JOI 君が街 1 から行動を開始し,街 3 でスタンプラリーを終了したときにスタンプラリー成功となる可能性があるため,街 3 にプレゼントを用意しておく必要がある. JOI 君が街 1 から行動を開始したときにプレゼントを用意しておく必要のある街は街 3 と街 4 のみであるため,1 行目には 2 を出力する.

また,JOI 君が街 2 から行動を開始したときにプレゼントを用意しておく必要のある街は街 3,街 4 と街 5 のみであるため,2 行目には 3 を出力する.

この入力例は小課題 3,6 の制約を満たす.


入力例 2

5 1
0 1 2 1 2
1 2
2 3
3 4
4 5

出力例 2

2
1
2
0
1

この入力例は小課題 1,2,3,4,6 の制約を満たす.


入力例 3

7 6
2 3 0 4 1 3 4
1 2
2 3
2 4
1 5
1 6
6 7

出力例 3

2
2
7
5
1
2
5

この入力例は小課題 3,5,6 の制約を満たす.

J - パン職人 (Baker)

実行時間制限: 2 sec / メモリ制限: 1024 MiB

Score: 100 points

Problem Statement

JOI Bakery is a bakery famous for its mouth-watering croissants. JOI Bakery has N bakers numbered from 1 to N. Baker i (1 \leq i \leq N) takes i minutes to make one croissant. One baker cannot make multiple croissants at the same time.

Today, M customers numbered from 1 to M are scheduled to visit JOI Bakery, and each customer plans to order one croissant. Customer j (1 \leq j \leq M) will order a croissant at time T_j, where time t denotes the time t minutes from now. However, a customer who cannot receive their croissant within L minutes of ordering will give up and leave the shop. In other words, to fulfill the order of customer j (1 \leq j \leq M), the croissant must be finished by time T_j + L (including exactly at time T_j + L).

Manager K, who manages JOI Bakery, plans to have exactly one baker work today and is considering which baker to send and at what time. Since bakers focus intensely on bread-making during their shift, they ignore all orders placed after their start time (not including exactly at the start time). That is, a baker starting work at time t cannot fulfill orders from customers j (1 \leq j \leq M) such that T_j > t.

Manager K is currently considering Q work plans. The q-th plan (1 \leq q \leq Q) is to have baker A_q start work at time B_q. To help with the decision, for each of the Q plans, he wants to know the maximum number of customers whose orders can be fulfilled if that plan is executed. Note that the time it takes for a baker to start making a croissant after arriving, and the time it takes to start making a new croissant after finishing one, can be ignored.

Given information about the customers visiting JOI Bakery and the work plans, create a program to find the maximum number of customers whose orders can be fulfilled for each plan.


Input

Read the following data from the standard input.

N M L Q
T_1 T_2 \cdots T_M
A_1 B_1
A_2 B_2
\vdots
A_Q B_Q

Output

Write Q lines to the standard output. The q-th line (1 \leq q \leq Q) of the output should contain an integer representing the maximum number of customers whose orders can be fulfilled in the q-th work plan.


Constraints

  • 1 \leq N \leq 4\times 10^{12}.
  • 1 \leq M \leq 2\,000\,000.
  • 1 \leq L \leq 2\times 10^{12}.
  • 1 \leq Q \leq 400\,000.
  • 0 \leq T_j \leq 2\times 10^{12} (1 \leq j \leq M).
  • T_j \leq T_{j+1} (1 \leq j \leq M-1).
  • 1 \leq A_q \leq N (1 \leq q \leq Q).
  • 0 \leq B_q \leq 4\times 10^{12} (1 \leq q \leq Q).
  • Given values are all integers.

Subtasks

  1. (8 points) M \leq 10, Q \leq 100\,000.
  2. (12 points) M \leq 500, Q \leq 100\,000.
  3. (30 points) T_M \leq B_q < T_1+L (1 \leq q \leq Q).
  4. (10 points) T_M \leq B_q (1 \leq q \leq Q).
  5. (22 points) M \leq 500\,000, Q \leq 100\,000.
  6. (18 points) No additional constraints.

Sample Input 1

4 4 6 4
0 2 3 8
2 3
1 6
3 3
4 7

Sample Output 1

3
2
2
0

Regarding the 1st work plan, baker 2 starting at time 3 can fulfill the orders of 3 customers 1, 2, and 3, for example, as follows:

  • First, to fulfill customer 1's order, start making a croissant at time 3 and finish it 2 minutes later at time 5. (This satisfies the condition to finish by time T_1 + L = 0 + 6 = 6.)
  • Next, to fulfill customer 2's order, start making a croissant at time 5 and finish it 2 minutes later at time 7. (This satisfies the condition to finish by time T_2 + L = 2 + 6 = 8.)
  • Finally, to fulfill customer 3's order, start making a croissant at time 7 and finish it 2 minutes later at time 9. (This satisfies the condition to finish by time T_3 + L = 3 + 6 = 9.)

Customer 4's order is ignored because it arrives after the baker's start time, so it cannot be fulfilled. Thus, a maximum of 3 customers' orders can be fulfilled, so the 1st line outputs 3.

Regarding the 2nd work plan, baker 1 starting at time 6 can fulfill the orders of 2 customers 2 and 3, for example, as follows:

  • First, to fulfill customer 3's order, start making a croissant at time 6 and finish it 1 minute later at time 7. (This satisfies the condition to finish by time T_3 + L = 3 + 6 = 9.)
  • Next, to fulfill customer 2's order, start making a croissant at time 7 and finish it 1 minute later at time 8. (This satisfies the condition to finish by time T_2 + L = 2 + 6 = 8.)

Customer 4's order is ignored because it arrives after the start time. Also, the order of customer 1, which needs to be finished by time 6, cannot be fulfilled. Thus, a maximum of 2 customers' orders can be fulfilled, so the 2nd line outputs 2.

Regarding the 3rd work plan, baker 3 starting at time 3 can fulfill orders for customers 1 and 3, or customers 2 and 3, but cannot fulfill all orders for customers 1, 2, and 3. They also cannot fulfill the order for customer 4 which arrives after the start time. Thus, a maximum of 2 customers' orders can be fulfilled, so the 3rd line outputs 2.

Regarding the 4th work plan, baker 4 starting at time 7 cannot fulfill any customer's order. Thus, the 4th line outputs 0.

This sample input satisfies the constraints of Subtasks 1,2,5, and 6.


Sample Input 2

20 5 12 4
1 2 4 8 10
1 12
3 10
3 11
15 10

Sample Output 2

5
4
3
0

This sample input satisfies the constraints of all the subtasks.


Sample Input 3

100000 6 272273 10
5 9 209 8128 17202 50102
164 9
11 24
835 9267
2 256
2 314156
18475 142
1826 18978
44757 1
4 1646
218 44

Sample Output 3

2
2
4
3
1
2
5
0
3
2

This sample input satisfies the constraints of Subtasks 1,2,5, and 6.

配点: 100

問題文

JOI ベーカリーは頬が落ちるほど美味しいクロワッサンで有名なパン屋さんである. JOI ベーカリーには 1 から N までの番号が付けられた N 人のパン職人が在籍しており,職人 i (1\leqq i \leqq N) は 1 つのクロワッサンを作るのに i 分かかる. 1 人の職人が同時に複数のクロワッサンを作ることはできない.

JOI ベーカリーには今日これから,1 から M までの番号が付けられた M 人の客が来店し,それぞれの客がクロワッサンを 1 つ注文する予定である. 今から t 分後のことを時刻 t と呼ぶことにすると,客 j (1\leqq j \leqq M) がクロワッサンを注文するのは時刻 T_j である. ただし,注文後 L 分以内にクロワッサンを受け取ることのできなかった客は,諦めて店から去ってしまう. すなわち,客 j (1\leqq j \leqq M) の注文に応えるには,時刻 T_j+L まで(時刻 T_j+L ちょうどを含む)にクロワッサンを作り終えなければならない.

JOI ベーカリーを管理する K 店長は,今日はちょうど 1 人の職人を店に出勤させることを予定しており,どの職人をどの時刻に出勤させるのがよいか考えている. 職人たちは勤務中はパン作りに集中してしまうため,出勤時刻より後(出勤時刻ちょうどを含まない)に入った注文をすべて無視してしまう. すなわち,時刻 t に出勤する職人は,T_j > t を満たすような客 j (1\leqq j \leqq M) の注文に応えることができない.

K 店長は現在 Q 個の出勤案を検討しており,q 番目の案 (1\leqq q \leqq Q) は職人 A_q を時刻 B_q に出勤させるというものである. 検討の材料とするため,Q 個の案それぞれについて,その案を実行した際に最大で何人の客の注文に応えることができるか知りたい. なお,職人が出勤してからクロワッサンを作り始めるまでにかかる時間や,クロワッサンを 1 つ作り終えてから新しいクロワッサンを作り始めるまでにかかる時間は無視できるものとする.

JOI ベーカリーに来店する客と出勤案の情報が与えられたとき,それぞれの案において最大で何人の客の注文に応えることができるかを求めるプログラムを作成せよ.


入力

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

N M L Q
T_1 T_2 \cdots T_M
A_1 B_1
A_2 B_2
\vdots
A_Q B_Q

出力

標準出力に Q 行で出力せよ. q 行目 (1\leqq q \leqq Q) には,q 番目の出勤案において最大で何人の客の注文に応えることができるかを表す整数を出力せよ.


制約

  • 1 \leqq N \leqq 4\times 10^{12}
  • 1 \leqq M \leqq 2\,000\,000
  • 1 \leqq L \leqq 2\times 10^{12}
  • 1 \leqq Q \leqq 400\,000
  • 0 \leqq T_j \leqq 2\times 10^{12} (1 \leqq j \leqq M).
  • T_j \leqq T_{j+1} (1 \leqq j \leqq M-1).
  • 1 \leqq A_q \leqq N (1 \leqq q \leqq Q).
  • 0 \leqq B_q \leqq 4\times 10^{12} (1 \leqq q \leqq Q).
  • 入力される値はすべて整数である.

小課題

  1. (8 点) M \leqq 10Q \leqq 100\,000
  2. (12 点) M \leqq 500Q \leqq 100\,000
  3. (30 点) T_M \leqq B_q < T_1+L (1 \leqq q \leqq Q).
  4. (10 点) T_M \leqq B_q (1 \leqq q \leqq Q).
  5. (22 点) M \leqq 500\,000Q \leqq 100\,000
  6. (18 点) 追加の制約はない.

入力例 1

4 4 6 4
0 2 3 8
2 3
1 6
3 3
4 7

出力例 1

3
2
2
0

1 番目の出勤案について,時刻 3 に出勤した職人 2 は,例えば以下のようにして 3 人の客 1,2,3 の注文に応えることができる.

  • まず,客 1 の注文に応えるため,時刻 3 にクロワッサンを 1 つ作り始め,2 分後の時刻 5 に作り終える.(これは,時刻 T_1+L=0+6=6 までに作り終えるという条件を満たしている.)
  • 次に,客 2 の注文に応えるため,時刻 5 にクロワッサンを 1 つ作り始め,2 分後の時刻 7 に作り終える.(これは,時刻 T_2+L=2+6=8 までに作り終えるという条件を満たしている.)
  • 最後に,客 3 の注文に応えるため,時刻 7 にクロワッサンを 1 つ作り始め,2 分後の時刻 9 に作り終える.(これは,時刻 T_3+L=3+6=9 までに作り終えるという条件を満たしている.)

4 の注文は出勤時刻よりも後に入るため無視してしまい,応えることができない. よって,最大で 3 人の客の注文に応えることができ,1 行目には 3 を出力する.

2 番目の出勤案について,時刻 6 に出勤した職人 1 は,例えば以下のようにして 2 人の客 2,3 の注文に応えることができる.

  • まず,客 3 の注文に応えるため,時刻 6 にクロワッサンを 1 つ作り始め,1 分後の時刻 7 に作り終える.(これは,時刻 T_3+L=3+6=9 までに作り終えるという条件を満たしている.)
  • 次に,客 2 の注文に応えるため,時刻 7 にクロワッサンを 1 つ作り始め,1 分後の時刻 8 に作り終える.(これは,時刻 T_2+L=2+6=8 までに作り終えるという条件を満たしている.)

4 の注文は出勤時刻よりも後に入るため無視してしまい,応えることができない. また,時刻 6 までに作り終える必要のある客 1 の注文にも応えることができない. よって,最大で 2 人の客の注文に応えることができ,2 行目には 2 を出力する.

3 番目の出勤案について,時刻 3 に出勤した職人 3 は,客 1,3 の注文に応えることや客 2,3 の注文に応えることはできるが, 客 1,2,3 の注文すべてに応えることはできず,また出勤時刻よりも後に入る客 4 の注文にも応えることができない. よって,最大で 2 人の客の注文に応えることができ,3 行目には 2 を出力する.

4 番目の出勤案について,時刻 7 に出勤した職人 4 は,どの客の注文にも応えることができない. よって,4 行目には 0 を出力する.

この入力例は小課題 1,2,5,6 の制約を満たす.


入力例 2

20 5 12 4
1 2 4 8 10
1 12
3 10
3 11
15 10

出力例 2

5
4
3
0

この入力例はすべての小課題の制約を満たす.


入力例 3

100000 6 272273 10
5 9 209 8128 17202 50102
164 9
11 24
835 9267
2 256
2 314156
18475 142
1826 18978
44757 1
4 1646
218 44

出力例 3

2
2
4
3
1
2
5
0
3
2

この入力例は小課題 1,2,5,6 の制約を満たす.

K - JOI 国のお祭り事情 3 (Festivals in JOI Kingdom 3)

実行時間制限: 4 sec / メモリ制限: 1024 MiB

Score : 100 points

Problem Statement

JOI Kingdom consists of N cities and N-1 highways. The cities are numbered 1 through N, and the highways are numbered 1 through N-1. It is possible to travel from any city to any other city by traversing a number of highways.

Each city has a popularity, represented by a non-negative integer. The popularity of city i (1 \leq i \leq N) is initially C_i. Each highway has a travel time, represented by a positive integer. Highway j (1 \leq j \leq N-1) connects city A_j and city B_j, and its travel time is initially D_j.

Each city in JOI Kingdom has a cauldron. JOI Kingdom keeps its tradition that, in a festival, cities ignite their cauldrons, and these ignitions signal the departure of parades from those cities.

City u is adjacent to city v if these two cities are directly connected by a highway. At the exact moment when a city ignites its cauldron, one parade will depart from this city for each of its adjacent cities, spending time equal to the travel time of the corresponding highway. To be precise, for two adjacent cities v and u, the parade from city v reaches city u at time t+d, where t is the time when the cauldron of city v is ignited, and d is the travel time of the highway connecting cities v and u.

Some cities ignite their cauldron the moment the festival starts, while other cities only do so after the festival heats up enough. Let the time 0 be the start of the festival. For the city i whose popularity is c, the time when city i ignites its cauldron is determined as follows:

  • If c = 0, city i ignites its cauldron at time 0.
  • If c \geq 1, city i ignites its cauldron at the time when the number of parades that have arrived from adjacent cities becomes at least c. If this never happens, city i never ignites its cauldron.

Mr. K will stay at JOI Kingdom. During his stay, JOI Kingdom will have Q events related to its festival. These events are numbered 1 through Q from earliest to latest.

Event k (1 \leq k \leq Q) is one of the following 3 types:

  • Type 1: The popularity of city V_k changes to X_k.
  • Type 2: The travel time of highway E_k changes to X_k.
  • Type 3: Mr. K visits city V_k. Assuming a festival starts at this moment, you must determine whether {city \nolinebreak V_k} would ignite its cauldron, and if so, calculate the time that the cauldron is ignited.

Write a program which, given the structure of JOI Kingdom, popularity of each city, travel time of each highway, and details of the events, for each Type 3 event, determines when the city Mr. K visits ignites its cauldron.


Input

Read the following data from the standard input.

N
A_1 B_1 D_1
\vdots
A_{N-1} B_{N-1} D_{N-1}
C_1
\vdots
C_N
Q
(Query 1)
\vdots
(Query Q)

(Query k) represents the details of event k (1 \leq k \leq Q). In (Query k), space-separated integers are written. Let P_k be the first integer. P_k is 1, 2, or 3, which means the type of event k. Then (Query k) means as follows:

  • If P_k = 1, there are two more integers V_k, X_k written in this order. This means that the popularity of city V_k changes to X_k.
  • If P_k = 2, there are two more integers E_k, X_k written in this order. This means that the travel time of highway E_k changes to X_k.
  • If P_k = 3, there is one more integer V_k written. This means Mr. K visits city V_k and, assuming a festival starts at this moment, you must determine the time that the cauldron at city V_k is ignited.

Output

To standard output, output the following in one line for each event k (1 \leq k \leq Q) with P_k = 3, in the increasing order of k.

  • If the city Mr. K visits would ignite its cauldron, output the time that the cauldron is ignited.
  • Otherwise, output -1.

Constraints

  • 2 \leq N \leq 150\,000.
  • 0 \leq C_i \leq N (1 \leq i \leq N).
  • 1 \leq A_j < B_j \leq N (1 \leq j \leq N-1).
  • 1 \leq D_j \leq 1\,000\,000 (1 \leq j \leq N-1).
  • It is possible to travel from any city to any other city by traversing a number of highways.
  • 1 \leq Q \leq 150\,000.
  • If P_k = 1, we have 1 \leq V_k \leq N, 0 \leq X_k \leq N (1 \leq k \leq Q).
  • If P_k = 2, we have 1 \leq E_k \leq N-1, 1 \leq X_k \leq 1\,000\,000 (1 \leq k \leq Q).
  • If P_k = 3, we have 1 \leq V_k \leq N (1 \leq k \leq Q).
  • Given values are all integers.

Subtasks

  1. (6 points) N \leq 2\,000, Q \leq 2\,000.
  2. (7 points) A_j = 1, B_j = j+1 (1 \leq j \leq N-1). If P_k = 3, we have V_k = 1 (1 \leq k \leq Q).
  3. (14 points) N-1 is divisible by 3. A_j = ((j - 1) \bmod \frac{N-1}{3}) + 1, B_j = j+1 (1 \leq j \leq N-1). If P_k = 3, we have V_k = 1 (1 \leq k \leq Q).
  4. (25 points) P_k \neq 1. If P_k = 3, we have V_k = 1 (1 \leq k \leq Q).
  5. (12 points) If P_k = 3, we have V_k = 1 (1 \leq k \leq Q).
  6. (22 points) P_k \neq 1 (1 \leq k \leq Q).
  7. (14 points) No additional constraints.

Sample Input 1

7
1 2 30
2 3 30
1 4 70
2 5 20
1 6 10
2 7 50
2
3
0
0
0
1
0
8
3 1
1 6 0
3 1
2 6 10
3 1
1 2 7
1 6 7
3 1

Sample Output 1

80
70
60
-1

In the festival considered in event 1, the times when the cities ignite their cauldrons are as follows:

  • At time 0, cities 3,4,5,7 ignite their cauldrons.
  • At time 50, city 2 ignites its cauldron. By then, the parades from cities 3,5,7 have reached city 2.
  • At time 80, city 1 ignites its cauldron. By then, the parades from cities 2,4 have reached city 1.
  • At time 90, city 6 ignites its cauldron. By then, the parade from city 1 has reached city 6.

Since city 1 would ignite its cauldron at time 80, output 80.

In the festival considered in event 3, the times when the cities ignite their cauldrons are as follows:

  • At time 0, cities 3,4,5,6,7 ignite their cauldrons.
  • At time 50, city 2 ignites its cauldron. By then, the parades from cities 3,5,7 have reached city 2.
  • At time 70, city 1 ignites its cauldron. By then, the parades from cities 4,6 have reached city 1.

Since city 1 would ignite its cauldron at time 70, output 70.

In the festival considered in event 5, the times when the cities ignite their cauldrons are as follows:

  • At time 0, cities 3,4,5,6,7 ignite their cauldrons.
  • At time 30, city 2 ignites its cauldron. By then, the parades from cities 3,5,7 have reached city 2.
  • At time 60, city 1 ignites its cauldron. By then, the parades from cities 2,6 have reached city 1.

Since city 1 would ignite its cauldron at time 60, output 60.

In the festival considered in event 8, the times when the cities ignite their cauldrons are as follows:

  • At time 0, cities 3,4,5,7 ignite their cauldrons.

Cities 1,2,6 would never ignite their cauldrons. Since city 1 would never ignite its cauldron, output -1.

This sample input satisfies the constraints of Subtasks 1,3,5,7.


Sample Input 2

6
1 2 10
1 3 30
1 4 50
1 5 30
1 6 10
2
0
0
0
0
1
10
3 1
2 3 20
3 1
1 6 0
3 1
1 1 4
3 1
1 2 6
1 3 6
3 1

Sample Output 2

30
20
10
30
-1

This sample input satisfies the constraints of Subtasks 1,2,5,7.

配点: 100

問題文

JOI 国は N 個の街と N-1 本の国道からなり, 街には 1 から N までの番号が,国道には 1 から N-1 までの番号が付けられている. どの街からどの街へも,何本かの国道を通ることで移動できる.

それぞれの街は,非負整数で表される人気度をもつ. 街 i (1 \leqq i \leqq N) の人気度ははじめ C_i である. それぞれの国道は,正の整数で表される所要時間をもつ. 国道 j (1 \leqq j \leqq N-1) は街 A_j と街 B_j を双方向につないでおり,その所要時間ははじめ D_j である.

JOI 国の街には聖火台が 1 つずつ設置されている. JOI 国のお祭りでは,街の聖火台に火をつけ,それを合図に街からパレードを出発させる伝統がある.

v と街 u が国道で直接つながれているとき,街 u は街 v隣接するという. ある街の聖火台に火がつけられた時点で,隣接する街のそれぞれに向けて 1 つずつパレードが出発し, それが通る国道の所要時間と同じ時間をかけて歩いたあと,向かいの街に到着する. つまり,互いに隣接する街 v, u について,街 v の聖火台に火がつけられた時刻を t,街 v, u を直接つなぐ国道の所要時間を d とすると, 街 v から出発するパレードが街 u に到着する時刻は t+d である.

街によっては,お祭りが始まった瞬間に火をつけるところもあれば,お祭りが盛り上がってきたタイミングで火をつけるところもある. お祭りが始まるタイミングを時刻 0 とする. 街 i の聖火台に火をつける時刻は,街 i の人気度を c とすると,以下のように決定される.

  • c = 0 の場合,時刻 0 に火をつける.
  • c \geqq 1 の場合,隣接する街から来て到着したパレードが c 個以上にはじめてなった時刻に火をつける. そのようなことが起こらない場合,火はつけない.

これから K 理事長が JOI 国に滞在する. そのあいだに, JOI 国のお祭りについて Q 回の出来事がある. これらの出来事には,起きるのが早い順に 1 から Q までの番号が付けられている. 出来事 k (1 \leqq k \leqq Q)は以下の 3 種類のいずれかである.

  • タイプ 1: 街 V_k の人気度が X_k に変更される.
  • タイプ 2: 国道 E_k の所要時間が X_k に変更される.
  • タイプ 3: K 理事長が街 V_k に来る.このとき,仮にこの時点でお祭りが始まるとして,街 V_k の聖火台に火がつけられるかどうかを判定し,つけられる場合はその時刻を求めなければならない.

JOI 国の構造,街の人気度,国道の所要時間,および滞在中の出来事の情報が与えられたとき, タイプ 3 の出来事において K 理事長がいる街の聖火台に火がつけられる時刻を計算するプログラムを作成せよ.


入力

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

N
A_1 B_1 D_1
\vdots
A_{N-1} B_{N-1} D_{N-1}
C_1
\vdots
C_N
Q
(Query 1)
\vdots
(Query Q)

(Query k) は出来事 k の情報を表す(1 \leqq k \leqq Q). (Query k) にはいくつかの整数が空白区切りで書かれている. そのうち最初に書かれているものは出来事の種類を表す整数であり,1,2,3 のいずれかである. これを P_k とすると,この行の内容は以下の 3 種類のいずれかである.

  • P_k = 1 のとき,この行には続いて 2 個の整数 V_k, X_k がこの順に書かれている. これは,街 V_k の人気度が X_k に変更されることを表す.
  • P_k = 2 のとき,この行には続いて 2 個の整数 E_k, X_k がこの順に書かれている. これは,国道 E_k の所要時間が X_k に変更されることを表す.
  • P_k = 3 のとき,この行には続いて整数 V_k が書かれている. これは,K 理事長がこのとき街 V_k におり,仮にこの時点でお祭りが始まるとしたときに街 V_k の聖火台に火がつけられる時刻を求める必要があることを表す.

出力

P_k = 3 である出来事 k (1 \leqq k \leqq Q) それぞれに対して, K 理事長がいる街の聖火台に火がつけられる場合はその時刻を, 火がつけられない場合は -1 を, k の昇順に改行区切りで出力せよ.


制約

  • 2 \leqq N \leqq 150\,000
  • 0 \leqq C_i \leqq N (1 \leqq i \leqq N).
  • 1 \leqq A_j < B_j \leqq N (1 \leqq j \leqq N-1).
  • 1 \leqq D_j \leqq 1\,000\,000 (1 \leqq j \leqq N-1).
  • どの街からどの街へも,何本かの国道を通ることで移動できる.
  • 1 \leqq Q \leqq 150\,000
  • P_k = 1 のとき,1 \leqq V_k \leqq N0 \leqq X_k \leqq N (1 \leqq k \leqq Q).
  • P_k = 2 のとき,1 \leqq E_k \leqq N-11 \leqq X_k \leqq 1\,000\,000 (1 \leqq k \leqq Q).
  • P_k = 3 のとき,1 \leqq V_k \leqq N (1 \leqq k \leqq Q).
  • 入力される値はすべて整数である.

小課題

  1. (6 点) N \leqq 2\,000Q \leqq 2\,000
  2. (7 点) A_j = 1B_j = j+1 (1 \leqq j \leqq N-1). P_k = 3 のとき V_k = 1 (1 \leqq k \leqq Q).
  3. (14 点) N-13 の倍数. A_j = ((j - 1) \bmod \frac{N-1}{3}) + 1B_j = j+1 (1 \leqq j \leqq N-1). P_k = 3 のとき V_k = 1 (1 \leqq k \leqq Q).
  4. (25 点) P_k \neq 1P_k = 3 のとき V_k = 1 (1 \leqq k \leqq Q).
  5. (12 点) P_k = 3 のとき V_k = 1 (1 \leqq k \leqq Q).
  6. (22 点) P_k \neq 1 (1 \leqq k \leqq Q).
  7. (14 点) 追加の制約はない.

入力例 1

7
1 2 30
2 3 30
1 4 70
2 5 20
1 6 10
2 7 50
2
3
0
0
0
1
0
8
3 1
1 6 0
3 1
2 6 10
3 1
1 2 7
1 6 7
3 1

出力例 1

80
70
60
-1

出来事 1 で想定されるお祭りにおいて,各街の聖火台に火がつく時刻は,時系列順に以下のとおりである.

  • 時刻 0 に,街 3,4,5,7 の聖火台に火がつく.
  • 時刻 50 に,街 2 の聖火台に火がつく.この時点で,街 2 には 街 3,5,7 からパレードが到着している.
  • 時刻 80 に,街 1 の聖火台に火がつく.この時点で,街 1 には 街 2,4 からパレードが到着している.
  • 時刻 90 に,街 6 の聖火台に火がつく.この時点で,街 6 には 街 1 からパレードが到着している.

1 の聖火台に火がつく時刻は 80 なので,80 を出力する.

出来事 3 で想定されるお祭りにおいて,各街の聖火台に火がつく時刻は,時系列順に以下のとおりである.

  • 時刻 0 に,街 3,4,5,6,7 の聖火台に火がつく.
  • 時刻 50 に,街 2 の聖火台に火がつく.この時点で,街 2 には 街 3,5,7 からパレードが到着している.
  • 時刻 70 に,街 1 の聖火台に火がつく.この時点で,街 1 には 街 4,6 からパレードが到着している.

1 の聖火台に火がつく時刻は 70 なので,70 を出力する.

出来事 5 で想定されるお祭りにおいて,各街の聖火台に火がつく時刻は,時系列順に以下のとおりである.

  • 時刻 0 に,街 3,4,5,6,7 の聖火台に火がつく.
  • 時刻 30 に,街 2 の聖火台に火がつく.この時点で,街 2 には 街 3,5,7 からパレードが到着している.
  • 時刻 60 に,街 1 の聖火台に火がつく.この時点で,街 1 には 街 2,6 からパレードが到着している.

1 の聖火台に火がつく時刻は 60 なので,60 を出力する.

出来事 8 で想定されるお祭りにおいて,各街の聖火台に火がつく時刻は,時系列順に以下のとおりである.

  • 時刻 0 に,街 3,4,5,7 の聖火台に火がつく.

1,2,6 の聖火台に火がつくことはない.街 1 の聖火台に火がつくことはないので,-1 を出力する.

この入力例は小課題 1,3,5,7 の制約を満たす.


入力例 2

6
1 2 10
1 3 30
1 4 50
1 5 30
1 6 10
2
0
0
0
0
1
10
3 1
2 3 20
3 1
1 6 0
3 1
1 1 4
3 1
1 2 6
1 3 6
3 1

出力例 2

30
20
10
30
-1

この入力例は小課題 1,2,5,7 の制約を満たす.

L - 電圧 2 (Voltage 2)

実行時間制限: 2 sec / メモリ制限: 1024 MiB

Score : 100 points

Distributed Files

Problem Statement

Do you know Just Odd Inventions Co., Ltd.? This company engages in nothing but just odd inventions. We abbreviate it as JOI.

In one of JOI's laboratories, there is a complicated electric circuit. The circuit consists of N nodes and M thin electric resistors. The nodes are numbered 0 through N - 1, and the resistors are numbered 0 through M - 1. Each node can be set to one of two states: high voltage or low voltage. Resistor i (0 \leq i \leq M - 1) is connected from node A_i to another node B_i. Current flows through this resistor if and only if node A_i is set to high voltage and node B_i is set to low voltage. In all other cases, no current flows through it. Also, for any two nodes, there is at most one resistor connecting them, regardless of its direction.

You are a researcher at JOI, and you are going to conduct an experiment using this circuit. The resistors in the circuit are so thin that you cannot visually determine which pairs of nodes they connect. However, there is one clue: while the voltages are being set, the temperature of the circuit rises according to the number of resistors through which current flows. Therefore, after setting the voltages, you decide to touch the circuit and read its temperature. Although you cannot measure the exact temperature of the circuit, you can perform two voltage settings and compare which one makes the circuit hotter. That is, by specifying two voltage settings, you can obtain one of the following pieces of information:

  • In the first voltage setting, the number of resistors through which current flows is larger.
  • In the two voltage settings, the number of resistors through which current flows is the same.
  • In the second voltage setting, the number of resistors through which current flows is larger.

Your goal is to repeat such temperature comparisons and determine all resistors in the circuit, that is, all ordered pairs (a, b) such that there is a resistor from node a to node b. You are given in advance the number of nodes N and the number of resistors M. You also know that every resistor connects two distinct nodes, and that for any two nodes there is at most one resistor connecting them, regardless of direction. Under these conditions, determine the entire set of ordered pairs (a, b) corresponding to the resistors in the circuit, from the information obtained by the temperature comparisons. Note that, depending on the structure of the circuit, it may be impossible to determine the resistors uniquely no matter what comparisons are performed or how many are performed. In that case, you must report that it is impossible to determine them uniquely.

To prevent deterioration of the resistors, you are allowed to compare temperatures at most 30\,000 times.

Incidentally, what kind of invention JOI is making with this odd circuit is top secret even within the company, and nobody except the president knows it.

Given the numbers of nodes and resistors in the circuit, write a program that determines the resistors in the circuit, or reports that this is impossible, using at most 30\,000 temperature comparisons.

Implementation Details

Your submitted program must include voltage.h using the #include preprocessor directive, and must implement the following function.

  • bool solve(int N, int M)
    • This function is called exactly once in each execution.
    • The argument N represents the number of nodes in the circuit, N.
    • The argument M represents the number of resistors in the circuit, M.
    • This function must return false if the resistor connections cannot be uniquely determined no matter what temperature comparisons are performed or how many are performed; otherwise, it must return true.
    • If this function returns true when the resistor connections cannot be determined uniquely, your program is judged as Wrong Answer [1].
    • If this function returns false even though the circuit can be determined from temperature comparisons, your program is judged as Wrong Answer [2].

Your program may call the following functions.

  • int query(std::vector<int> x, std::vector<int> y)
    • By using this function, you can perform two voltage settings and compare their temperatures.
    • The argument x specifies the first voltage setting, and the argument y specifies the second voltage setting.
    • The arguments x and y must be arrays of length N consisting of 0's and 1's.
    • If x[k] (0 \leq k \leq N - 1) is 1, then in the first voltage setting, node k is set to high voltage; if x[k] is 0, then node k is set to low voltage.
    • If y[k] (0 \leq k \leq N - 1) is 1, then in the second voltage setting, node k is set to high voltage; if y[k] is 0, then node k is set to low voltage.
    • The return value of this function is the result of comparing the temperatures of the circuit under the first and second voltage settings, and is one of -1, 0, or 1.
      • If the return value is -1, then the number of resistors through which current flows is larger in the first voltage setting than in the second.
      • If the return value is 0, then the numbers of resistors through which current flows are equal in the first and second voltage settings.
      • If the return value is 1, then the number of resistors through which current flows is larger in the second voltage setting than in the first.
    • If the length of x is not N, your program is judged as Wrong Answer [3].
    • If x contains a value other than 0 or 1, your program is judged as Wrong Answer [4].
    • If the length of y is not N, your program is judged as Wrong Answer [5].
    • If y contains a value other than 0 or 1, your program is judged as Wrong Answer [6].
    • You must not call this function more than 30\,000 times. If this function is called more than 30\,000 times, your program is judged as Wrong Answer [7].
  • void answer(int a, int b)
    • Use this function to report a resistor that you have identified.
    • The arguments a and b indicate that there is a resistor directed from node a to node b.
    • It must hold that 0 \leq a \leq N - 1 and 0 \leq b \leq N - 1. If this condition is not satisfied, your program is judged as Wrong Answer [8].
    • You must not call this function two or more times with the same pair (a, b). If this condition is violated, your program is judged as Wrong Answer [9].
    • You must not call this function more than M times. If this function is called more than M times, your program is judged as Wrong Answer [10].
    • When the function solve returns true, the function answer must have been called exactly M times by then. If this condition is not satisfied, your program is judged as Wrong Answer [11].
    • When the function solve returns true, for every pair (a, b) passed as arguments to answer up to that point, there must actually exist a resistor directed from node a to node b. If this condition is not satisfied, your program is judged as Wrong Answer [12].

Important Notices

  • You may implement additional helper functions or declare global variables for internal use.
  • Your submitted program must not use standard input/output or any other file interaction. However, you may use standard error output for debugging.

Compilation and Execution

You can download an archive file from the contest webpage which contains the sample grader to test your program. The archive file also contains a sample source file of your program.

The sample grader is the file grader.cpp. To test your program, place the files grader.cpp, voltage.cpp, and voltage.h in the same directory, and compile them using the following command:

g++ -std=gnu++20 -O2 -o grader grader.cpp voltage.cpp

Alternatively, you can execute the compile.sh file included in the archive by running:

./compile.sh

If compilation is successful, an executable file named grader will be generated.

Note that the actual grader is different from the sample grader. The sample grader is launched as a single process, which will read input data from the standard input and write the results to the standard output.


Input for the Sample Grader

The sample grader reads input from standard input in the following format:

N M
A_0 B_0
\vdots
A_{M-1} B_{M-1}

Output of the Sample Grader

The sample grader outputs the following information to standard output (quotation marks are not included in the actual output).

  • If your program is judged as one of Wrong Answer [3] – [12], the type of Wrong Answer is printed as in "Wrong Answer [5]".
  • Otherwise, the number of calls to the function query and the return value of the function solve are printed as in "Accepted: 30 true". The sample grader, unlike the actual grader, does not judge whether the verdict should be Wrong Answer [1] or [2], that is, whether the return value of the function solve is correct.

The sample grader terminates execution as soon as any of the conditions for Wrong Answer [3] – [12] is met. If multiple incorrect conditions are met, only one of them is displayed.

Notices on Grading

The actual grader is not adaptive; it has a fixed answer from the beginning of the interaction.


Constraints

  • 2 \leq N \leq 600.
  • 1 \leq M \leq 1\,000.
  • 0 \leq A_i \leq N - 1 (0 \leq i \leq M - 1).
  • 0 \leq B_i \leq N - 1 (0 \leq i \leq M - 1).
  • A_i \neq B_i (0 \leq i \leq M - 1).
  • (A_i, B_i) \neq (A_j, B_j) and (A_i, B_i) \neq (B_j, A_j) (0 \leq i < j \leq M - 1).
  • N, M, A_i, and B_i are integers (0 \leq i \leq M - 1).

Subtasks

  1. (10 points) N \leq 100, M = N - 1, B_i = A_{i+1} (0 \leq i \leq N - 3), and the N values A_0, A_1, \ldots, A_{N-2}, B_{N-2} are all distinct.
  2. (12 points) M = N - 1, B_i = A_{i+1} (0 \leq i \leq N - 3), and the N values A_0, A_1, \ldots, A_{N-2}, B_{N-2} are all distinct.
  3. (27 points) N \leq 100, A_i \neq A_j (0 \leq i < j \leq M - 1).
  4. (18 points) A_i \neq A_j (0 \leq i < j \leq M - 1).
  5. (17 points) N \leq 100.
  6. (16 points) No additional constraints.

Example Interaction

Below is an example of input that the sample grader reads and the corresponding function calls.

Sample Input 1

5 6
0 2
2 1
0 3
3 2
3 4
4 1

In the first call to query, the two voltage settings and the resistors through which current flows are as follows.

  • In the first setting, nodes 0 and 1 are set to low voltage, and nodes 2, 3, and 4 are set to high voltage. At this time, current flows through resistors 1 and 5.
  • In the second setting, nodes 3 and 4 are set to low voltage, and nodes 0, 1, and 2 are set to high voltage. At this time, current flows through resistor 2.

Since the number of resistors through which current flows is larger in the first voltage setting, the return value is -1.

This sample input satisfies the constraints for subtasks 5, 6.

Among the files available for download from the contest site: sample-01-in.txt corresponds to Sample Input 1. Additionally, sample-02-in.txt satisfies the constraints for all subtasks, and sample-03-in.txt satisfies the constraints for subtasks 3, 4, 5, 6.

配点 : 100

配布ファイル

問題文

あなたは Just Odd Inventions 社を知っているだろうか? この会社の業務は「ただ奇妙な発明 (just odd inventions)」をすることである.ここでは略して JOI 社と呼ぶ.

JOI 社のとある実験室には,複雑な電気回路がある. 回路は N 個の節点と M 本の細長い電気抵抗からなり,節点には 0 から N - 1 までの,電気抵抗には 0 から M - 1 までの番号が付けられている. 各節点は「高電圧」または「低電圧」のいずれかの状態に設定することができる. 電気抵抗 i (0 \leqq i \leqq M - 1) は節点 A_i から異なる節点 B_i に向けて繋がれており,節点 A_i が「高電圧」,節点 B_i が「低電圧」の状態にあるときにのみ電流が流れる. それ以外の場合には電流は流れない. また,任意の 2 つの節点を繋ぐ電気抵抗は,その向きに関わらず高々 1 本しか存在しない.

JOI 社の研究員であるあなたは,この回路を用いて実験を行うことになった. この回路の電気抵抗はあまりにも細長いため,電気抵抗がどの節点を結んでいるのかを目視で確認することはできない. しかし,各節点の電圧を設定している間,電流が流れている電気抵抗の本数に応じて回路の温度が上昇するという手がかりがある. そこであなたは,電圧を設定した後,回路に触れることで温度を読み取ることにした. あなたは回路の温度を正確に読み取ることはできないが,電圧の設定を 2 回行い,どちらの設定においてより温度が高くなったかを比較することはできる. つまり,電圧の設定を 2 回指定することで,以下のいずれかの情報が得られる.

  • 1 回目の電圧の設定の方が,電流が流れる電気抵抗の本数が多い.
  • 2 回の電圧の設定において,電流が流れる電気抵抗の本数は同じである.
  • 2 回目の電圧の設定の方が,電流が流れる電気抵抗の本数が多い.

あなたの目的は,この温度の比較を繰り返すことで,どの節点からどの節点へ向かう電気抵抗が存在するかをすべて特定することである. あらかじめ,節点の数 N,電気抵抗の本数 M は与えられている. また,電気抵抗は異なる節点を繋いでいること,および任意の 2 つの節点を繋ぐ電気抵抗はその向きに関わらず高々 1 本であることも分かっている. これらの条件のもと,温度の比較によって得られた情報から,回路に存在する電気抵抗が繋ぐ節点のペア (a, b) の集合をすべて特定せよ. ただし,回路の構造によっては,いかなる比較を何回行ったとしても,電気抵抗の繋がりを一意に特定できない場合がある. その場合には,特定不能であることを報告する必要がある.

電気抵抗の劣化を防ぐため,実際に温度を比較するのは 30\,000 回までしか行うことが許されない.

なお,JOI 社がこの奇妙な回路を用いてどのような発明をしているかは,社内でも最高機密であり社長以外の誰も知らない.

回路の節点と電気抵抗の本数が与えられたとき,30\,000 回以下の温度の比較で,回路の電気抵抗を特定するかもしくは特定不能であることを報告するプログラムを作成せよ.

実装の詳細

あなたの回答プログラムは,voltage.h#include プリプロセッサ指令で読み込み,以下の関数を実装しなければならない.

  • bool solve(int N, int M)
    • この関数は 1 回の実行で 1 回だけ呼び出される.
    • 引数 N は 回路の節点の数 N である.
    • 引数 M は 回路の電気抵抗の本数 M である.
    • この関数は,いかなる温度の比較を何回行ったとしても電気抵抗の繋がりを一意に特定できない場合には false を,それ以外の場合には true を返さなければならない.
    • 電気抵抗の繋がりを特定不能であるのに true を返した場合,不正解[1] と判定される.
    • 温度の比較によって回路が特定できるのに false を返した場合,不正解[2] と判定される.

あなたのプログラムは以下の関数を呼び出すことができる.

  • int query(std::vector<int> x, std::vector<int> y)
    • あなたはこの関数を用いて電圧の設定を 2 回行い,温度を比較できる.
    • 引数 x1 回目の電圧の設定を,引数 y2 回目の電圧の設定を指定する.
    • 引数 xy01 からなる長さ N の配列でなければならない.
    • x[k] (0 \leqq k \leqq N - 1) が 1 ならば 1 回目の電圧の設定において,節点 k は「高電圧」に, x[k]0 ならば節点 k は「低電圧」に設定することを表す.
    • y[k] (0 \leqq k \leqq N - 1) が 1 ならば 2 回目の電圧の設定において,節点 k は「高電圧」に, y[k]0 ならば節点 k は「低電圧」に設定することを表す.
    • この関数の戻り値は 1 回目の電圧の設定と 2 回目の電圧の設定において,温度を比較した結果であり,-1,0,1のいずれかの値である.
      • 戻り値が-1のとき,1 回目の方が,2 回目の電圧の設定より,電流が流れる電気抵抗の本数が多いことを表す.
      • 戻り値が0のとき,1 回目と2 回目の電圧の設定において電流が流れる電気抵抗の本数が等しいことを表す.
      • 戻り値が1のとき,2 回目の方が,1 回目の電圧の設定より,電流が流れる電気抵抗の本数が多いことを表す.
    • 引数 x の長さが N でない場合,不正解[3] と判定される.
    • 引数 x0 でも 1 でもない値が含まれる場合,不正解[4] と判定される.
    • 引数 y の長さが N でない場合,不正解[5] と判定される.
    • 引数 y0 でも 1 でもない値が含まれる場合,不正解[6] と判定される.
    • この関数を 30\,000 回より多く呼び出してはならない.30\,000 回より多く呼び出した場合,不正解[7] と判定される.
  • void answer(int a, int b)
    • この関数を用いて,特定した電気抵抗を解答する.
    • 引数 a, b は節点 a から節点 b に向けて繋がれている電気抵抗があることを表す.
    • 0 \leqq a \leqq N - 1 かつ 0 \leqq b \leqq N - 1 でなければならない.これが満たされない場合 不正解[8] と判定される.
    • 同じ (a, b) の組を引数として 2 回以上呼び出してはならない.これが満たされない場合 不正解[9] と判定される.
    • この関数を M 回より多く呼び出してはならない.M 回より多く呼び出した場合,不正解[10] と判定される.
    • 関数 solvetrue を返したとき,それまでに関数 answer はちょうど M 回呼び出されている必要がある. これが満たされない場合,不正解[11] と判定される.
    • 関数 solvetrue を返したとき,それまでに関数 answer の引数として渡されたすべての組 (a, b) について,節点 a から節点 b へ向けて繋がれている電気抵抗が実際に存在していなければならない. これが満たされない場合,不正解[12] と判定される.

重要な注意

  • 内部での使用のために他の関数を実装したり,グローバル変数を宣言するのは自由である.
  • あなたの提出したプログラムは,標準入力・標準出力,あるいは他のファイルといかなる方法でもやりとりしてはならない. ただし,標準エラー出力にデバッグ情報等を出力することは許される.

コンパイル・実行の方法

作成したプログラムをテストするための,採点プログラムのサンプルが, コンテストサイトからダウンロードできるアーカイブの中に含まれている. このアーカイブには,提出しなければならないファイルのサンプルも含まれている.

採点プログラムのサンプルは 1 つのファイルからなる. そのファイルは grader.cpp である. 作成したプログラムをテストするには, これらのファイル grader.cpp, voltage.cpp, voltage.h を同じディレクトリに置き,次のようにコマンドを実行する.

g++ -std=gnu++20 -O2 -o grader grader.cpp voltage.cpp

なお,アーカイブの中に含まれている compile.sh というファイルを代わりに実行してもよい.その場合,次のようにコマンドを実行する.

./compile.sh

コンパイルが成功すれば,grader という実行ファイルが生成される.

実際の採点プログラムは,採点プログラムのサンプルとは異なることに注意すること. 採点プログラムのサンプルは単一のプロセスとして起動する. このプログラムは,標準入力から入力を読み込み,標準出力に結果を出力する.

採点プログラムのサンプルの入力

採点プログラムのサンプルは標準入力から以下の形式で入力を読み込む.

N M
A_0 B_0
\vdots
A_{M-1} B_{M-1}

採点プログラムのサンプルの出力

採点プログラムのサンプルは標準出力へ以下の情報を出力する (引用符は実際には出力されない).

  • 不正解 [3] ~ [12] のいずれかの場合,不正解の種類が "Wrong Answer [5]" のように出力される.
  • そうでない場合,関数 query の呼び出し回数と,関数 solve の戻り値が "Accepted: 30 true" のように出力される. 採点プログラムのサンプルは,実際の採点プログラムと違って不正解 [1], [2] であるか,つまり関数 solve の戻り値が正しいかを判定しないことに注意せよ.

採点プログラムのサンプルは,不正解 [3] ~ [12] のいずれかの不正解の条件が満たされた時点で実行を終了する. 実行するプログラムが不正解 [3] ~ [12] のうち,複数の条件を満たした場合,表示される不正解の種類はそれらのうち 1 つのみである.

採点に関する注意

実際の採点プログラムは適応的 (adaptive) ではなく,やりとりの初めから固定された答えを持つ.


制約

  • 2 \leqq N \leqq 500
  • 1 \leqq M \leqq 1\,000
  • 0 \leqq A_i \leqq N - 1 (0 \leqq i \leqq M - 1).
  • 0 \leqq B_i \leqq N - 1 (0 \leqq i \leqq M - 1).
  • A_i \neq B_i (0 \leqq i \leqq M - 1).
  • (A_i, B_i) \neq (A_j, B_j) かつ (A_i, B_i) \neq (B_j, A_j) (0 \leqq i < j \leqq M - 1).
  • N, M, A_i, B_i は整数である (0 \leqq i \leqq M - 1).

小課題

  1. (10 点) N \leqq 100M = N - 1B_i = A_{i+1} (0 \leqq i \leqq N - 3),N 個の値 A_0, A_1, \ldots ,A_{N-2}, B_{N-2} はすべて異なる.
  2. (12 点) M = N - 1B_i = A_{i+1} (0 \leqq i \leqq N - 3),N 個の値 A_0, A_1, \ldots ,A_{N-2}, B_{N-2} はすべて異なる.
  3. (27 点) N \leqq 100A_i \neq A_j (0 \leqq i < j \leqq M - 1).
  4. (18 点) A_i \neq A_j (0 \leqq i < j \leqq M - 1).
  5. (17 点) N \leqq 100
  6. (16 点) 追加の制約はない.

やりとりの例

採点プログラムのサンプルが読み込む入力の例と,それに対応する関数の呼び出しの例を以下に示す.

入力例 1

5 6
0 2
2 1
0 3
3 2
3 4
4 1

1 回目の \texttt{query} の呼び出しにおける 2 回の電圧の設定と,電流が流れる電気抵抗は以下の通りである.

  • 1 回目は節点 0, 1 を「低電圧」に,節点 2, 3, 4 を「高電圧」に設定する.このとき,電気抵抗 1, 5 に電流が流れる.
  • 2 回目は節点 3, 4 を「低電圧」に,節点 0, 1, 2 を「高電圧」に設定する.このとき,電気抵抗 2 に電流が流れる.

1 回目の電圧の設定の方が電流が流れる電気抵抗の本数が多いため,戻り値は -1 である.

この入力例は小課題 5, 6 の制約を満たす.

コンテストサイトからダウンロードできるファイルのうち,sample-01-in.txt は入力例 1 に対応する. また,sample-02-in.txt はすべての小課題の制約を満たし,sample-03-in.txt は小課題 3, 4, 5, 6 の制約を満たす.