Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 266 点
問題文
高橋君のクラスには N 人の生徒がいます。各生徒には 0 から N - 1 までの出席番号が割り振られています。最初、出席番号 0 の生徒だけがある噂を知っています。
噂は、それを知っている生徒が別の生徒に直接伝えることで広まります。噂の伝達に関する記録が M 件与えられ、i 番目の記録 (a_i, b_i) は「出席番号 a_i の生徒が出席番号 b_i の生徒に噂を伝えようとした」ことを表します。
記録は i = 1, 2, \ldots, M の順に処理されます。i 番目の記録を処理するとき、出席番号 a_i の生徒がその時点で噂を知っていれば、出席番号 b_i の生徒も噂を知っている状態になります(すでに知っている場合は何も変わりません)。出席番号 a_i の生徒がその時点で噂を知らなければ、この記録では何も起こりません。
ある記録によって新たに噂を知った生徒は、それ以降の記録において噂を伝える側になることができます。
全ての記録を処理した後、最終的に噂を知っている生徒の人数を求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- 0 \leq M \leq 10^5
- 0 \leq a_i \leq N - 1
- 0 \leq b_i \leq N - 1
- a_i \neq b_i
- 同じ (a_i, b_i) の組が複数回現れることがある
- 入力はすべて整数である
入力
入力は以下の形式で与えられます。
N M a_1 b_1 a_2 b_2 \vdots a_M b_M
1 行目には、生徒の人数 N と伝達記録の件数 M がスペース区切りで与えられます。続く M 行の i 行目には、i 番目の伝達記録を表す 2 つの整数 a_i, b_i がスペース区切りで与えられます。
出力
最終的に噂を知っている生徒の人数を 1 行で出力してください。
入力例 1
4 3 0 1 1 2 3 2
出力例 1
3
入力例 2
4 3 1 2 0 1 2 3
出力例 2
2
入力例 3
8 13 0 3 2 4 3 2 4 5 5 1 1 7 6 7 7 6 0 2 2 4 4 5 5 6 6 7
出力例 3
7
入力例 4
15 28 0 4 1 2 4 1 2 3 3 5 6 7 5 6 8 9 7 8 10 11 11 12 12 13 13 14 0 10 9 10 14 2 4 6 6 1 1 9 9 11 11 13 13 12 12 14 14 5 5 6 6 7 7 8 8 9
出力例 4
13
入力例 5
1 0
出力例 5
1
Score : 266 pts
Problem Statement
There are N students in Takahashi's class. Each student is assigned an attendance number from 0 to N - 1. Initially, only the student with attendance number 0 knows a certain rumor.
The rumor spreads when a student who knows it directly tells it to another student. You are given M records of rumor transmission attempts. The i-th record (a_i, b_i) means "the student with attendance number a_i attempted to tell the rumor to the student with attendance number b_i."
The records are processed in order i = 1, 2, \ldots, M. When processing the i-th record, if the student with attendance number a_i knows the rumor at that point, then the student with attendance number b_i also comes to know the rumor (if they already know it, nothing changes). If the student with attendance number a_i does not know the rumor at that point, nothing happens for this record.
A student who newly learns the rumor from a certain record can become a transmitter of the rumor in subsequent records.
After processing all records, find the total number of students who know the rumor.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 0 \leq M \leq 10^5
- 0 \leq a_i \leq N - 1
- 0 \leq b_i \leq N - 1
- a_i \neq b_i
- The same pair (a_i, b_i) may appear multiple times
- All input values are integers
Input
The input is given in the following format.
N M a_1 b_1 a_2 b_2 \vdots a_M b_M
The first line contains the number of students N and the number of transmission records M, separated by a space. The i-th of the following M lines contains two integers a_i and b_i representing the i-th transmission record, separated by a space.
Output
Output the number of students who know the rumor in the end, on a single line.
Sample Input 1
4 3 0 1 1 2 3 2
Sample Output 1
3
Sample Input 2
4 3 1 2 0 1 2 3
Sample Output 2
2
Sample Input 3
8 13 0 3 2 4 3 2 4 5 5 1 1 7 6 7 7 6 0 2 2 4 4 5 5 6 6 7
Sample Output 3
7
Sample Input 4
15 28 0 4 1 2 4 1 2 3 3 5 6 7 5 6 8 9 7 8 10 11 11 12 12 13 13 14 0 10 9 10 14 2 4 6 6 1 1 9 9 11 11 13 13 12 12 14 14 5 5 6 6 7 7 8 8 9
Sample Output 4
13
Sample Input 5
1 0
Sample Output 5
1
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 333 点
問題文
高橋君は音楽フェスティバルのDJです。次のステージでは N 曲を再生する予定ですが、観客が違和感なく楽しめるように、曲の再生順序を工夫する必要があります。
各曲 i(1 \leq i \leq N)には「テンポ値」と呼ばれる整数 A_i が定められています。テンポ値は曲の雰囲気を数値化したもので、値が近い曲同士は似た印象を持ちます。また、非負整数 D が与えられます。テンポ値の差の絶対値が D 以下である 2 曲は「似ている」とみなされます。
高橋君は N 曲すべてをちょうど 1 回ずつ、好きな順序で再生します。再生順で j 番目に再生される曲のテンポ値を B_j とします。すなわち、(B_1, B_2, \ldots, B_N) は (A_1, A_2, \ldots, A_N) の並べ替え(多重集合として一致するもの)です。
再生順で j 番目の曲の「違和感スコア」を次のように定めます:
- j = 1(1 曲目)のとき、違和感スコアは 0 です。
- j \geq 2(2 曲目以降)のとき、過去に再生された曲のテンポ値 B_1, B_2, \ldots, B_{j-1} の中に、j 番目の曲と似ているもの、すなわち |B_j - B_k| \leq D を満たす k(1 \leq k \leq j-1)が存在するかどうかで次のように定まります:
- 存在する場合:過去に似たテンポの曲が再生されていたため、観客は違和感を覚えません。違和感スコアは 0 です。
- 存在しない場合:過去に再生されたどの曲ともテンポ値の差の絶対値が D より大きいため、直前の曲からの急激なテンポ変化が違和感として現れます。違和感スコアは |B_j - B_{j-1}| です。
全ての曲の違和感スコアの合計値を「違和感の総和」と呼びます。高橋君が再生順序を最適に選んだとき、違和感の総和の最小値を求めてください。
制約
- 1 \leq N \leq 10^6
- 0 \leq D \leq 10^9
- 1 \leq A_i \leq 10^9(1 \leq i \leq N)
- 入力は全て整数である
入力
N D A_1 A_2 \ldots A_N
- 1 行目には、曲の数を表す整数 N と、似ている曲かどうかを判定する閾値を表す非負整数 D が、スペース区切りで与えられる。
- 2 行目には、各曲のテンポ値を表す N 個の整数 A_1, A_2, \ldots, A_N が、スペース区切りで与えられる。
出力
違和感の総和の最小値を整数として 1 行で出力せよ。
入力例 1
5 3 10 13 20 22 40
出力例 1
25
入力例 2
6 0 5 5 10 10 15 30
出力例 2
25
入力例 3
15 4 32 10 14 18 50 54 55 80 83 120 121 124 200 205 209
出力例 3
175
入力例 4
30 10 100 5 15 25 40 42 60 70 71 85 95 130 131 140 150 300 305 315 500 510 520 700 701 710 900 920 940 960 980 1000
出力例 4
882
入力例 5
1 1000000000 1000000000
出力例 5
0
Score : 333 pts
Problem Statement
Takahashi is a DJ at a music festival. He plans to play N songs in his next stage, but he needs to carefully arrange the playing order so that the audience can enjoy it without feeling any discomfort.
Each song i (1 \leq i \leq N) has an integer "tempo value" A_i. The tempo value quantifies the mood of the song, and songs with close tempo values have a similar impression. Additionally, a non-negative integer D is given. Two songs whose absolute difference in tempo values is at most D are considered "similar."
Takahashi will play all N songs exactly once in any order he chooses. Let B_j be the tempo value of the j-th song played in this order. That is, (B_1, B_2, \ldots, B_N) is a permutation of (A_1, A_2, \ldots, A_N) (as multisets).
The "discomfort score" of the j-th song in the playing order is defined as follows:
- If j = 1 (the first song), the discomfort score is 0.
- If j \geq 2 (the second song or later), it is determined by whether there exists a song similar to the j-th song among the previously played songs B_1, B_2, \ldots, B_{j-1} (i.e., whether there exists k (1 \leq k \leq j-1) such that |B_j - B_k| \leq D):
- If it exists: Since a song with a similar tempo was played in the past, the audience does not feel discomfort. The discomfort score is 0.
- If it does not exist: Since the absolute difference in tempo values between this song and any of the previously played songs is strictly greater than D, the sudden change in tempo from the immediately preceding song causes discomfort. The discomfort score is |B_j - B_{j-1}|.
The sum of the discomfort scores of all songs is called the "total discomfort." Find the minimum possible total discomfort when Takahashi chooses the playing order optimally.
Constraints
- 1 \leq N \leq 10^6
- 0 \leq D \leq 10^9
- 1 \leq A_i \leq 10^9 (1 \leq i \leq N)
- All input values are integers.
Input
N D A_1 A_2 \ldots A_N
- The first line contains the integer N, representing the number of songs, and the non-negative integer D, representing the threshold for judging whether songs are similar, separated by a space.
- The second line contains N integers A_1, A_2, \ldots, A_N, representing the tempo values of each song, separated by spaces.
Output
Print the minimum possible total discomfort as an integer in a single line.
Sample Input 1
5 3 10 13 20 22 40
Sample Output 1
25
Sample Input 2
6 0 5 5 10 10 15 30
Sample Output 2
25
Sample Input 3
15 4 32 10 14 18 50 54 55 80 83 120 121 124 200 205 209
Sample Output 3
175
Sample Input 4
30 10 100 5 15 25 40 42 60 70 71 85 95 130 131 140 150 300 305 315 500 510 520 700 701 710 900 920 940 960 980 1000
Sample Output 4
882
Sample Input 5
1 1000000000 1000000000
Sample Output 5
0
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 366 点
問題文
ある学校には N 人の生徒がおり、生徒には 1 から N までの番号が付けられています。
この学校では噂が独特な仕方で広まります。生徒間には M 個の有向の伝達関係があり、i 番目の伝達関係は順序対 (u_i, v_i) で表されます。これは「生徒 u_i があるステップで噂を受け取ると、次のステップで生徒 v_i に噂が伝わる」という一方向の関係を意味します。
噂の伝播はステップ 0, 1, 2, \ldots と同期的に進行します。各ステップ t で噂を受け取る生徒の集合を A_t とします。A_t は以下のように定まります。
- ステップ 0: 最初に、生徒 S が外部から噂を聞きます。A_0 = \{S\} です。
- ステップ t \geq 1: 直前のステップ t-1 で噂を受け取った生徒(すなわち A_{t-1} の要素)だけが噂を伝えます。具体的には、伝達関係 (u, v) であって u \in A_{t-1} を満たすものすべてについて、生徒 v がステップ t で噂を受け取ります。すなわち、
A_t = \{v \mid \text{ある } u \in A_{t-1} \text{ が存在して伝達関係 } (u, v) \text{ がある}\}
です。
ここで重要な点を補足します。
- 生徒が噂を伝えられるのは、その生徒自身が噂を受け取った直後の 1 ステップだけです。 A_{t-1} に含まれていない生徒は、たとえ過去のステップで噂を受け取ったことがあっても、ステップ t では誰にも噂を伝えません。
- A_t は集合であるため、ある生徒がステップ t で複数の生徒から同時に噂を受け取っても、A_t に含まれるのは 1 回と数えます。
- ある生徒が過去のステップで既に噂を聞いたことがあっても、再び別のステップで A_t に含まれることがあり得ます。特にグラフにサイクルがある場合には、同じ生徒が複数の異なるステップで噂を受け取ることがあります。
- A_{t-1} のどの要素からも伝達関係で到達できる生徒がいない場合、A_t = \emptyset(空集合)となり、以降のすべてのステップでも A_t = \emptyset です。
各生徒 i について、生徒 i が噂を受け取るステップの集合を T_i = \{t \geq 0 \mid i \in A_t\} とします。例えば、生徒 i がステップ 0, 2, 5 で噂を受け取ったなら T_i = \{0, 2, 5\} です。(サイクルの存在により T_i が無限集合になることもあり得ますが、以下のクエリでは K_j \leq N の範囲のみを問うため、有限の範囲だけを考えれば十分です。)
最初に噂を聞く生徒 S は 1 から N のいずれかの生徒を自由に選ぶことができます。
Q 個のクエリが与えられます。j 番目のクエリでは非負整数 K_j(0 \leq K_j \leq N)が与えられるので、以下の値を求めてください:
最初に噂を聞く生徒 S の選び方のうち、ステップ 0 からステップ K_j までの間(両端含む)にすべての生徒(1 から N まで)が少なくとも 1 回は噂を受け取るような S の個数。
すなわち、すべての生徒 i (1 \leq i \leq N) について T_i \cap \{0, 1, \ldots, K_j\} \neq \emptyset を満たすような S (1 \leq S \leq N) の個数を求めてください。
制約
- 1 \leq N \leq 2000
- 0 \leq M \leq \min(N(N-1), 50000)
- 1 \leq Q \leq 2 \times 10^5
- 1 \leq u_i, v_i \leq N
- u_i \neq v_i(自己ループはない)
- 伝達関係に重複はない。すなわち、i \neq j ならば (u_i, v_i) \neq (u_j, v_j)
- 0 \leq K_j \leq N
- 入力はすべて整数である
入力
N M Q u_1 v_1 u_2 v_2 \vdots u_M v_M K_1 K_2 \vdots K_Q
- 1 行目には、生徒の人数 N、伝達関係の個数 M、クエリの個数 Q が、スペース区切りで与えられる。
- 続く M 行のうち i 行目 (1 \leq i \leq M) には、i 番目の伝達関係を表す u_i と v_i がスペース区切りで与えられる。これは、生徒 u_i がステップ t で噂を受け取ったとき(u_i \in A_t)、ステップ t+1 で生徒 v_i が噂を受け取る(v_i \in A_{t+1})という有向の関係を表す。
- 続く Q 行のうち j 行目 (1 \leq j \leq Q) には、j 番目のクエリにおけるステップ数の上限 K_j が与えられる。
出力
Q 行出力せよ。j 行目には、j 番目のクエリに対する答え、すなわちステップ 0 からステップ K_j までの間にすべての生徒が少なくとも 1 回は噂を受け取るような、最初に噂を聞く生徒 S の個数を出力せよ。
入力例 1
4 4 5 1 2 2 3 3 4 4 1 0 1 2 3 4
出力例 1
0 0 0 4 4
入力例 2
3 2 4 1 2 2 1 0 1 2 3
出力例 2
0 0 0 0
入力例 3
8 14 8 1 2 1 3 2 4 3 4 4 5 5 6 6 4 5 7 7 8 8 5 2 6 3 7 6 8 8 1 0 1 2 3 4 5 6 8
出力例 3
0 0 0 3 7 8 8 8
入力例 4
16 40 12 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 16 16 1 1 5 1 9 2 6 2 10 3 7 3 11 4 8 4 12 5 9 5 13 6 10 6 14 7 11 7 15 8 12 8 16 9 13 9 1 10 14 10 2 11 15 11 3 12 16 12 4 0 1 2 3 4 5 6 8 10 12 15 16
出力例 4
0 0 0 0 0 13 14 16 16 16 16 16
入力例 5
1 0 4 0 1 0 1
出力例 5
1 1 1 1
Score : 366 pts
Problem Statement
A school has N students, numbered from 1 to N.
Rumors spread in a unique way at this school. There are M directed transmission relationships between students, and the i-th transmission relationship is represented by the ordered pair (u_i, v_i). This means a one-way relationship: "if student u_i receives a rumor at a certain step, the rumor is transmitted to student v_i at the next step."
The propagation of the rumor proceeds synchronously through steps 0, 1, 2, \ldots. Let A_t denote the set of students who receive the rumor at each step t. A_t is determined as follows:
- Step 0: Initially, student S hears the rumor from an external source. A_0 = \{S\}.
- Step t \geq 1: Only the students who received the rumor at the immediately preceding step t-1 (i.e., the elements of A_{t-1}) transmit the rumor. Specifically, for all transmission relationships (u, v) satisfying u \in A_{t-1}, student v receives the rumor at step t. That is,
A_t = \{v \mid \text{there exists } u \in A_{t-1} \text{ such that there is a transmission relationship } (u, v)\}
Here are some important supplementary notes:
- A student can only transmit the rumor during the single step immediately after they themselves received it. A student not in A_{t-1} does not transmit the rumor to anyone at step t, even if they received the rumor at some past step.
- Since A_t is a set, even if a student receives the rumor from multiple students simultaneously at step t, they are counted only once in A_t.
- Even if a student has already heard the rumor at a past step, they may be included in A_t again at a different step. In particular, when the graph contains cycles, the same student may receive the rumor at multiple different steps.
- If no student is reachable via transmission relationships from any element of A_{t-1}, then A_t = \emptyset (the empty set), and A_t = \emptyset for all subsequent steps as well.
For each student i, let T_i = \{t \geq 0 \mid i \in A_t\} be the set of steps at which student i receives the rumor. For example, if student i receives the rumor at steps 0, 2, 5, then T_i = \{0, 2, 5\}. (Due to the existence of cycles, T_i may be an infinite set, but since the queries below only ask about the range K_j \leq N, it suffices to consider only a finite range.)
The student S who first hears the rumor can be freely chosen from any of the students 1 to N.
Q queries are given. For the j-th query, a non-negative integer K_j (0 \leq K_j \leq N) is given. Determine the following value:
Among all choices of the student S who first hears the rumor, the number of choices of S such that all students (from 1 to N) receive the rumor at least once during the period from step 0 to step K_j (inclusive of both endpoints).
In other words, find the number of S (1 \leq S \leq N) such that for all students i (1 \leq i \leq N), T_i \cap \{0, 1, \ldots, K_j\} \neq \emptyset holds.
Constraints
- 1 \leq N \leq 2000
- 0 \leq M \leq \min(N(N-1), 50000)
- 1 \leq Q \leq 2 \times 10^5
- 1 \leq u_i, v_i \leq N
- u_i \neq v_i (no self-loops)
- There are no duplicate transmission relationships. That is, if i \neq j then (u_i, v_i) \neq (u_j, v_j)
- 0 \leq K_j \leq N
- All input values are integers
Input
N M Q u_1 v_1 u_2 v_2 \vdots u_M v_M K_1 K_2 \vdots K_Q
- The first line contains the number of students N, the number of transmission relationships M, and the number of queries Q, separated by spaces.
- The i-th of the following M lines (1 \leq i \leq M) contains u_i and v_i representing the i-th transmission relationship, separated by a space. This represents the directed relationship that when student u_i receives the rumor at step t (u_i \in A_t), student v_i receives the rumor at step t+1 (v_i \in A_{t+1}).
- The j-th of the following Q lines (1 \leq j \leq Q) contains the upper limit on the number of steps K_j for the j-th query.
Output
Output Q lines. On the j-th line, output the answer to the j-th query, i.e., the number of students S who can be chosen as the first to hear the rumor such that all students receive the rumor at least once during the period from step 0 to step K_j.
Sample Input 1
4 4 5 1 2 2 3 3 4 4 1 0 1 2 3 4
Sample Output 1
0 0 0 4 4
Sample Input 2
3 2 4 1 2 2 1 0 1 2 3
Sample Output 2
0 0 0 0
Sample Input 3
8 14 8 1 2 1 3 2 4 3 4 4 5 5 6 6 4 5 7 7 8 8 5 2 6 3 7 6 8 8 1 0 1 2 3 4 5 6 8
Sample Output 3
0 0 0 3 7 8 8 8
Sample Input 4
16 40 12 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 16 16 1 1 5 1 9 2 6 2 10 3 7 3 11 4 8 4 12 5 9 5 13 6 10 6 14 7 11 7 15 8 12 8 16 9 13 9 1 10 14 10 2 11 15 11 3 12 16 12 4 0 1 2 3 4 5 6 8 10 12 15 16
Sample Output 4
0 0 0 0 0 13 14 16 16 16 16 16
Sample Input 5
1 0 4 0 1 0 1
Sample Output 5
1 1 1 1
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
三角形状に並べられた数が書かれたボードがあります。上から i 段目には i 個のマスが横一列に並んでおり、i 段目の左から p 番目(1 \leq p \leq i)のマスに書かれた数を A_{i,p} とします。ボードは全部で N 段あります。
非負整数 x に対して、x の十進表記における各桁の数字の和を S(x) と定義します。例えば S(0) = 0, S(19) = 10, S(999) = 27 です。
さらに、非負整数 V に対して、f(V) = \max_{0 \leq Y \leq V} S(Y) と定義します。すなわち、f(V) は 0 以上 V 以下の整数の中で桁和が最大となるものの桁和です。
高橋君は、あるマスにコマを置き、以下の 3 つの操作のうち 1 つを選んで行うことを繰り返します。
- とどまる:現在のマスにとどまる。(現在の段によらず常に選択可能。)
- 真下に移動:現在 i 段目の左から p 番目のマスにいるとき、i+1 段目の左から p 番目のマスへ移動する。(i < N のときのみ選択可能。)
- 右下に移動:現在 i 段目の左から p 番目のマスにいるとき、i+1 段目の左から p+1 番目のマスへ移動する。(i < N のときのみ選択可能。)
Q 個の問い合わせが与えられます。j 番目の問い合わせでは、開始段 L_j、開始位置 P_j、操作回数 T_j が与えられます。
P_j > L_j の場合、L_j 段目には左から P_j 番目のマスが存在しないため、この問い合わせに対しては NA を出力してください。
P_j \leq L_j の場合、コマを L_j 段目の左から P_j 番目のマスに置き、ちょうど T_j 回の操作を行います。T_j 回の操作列として考えうるすべての選び方を考えたとき、最終的にコマが位置しうるマス全体の集合を到達可能なマスの集合と呼びます。(例えば「とどまる」を T_j 回選べば開始マスに到達できるため、開始マスは常に到達可能です。)
到達可能なマスの集合に含まれるすべてのマスについて、そのマスに書かれた値 A_{i,p} に対する f(A_{i,p}) の値を求め、その中の最大値を出力してください。
制約
- 1 \leq N \leq 1000
- 1 \leq Q \leq 10^5
- NQ \leq 10^7
- 0 \leq A_{i,p} \leq 10^{18}
- 1 \leq L_j \leq N
- 1 \leq P_j \leq N(P_j > L_j の場合は開始マスが存在しない)
- 0 \leq T_j \leq 10^9
- 入力はすべて整数である
入力
N Q
A_{1,1}
A_{2,1} A_{2,2}
\vdots
A_{N,1} A_{N,2} \ldots A_{N,N}
L_1 P_1 T_1
L_2 P_2 T_2
\vdots
L_Q P_Q T_Q
- 1 行目には、ボードの段数を表す整数 N と、問い合わせの個数を表す整数 Q が、スペース区切りで与えられる。
- 続く N 行には、各段のマスに書かれた数が順に与えられる。
- 1 + i 行目には、i 段目の i 個の値 A_{i,1}, A_{i,2}, \ldots, A_{i,i} がスペース区切りで与えられる。
- 続く Q 行には、問い合わせが与えられる。
- 1 + N + j 行目には、j 番目の問い合わせの開始段 L_j、開始位置 P_j、操作回数 T_j がスペース区切りで与えられる。
出力
Q 行出力せよ。
j 行目には、j 番目の問い合わせに対する答えを出力せよ。
開始マスが存在しない場合は NA を、そうでない場合は答えとなる整数を出力せよ。
入力例 1
4 5 5 12 30 7 99 100 0 19 88 123 1 1 0 1 1 1 2 1 2 3 2 10 4 4 1
出力例 1
5 11 18 18 18
入力例 2
3 5 8 10 29 1000 909 555 1 2 0 2 3 5 1 1 2 3 1 0 2 2 1
出力例 2
NA NA 27 27 26
入力例 3
7 10 0 9 10 18 99 100 123 4567 890 9999 1000000000000000000 999999999999999999 123456789012345678 400000000000000000 101010101010101010 1 20 300 4000 50000 600000 987654321 123456789 111111111 222222222 333333333 444444444 555555555 1 1 6 2 2 3 3 1 0 3 4 1 5 3 2 6 6 1000000000 7 1 5 4 2 1 2 1 4 7 7 0
出力例 3
162 162 9 NA 153 76 80 162 162 76
入力例 4
12 20 42 7 88 1234 99999 100000 314159 271828 161803 141421 999999999999999999 1000000000000000000 987654321098765432 123456789012345678 555555555555555555 0 1 8 17 26 35 44 53 62 71 80 89 98 107 116 125 134 143 152 161 170 179 188 197 206 215 224 233 242 251 260 269 278 287 296 305 314 323 332 341 350 359 368 377 386 395 404 413 422 431 440 449 458 467 476 485 494 503 512 521 530 539 548 1 1 11 1 2 5 2 1 0 2 2 100 3 3 2 4 1 8 5 2 7 5 6 1 6 4 3 7 7 0 8 3 1000000000 9 9 4 10 5 2 11 1 1 12 12 0 12 1 100 4 4 10 6 1 5 3 1 9 10 11 0
出力例 4
162 NA 7 162 161 162 162 NA 19 17 22 22 22 21 22 21 157 20 162 NA
入力例 5
1 4 1000000000000000000 1 1 0 1 1 1 1 1 1000000000 1 1 999999999
出力例 5
162 162 162 162
Score : 400 pts
Problem Statement
There is a board with numbers arranged in a triangular shape. The i-th row from the top contains i cells arranged in a horizontal line, and the number written in the p-th cell from the left in the i-th row (1 \leq p \leq i) is denoted A_{i,p}. The board has N rows in total.
For a non-negative integer x, define S(x) as the sum of the digits in the decimal representation of x. For example, S(0) = 0, S(19) = 10, S(999) = 27.
Furthermore, for a non-negative integer V, define f(V) = \max_{0 \leq Y \leq V} S(Y). That is, f(V) is the maximum digit sum among all integers from 0 to V inclusive.
Takahashi places a piece on a cell and repeatedly performs one of the following 3 operations:
- Stay: Stay on the current cell. (Can always be chosen regardless of the current row.)
- Move directly below: When currently on the p-th cell from the left in row i, move to the p-th cell from the left in row i+1. (Can only be chosen when i < N.)
- Move diagonally right-down: When currently on the p-th cell from the left in row i, move to the (p+1)-th cell from the left in row i+1. (Can only be chosen when i < N.)
Q queries are given. The j-th query provides a starting row L_j, a starting position P_j, and a number of operations T_j.
If P_j > L_j, then the P_j-th cell from the left does not exist in row L_j, so output NA for this query.
If P_j \leq L_j, place the piece on the P_j-th cell from the left in row L_j and perform exactly T_j operations. Considering all possible choices of sequences of T_j operations, the set of all cells where the piece can ultimately be located is called the set of reachable cells. (For example, choosing "Stay" T_j times reaches the starting cell, so the starting cell is always reachable.)
For all cells in the set of reachable cells, compute f(A_{i,p}) for the value A_{i,p} written on that cell, and output the maximum among these values.
Constraints
- 1 \leq N \leq 1000
- 1 \leq Q \leq 10^5
- NQ \leq 10^7
- 0 \leq A_{i,p} \leq 10^{18}
- 1 \leq L_j \leq N
- 1 \leq P_j \leq N (if P_j > L_j, the starting cell does not exist)
- 0 \leq T_j \leq 10^9
- All inputs are integers
Input
N Q
A_{1,1}
A_{2,1} A_{2,2}
\vdots
A_{N,1} A_{N,2} \ldots A_{N,N}
L_1 P_1 T_1
L_2 P_2 T_2
\vdots
L_Q P_Q T_Q
- The first line contains the integer N representing the number of rows in the board and the integer Q representing the number of queries, separated by a space.
- The following N lines give the numbers written on the cells of each row in order.
- The (1 + i)-th line contains the i values A_{i,1}, A_{i,2}, \ldots, A_{i,i} of the i-th row, separated by spaces.
- The following Q lines give the queries.
- The (1 + N + j)-th line contains the starting row L_j, starting position P_j, and number of operations T_j for the j-th query, separated by spaces.
Output
Output Q lines.
The j-th line should contain the answer to the j-th query.
If the starting cell does not exist, output NA; otherwise, output the integer that is the answer.
Sample Input 1
4 5 5 12 30 7 99 100 0 19 88 123 1 1 0 1 1 1 2 1 2 3 2 10 4 4 1
Sample Output 1
5 11 18 18 18
Sample Input 2
3 5 8 10 29 1000 909 555 1 2 0 2 3 5 1 1 2 3 1 0 2 2 1
Sample Output 2
NA NA 27 27 26
Sample Input 3
7 10 0 9 10 18 99 100 123 4567 890 9999 1000000000000000000 999999999999999999 123456789012345678 400000000000000000 101010101010101010 1 20 300 4000 50000 600000 987654321 123456789 111111111 222222222 333333333 444444444 555555555 1 1 6 2 2 3 3 1 0 3 4 1 5 3 2 6 6 1000000000 7 1 5 4 2 1 2 1 4 7 7 0
Sample Output 3
162 162 9 NA 153 76 80 162 162 76
Sample Input 4
12 20 42 7 88 1234 99999 100000 314159 271828 161803 141421 999999999999999999 1000000000000000000 987654321098765432 123456789012345678 555555555555555555 0 1 8 17 26 35 44 53 62 71 80 89 98 107 116 125 134 143 152 161 170 179 188 197 206 215 224 233 242 251 260 269 278 287 296 305 314 323 332 341 350 359 368 377 386 395 404 413 422 431 440 449 458 467 476 485 494 503 512 521 530 539 548 1 1 11 1 2 5 2 1 0 2 2 100 3 3 2 4 1 8 5 2 7 5 6 1 6 4 3 7 7 0 8 3 1000000000 9 9 4 10 5 2 11 1 1 12 12 0 12 1 100 4 4 10 6 1 5 3 1 9 10 11 0
Sample Output 4
162 NA 7 162 161 162 162 NA 19 17 22 22 22 21 22 21 157 20 162 NA
Sample Input 5
1 4 1000000000000000000 1 1 0 1 1 1 1 1 1000000000 1 1 999999999
Sample Output 5
162 162 162 162
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 433 点
問題文
高橋君は N ステップからなる信号変換器を管理しています。この変換器は 1 以上 K 以下の整数値の信号を受け取り、変換処理を施して出力します。
入力された信号は、第 1 ステップ、第 2 ステップ、\ldots、第 N ステップの順に変換処理を受けます。第 i ステップ (1 \leq i \leq N) は整数の組 (L_i, R_i, X_i) で定められ、次の処理を行います。
- そのステップの処理開始時点での信号値を v とする。L_i \leq v \leq R_i ならば、信号値を X_i に置き換える。そうでなければ、信号値は変化しない。
各ステップの処理後の信号値が次のステップへの入力となり、第 N ステップの処理後の信号値が変換器の最終的な出力となります。入力信号値が整数 s のとき、最終的な出力信号値を F(s) と定義します。
青木君から Q 個の質問が届きます。j 番目の質問では整数 A_j, B_j が与えられます。入力信号値を A_j 以上 B_j 以下のすべての整数としたとき、出力信号値として現れる値の種類数、すなわち集合 \{F(s) \mid s \text{ は整数}, A_j \leq s \leq B_j\} の要素数を求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq K \leq 2 \times 10^5
- 1 \leq Q \leq 2 \times 10^5
- N \times K \leq 10^7
- N + Q \leq 2 \times 10^5
- 1 \leq L_i \leq R_i \leq K (1 \leq i \leq N)
- 1 \leq X_i \leq K (1 \leq i \leq N)
- 1 \leq A_j \leq B_j \leq K (1 \leq j \leq Q)
- 入力はすべて整数である。
入力
N K Q L_1 R_1 X_1 L_2 R_2 X_2 \vdots L_N R_N X_N A_1 B_1 A_2 B_2 \vdots A_Q B_Q
- 1 行目には、変換ステップ数 N、信号値の取りうる範囲の上限 K、質問数 Q がスペース区切りで与えられる。
- 続く N 行のうち i 行目には、第 i ステップを定める整数 L_i, R_i, X_i がスペース区切りで与えられる。
- 続く Q 行のうち j 行目には、j 番目の質問を表す整数 A_j, B_j がスペース区切りで与えられる。
出力
ans_1 ans_2 \vdots ans_Q
Q 行出力せよ。j 行目には、j 番目の質問に対する答え、すなわち集合 \{F(s) \mid s \text{ は整数}, A_j \leq s \leq B_j\} の要素数を出力せよ。
入力例 1
3 6 4 2 4 5 5 6 1 1 3 4 1 6 1 1 2 4 5 6
出力例 1
1 1 1 1
入力例 2
4 8 5 1 3 7 4 8 2 2 2 5 5 7 8 1 4 3 6 7 8 1 8 5 5
出力例 2
1 1 1 1 1
入力例 3
10 20 8 1 5 12 10 15 3 3 12 18 18 20 6 6 9 14 14 14 2 2 4 19 16 19 7 7 13 1 1 1 20 1 20 1 5 6 10 11 15 16 20 3 17 8 8 14 19
出力例 3
1 1 1 1 1 1 1 1
入力例 4
20 50 12 1 10 25 11 20 5 21 30 40 31 40 15 41 50 1 5 25 33 26 45 12 1 3 50 48 50 7 7 18 22 19 32 44 33 44 9 9 9 30 30 35 2 2 8 16 16 16 41 41 49 6 6 14 28 28 38 11 11 27 35 1 50 1 10 11 20 21 30 31 40 41 50 5 45 13 37 25 25 2 49 17 33 39 50
出力例 4
1 1 1 1 1 1 1 1 1 1 1 1
入力例 5
1 1 1 1 1 1 1 1
出力例 5
1
Score : 433 pts
Problem Statement
Takahashi manages a signal converter consisting of N steps. This converter receives an integer signal with a value between 1 and K inclusive, applies conversion processing, and produces an output.
The input signal undergoes conversion processing in order: step 1, step 2, \ldots, step N. Step i (1 \leq i \leq N) is defined by a tuple of integers (L_i, R_i, X_i) and performs the following operation:
- Let v be the signal value at the start of this step's processing. If L_i \leq v \leq R_i, replace the signal value with X_i. Otherwise, the signal value remains unchanged.
The signal value after each step's processing becomes the input to the next step, and the signal value after step N's processing becomes the final output of the converter. When the input signal value is an integer s, we define the final output signal value as F(s).
Aoki sends Q queries. In the j-th query, integers A_j and B_j are given. When the input signal values are all integers from A_j to B_j inclusive, find the number of distinct values that appear as output signal values, that is, the number of elements in the set \{F(s) \mid s \text{ is an integer}, A_j \leq s \leq B_j\}.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq K \leq 2 \times 10^5
- 1 \leq Q \leq 2 \times 10^5
- N \times K \leq 10^7
- N + Q \leq 2 \times 10^5
- 1 \leq L_i \leq R_i \leq K (1 \leq i \leq N)
- 1 \leq X_i \leq K (1 \leq i \leq N)
- 1 \leq A_j \leq B_j \leq K (1 \leq j \leq Q)
- All inputs are integers.
Input
N K Q L_1 R_1 X_1 L_2 R_2 X_2 \vdots L_N R_N X_N A_1 B_1 A_2 B_2 \vdots A_Q B_Q
- The first line contains the number of conversion steps N, the upper bound of possible signal values K, and the number of queries Q, separated by spaces.
- In the following N lines, the i-th line contains the integers L_i, R_i, X_i that define step i, separated by spaces.
- In the following Q lines, the j-th line contains the integers A_j, B_j representing the j-th query, separated by spaces.
Output
ans_1 ans_2 \vdots ans_Q
Output Q lines. The j-th line should contain the answer to the j-th query, that is, the number of elements in the set \{F(s) \mid s \text{ is an integer}, A_j \leq s \leq B_j\}.
Sample Input 1
3 6 4 2 4 5 5 6 1 1 3 4 1 6 1 1 2 4 5 6
Sample Output 1
1 1 1 1
Sample Input 2
4 8 5 1 3 7 4 8 2 2 2 5 5 7 8 1 4 3 6 7 8 1 8 5 5
Sample Output 2
1 1 1 1 1
Sample Input 3
10 20 8 1 5 12 10 15 3 3 12 18 18 20 6 6 9 14 14 14 2 2 4 19 16 19 7 7 13 1 1 1 20 1 20 1 5 6 10 11 15 16 20 3 17 8 8 14 19
Sample Output 3
1 1 1 1 1 1 1 1
Sample Input 4
20 50 12 1 10 25 11 20 5 21 30 40 31 40 15 41 50 1 5 25 33 26 45 12 1 3 50 48 50 7 7 18 22 19 32 44 33 44 9 9 9 30 30 35 2 2 8 16 16 16 41 41 49 6 6 14 28 28 38 11 11 27 35 1 50 1 10 11 20 21 30 31 40 41 50 5 45 13 37 25 25 2 49 17 33 39 50
Sample Output 4
1 1 1 1 1 1 1 1 1 1 1 1
Sample Input 5
1 1 1 1 1 1 1 1
Sample Output 5
1