A - Distributing Presents

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 233 点

問題文

高橋君は、 N 人の友人にプレゼントを届けるため、友人 1 、友人 2 、…、友人 N の順に家を訪問します。

高橋君は最初、プレゼントを N 個持っています。しかし、各友人の家に向かう道中には障害物があり、つまずいてプレゼントを落としてしまうことがあります。

友人 i の家に向かう途中で、高橋君はちょうど F_i 回つまずきます。つまずくたびに、その時点で持っているプレゼントの数が 1 個以上であれば、プレゼントを 1 個落として失ってしまい、持っているプレゼントの数が 1 減ります。持っているプレゼントが 0 個の状態でつまずいた場合は、何も起こりません。

友人 i の家に到着したとき、高橋君が持っているプレゼントが 1 個以上であれば、プレゼントを 1 個渡します。このとき、持っているプレゼントの数が 1 減ります。プレゼントを 1 個も持っていなければ、何も渡せません。

すべての友人を訪問し終えた後、プレゼントを受け取ることができた友人の人数を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq F_i \leq 10^9
  • 入力はすべて整数

入力

N
F_1
F_2
\vdots
F_N
  • 1 行目には、友人の人数を表す整数 N が与えられる。
  • 1 + i 行目 (1 \leq i \leq N) には、友人 i の家に向かう途中でつまずく回数 F_i が与えられる。

出力

プレゼントを受け取ることができた友人の人数を 1 行で出力してください。


入力例 1

5
0
1
0
2
0

出力例 1

3

入力例 2

4
0
0
0
0

出力例 2

4

入力例 3

10
0
2
0
1
0
3
0
0
1
0

出力例 3

5

入力例 4

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

出力例 4

15

入力例 5

1
1000000000

出力例 5

0

Score : 233 pts

Problem Statement

Takahashi will visit his friends' houses in the order of Friend 1, Friend 2, ..., Friend N to deliver presents to his N friends.

Takahashi initially holds N presents. However, there are obstacles on the way to each friend's house, and he may stumble and drop presents.

On the way to Friend i's house, Takahashi stumbles exactly F_i times. Each time he stumbles, if he holds at least 1 present at that moment, he drops and loses 1 present, reducing the number of presents he holds by 1. If he stumbles while holding 0 presents, nothing happens.

Upon arriving at Friend i's house, if Takahashi holds at least 1 present, he gives 1 present to the friend. In this case, the number of presents he holds decreases by 1. If he holds no presents, he cannot give anything.

Find the number of friends who received a present after Takahashi has visited all his friends.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq F_i \leq 10^9
  • All input values are integers.

Input

N
F_1
F_2
\vdots
F_N
  • The 1-st line contains an integer N, representing the number of friends.
  • The (1 + i)-th line (1 \leq i \leq N) contains an integer F_i, representing the number of times Takahashi stumbles on the way to Friend i's house.

Output

Print the number of friends who received a present in a single line.


Sample Input 1

5
0
1
0
2
0

Sample Output 1

3

Sample Input 2

4
0
0
0
0

Sample Output 2

4

Sample Input 3

10
0
2
0
1
0
3
0
0
1
0

Sample Output 3

5

Sample Input 4

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

Sample Output 4

15

Sample Input 5

1
1000000000

Sample Output 5

0
B - Word Search

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 333 点

問題文

高橋君は図書館の司書として働いており、書棚に並んだ本のタイトルについて利用者からの問い合わせに対応しています。

高橋君が管理する書棚には N 冊の本が一列に並んでおり、左から i 番目の本のタイトルは文字列 S_i です(1 \leq i \leq N)。なお、同じタイトルの本が複数存在することもあります。ある日、青木君が図書館を訪れ、書棚の本について Q 個の問い合わせを行いました。

j 番目の問い合わせ(1 \leq j \leq Q)では、青木君は 2 つの整数 L_j, R_j と 1 つの文字列 T_j を指定します。高橋君は、左から L_j 番目から R_j 番目までの本(両端を含む)の中に、タイトルが文字列として T_j と完全に一致する本が何冊あるかを答えなければなりません。

すべての問い合わせに対する答えを求めてください。

制約

  • 1 \leq N \leq 10^5
  • 1 \leq Q \leq 10^5
  • N, Q は整数
  • S_i は英小文字からなる長さ 1 以上 10 以下の文字列 (1 \leq i \leq N)
  • 1 \leq L_j \leq R_j \leq N (1 \leq j \leq Q)
  • L_j, R_j は整数
  • T_j は英小文字からなる長さ 1 以上 10 以下の文字列 (1 \leq j \leq Q)

入力

N Q
S_1 S_2 \ldots S_N
L_1 R_1 T_1
L_2 R_2 T_2
\vdots
L_Q R_Q T_Q
  • 1 行目には、本の冊数 N と問い合わせの数 Q が、スペース区切りで与えられる。
  • 2 行目には、書棚に並んだ N 冊の本のタイトル S_1, S_2, \ldots, S_N が、スペース区切りで与えられる。
  • 続く Q 行のうち j 行目(1 \leq j \leq Q)には、j 番目の問い合わせにおける区間の左端 L_j、右端 R_j、および検索するタイトル T_j が、スペース区切りで与えられる。

出力

Q 行にわたって出力せよ。j 行目(1 \leq j \leq Q)には、j 番目の問い合わせに対する答え、すなわち左から L_j 番目から R_j 番目までの本のうちタイトルが T_j と一致するものの冊数を出力せよ。


入力例 1

5 3
apple banana apple orange apple
1 5 apple
2 4 apple
1 3 grape

出力例 1

3
1
0

入力例 2

8 5
cat dog cat bird dog cat dog cat
1 8 cat
3 6 dog
1 4 bird
5 8 cat
2 2 dog

出力例 2

4
1
1
2
1

入力例 3

15 8
book pen book note pen book desk pen note book lamp pen book note desk
1 15 book
1 15 pen
3 10 book
5 12 pen
1 1 book
7 7 desk
10 15 note
1 15 chair

出力例 3

5
4
3
3
1
1
1
0

Score : 333 pts

Problem Statement

Takahashi works as a librarian and handles inquiries from patrons about the titles of books arranged on a bookshelf.

The bookshelf managed by Takahashi has N books lined up in a row, and the title of the i-th book from the left is the string S_i (1 \leq i \leq N). Note that there may be multiple books with the same title. One day, Aoki visits the library and makes Q inquiries about the books on the bookshelf.

In the j-th inquiry (1 \leq j \leq Q), Aoki specifies two integers L_j, R_j and one string T_j. Takahashi must answer how many books among those from the L_j-th to the R_j-th from the left (inclusive) have a title that exactly matches the string T_j.

Find the answers to all inquiries.

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq Q \leq 10^5
  • N, Q are integers
  • S_i is a string of lowercase English letters with length between 1 and 10 inclusive (1 \leq i \leq N)
  • 1 \leq L_j \leq R_j \leq N (1 \leq j \leq Q)
  • L_j, R_j are integers
  • T_j is a string of lowercase English letters with length between 1 and 10 inclusive (1 \leq j \leq Q)

Input

N Q
S_1 S_2 \ldots S_N
L_1 R_1 T_1
L_2 R_2 T_2
\vdots
L_Q R_Q T_Q
  • The first line contains the number of books N and the number of inquiries Q, separated by a space.
  • The second line contains the titles S_1, S_2, \ldots, S_N of the N books on the bookshelf, separated by spaces.
  • In the following Q lines, the j-th line (1 \leq j \leq Q) contains the left endpoint L_j, right endpoint R_j of the range, and the title to search for T_j in the j-th inquiry, separated by spaces.

Output

Output Q lines. On the j-th line (1 \leq j \leq Q), output the answer to the j-th inquiry, that is, the number of books from the L_j-th to the R_j-th from the left whose title matches T_j.


Sample Input 1

5 3
apple banana apple orange apple
1 5 apple
2 4 apple
1 3 grape

Sample Output 1

3
1
0

Sample Input 2

8 5
cat dog cat bird dog cat dog cat
1 8 cat
3 6 dog
1 4 bird
5 8 cat
2 2 dog

Sample Output 2

4
1
1
2
1

Sample Input 3

15 8
book pen book note pen book desk pen note book lamp pen book note desk
1 15 book
1 15 pen
3 10 book
5 12 pen
1 1 book
7 7 desk
10 15 note
1 15 chair

Sample Output 3

5
4
3
3
1
1
1
0
C - Toppling Blocks

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 366 点

問題文

高橋君は N 個の積み木を左から右へ一列に並べています。左から i 番目の積み木の高さは H_i です。

高橋君は、この積み木の列に対して以下の手順で整理を行います。整理は複数のラウンドからなります。

1ラウンドの手順:

  1. 現在の列に含まれる積み木の個数を M とし、列の積み木を左から順に位置 1, 2, \ldots, M と番号付けする。
  2. カーソル k を M に設定する。
  3. k \geq 2 である間、以下を繰り返す:
  • 位置 k の積み木の高さが位置 k-1 の積み木の高さより大きい場合:
  • 位置 k-1 の積み木を列から取り除く。残った積み木を左から詰めて位置を振り直す。この結果、取り除かれた位置より右にあった積み木の位置番号はそれぞれ 1 減る。特に、直前まで位置 k にあった積み木は位置 k-1 に移動する。
  • k を 1 減らす。これにより、次のステップでカーソルが指す位置には、今回取り除きを引き起こした積み木が置かれている。したがって、同じ積み木がさらにその左隣と比較される。
  • そうでない場合(位置 k の積み木の高さが位置 k-1 の積み木の高さ以下の場合):
  • 何もせず、k を 1 減らす。
  1. k < 2 となったらそのラウンドは終了する。

ラウンドの繰り返しと整理の終了:

  • そのラウンドで 1個以上 の積み木が取り除かれた場合、新たなラウンドを開始する(手順1に戻る)。
  • そのラウンドで 1個も 積み木が取り除かれなかった場合、整理は終了する。

なお、現在の列の積み木が 1 個以下の場合は、手順2で k \leq 1 となり手順3の条件 k \geq 2 を満たさないため、そのラウンドでは取り除きは発生せず、整理は終了します。

整理が終了した時点で、列に残っている積み木の個数を求めてください。

制約

  • 1 \leq N \leq 10^6
  • 1 \leq H_i \leq 10^9 (1 \leq i \leq N)
  • 入力はすべて整数である

入力

N
H_1 H_2 \ldots H_N
  • 1 行目には、積み木の個数を表す整数 N が与えられる。
  • 2 行目には、左から i 番目の積み木の高さを表す整数 H_i が、スペース区切りで N 個与えられる。

出力

整理が終了した時点で列に残っている積み木の個数を 1 行で出力せよ。


入力例 1

5
3 1 4 2 5

出力例 1

1

入力例 2

6
9 7 7 5 3 1

出力例 2

6

入力例 3

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

出力例 3

2

入力例 4

30
15 3 20 18 7 25 10 10 30 5 12 28 1 35 22 22 40 6 9 33 14 50 2 45 45 11 60 8 55 13

出力例 4

3

入力例 5

1
1000000000

出力例 5

1

Score : 366 pts

Problem Statement

Takahashi has N blocks arranged in a row from left to right. The height of the i-th block from the left is H_i.

Takahashi performs a reorganization on this row of blocks using the following procedure. The reorganization consists of multiple rounds.

Procedure of one round:

  1. Let M be the number of blocks currently in the row, and number the blocks in the row from left to right as positions 1, 2, \ldots, M.
  2. Set a cursor k to M.
  3. While k \geq 2, repeat the following:
  • If the height of the block at position k is strictly greater than the height of the block at position k-1:
  • Remove the block at position k-1 from the row. Shift the remaining blocks to the left and re-index them. As a result, the position index of each block to the right of the removed block decreases by 1. In particular, the block that was at position k just before this step moves to position k-1.
  • Decrease k by 1. As a result, in the next step, the cursor will point to the block that caused the removal in this step. Thus, the same block will be compared with its new left neighbor.
  • Otherwise (if the height of the block at position k is less than or equal to the height of the block at position k-1):
  • Do nothing, and decrease k by 1.
  1. When k < 2, the round ends.

Repetition of rounds and termination of reorganization:

  • If one or more blocks were removed during the round, start a new round (return to Step 1).
  • If no blocks were removed during the round, the reorganization ends.

Note that if there is 1 or fewer blocks in the current row, we will have k \leq 1 in Step 2, which does not satisfy the condition k \geq 2 in Step 3. Thus, no blocks will be removed in that round, and the reorganization will end.

Find the number of blocks remaining in the row when the reorganization ends.

Constraints

  • 1 \leq N \leq 10^6
  • 1 \leq H_i \leq 10^9 (1 \leq i \leq N)
  • All input values are integers.

Input

N
H_1 H_2 \ldots H_N
  • The first line contains an integer N, representing the number of blocks.
  • The second line contains N space-separated integers, where the i-th integer represents the height of the i-th block from the left, H_i.

Output

Print the number of blocks remaining in the row when the reorganization ends in a single line.


Sample Input 1

5
3 1 4 2 5

Sample Output 1

1

Sample Input 2

6
9 7 7 5 3 1

Sample Output 2

6

Sample Input 3

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

Sample Output 3

2

Sample Input 4

30
15 3 20 18 7 25 10 10 30 5 12 28 1 35 22 22 40 6 9 33 14 50 2 45 45 11 60 8 55 13

Sample Output 4

3

Sample Input 5

1
1000000000

Sample Output 5

1
D - Factory Production Volume

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400 点

問題文

高橋君は N 個の工場を管理しています。各工場 i(1 \leq i \leq N)には生産効率を表す正整数 P_i が設定されています。

N 個の工場全体の総合生産量は、すべての工場の生産効率の積 P_1 \times P_2 \times \cdots \times P_N を素数 M で割った余りで評価されます。

高橋君は Q 個のシミュレーションを行いたいと考えています。j 番目(1 \leq j \leq Q)のシミュレーションでは、工場 X_j の生産効率のみを非負整数 V_j に変更し、他の工場の生産効率は元のままとしたときの総合生産量を求めます。すなわち、

P_1 \times \cdots \times P_{X_j - 1} \times V_j \times P_{X_j + 1} \times \cdots \times P_N

を M で割った余りを求めてください。

各シミュレーションは独立です。あるシミュレーションでの変更は他のシミュレーションには影響せず、毎回元の生産効率 P_1, P_2, \ldots, P_N から出発します。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 2 \leq M \leq 10^9
  • M は素数
  • 1 \leq P_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq X_j \leq N (1 \leq j \leq Q)
  • 0 \leq V_j \leq 10^9 (1 \leq j \leq Q)
  • 入力はすべて整数

入力

N Q M
P_1 P_2 \ldots P_N
X_1 V_1
X_2 V_2
\vdots
X_Q V_Q
  • 1 行目には、工場の個数 N、シミュレーションの回数 Q、素数 M がスペース区切りで与えられる。
  • 2 行目には、各工場の初期生産効率 P_1, P_2, \ldots, P_N がスペース区切りで与えられる。
  • 続く Q 行のうち j 行目(1 \leq j \leq Q)には、変更対象の工場の番号 X_j と変更後の生産効率 V_j がスペース区切りで与えられる。

出力

Q 行出力せよ。j 行目(1 \leq j \leq Q)には、j 番目のシミュレーションに対する答え、すなわち工場 X_j の生産効率を V_j に置き換えたときの全工場の生産効率の積を M で割った余りを出力せよ。


入力例 1

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

出力例 1

4
0
1
3

入力例 2

4 5 5
1 5 2 3
2 4
1 7
3 10
4 0
2 5

出力例 2

4
0
0
0
0

入力例 3

10 10 17
3 8 17 5 21 1 34 9 2 11
3 4
7 6
1 0
5 100
10 17
2 19
8 8
4 12
6 13
9 51

出力例 3

0
0
0
0
0
0
0
0
0
0

入力例 4

30 25 998244353
123456789 987654321 111111111 222222222 333333333 444444444 555555555 666666666 777777777 888888888 999999999 998244353 1 2 3 4 5 6 7 8 9 10 314159265 271828182 161803398 42 1000000000 999999937 12345 54321
12 7
1 1000000000
30 0
27 998244353
13 123456789
20 876543210
5 5
18 18
22 22
24 314159265
2 1
11 999999999
14 0
25 271828182
28 999999937
3 333333333
7 777777777
16 4
26 42
29 12345
10 888888888
4 222222222
19 7
21 9
6 444444444

出力例 4

643945267
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0

入力例 5

1 1 2
1000000000
1 0

出力例 5

0

Score : 400 pts

Problem Statement

Takahashi manages N factories. Each factory i (1 \leq i \leq N) has a positive integer P_i representing its production efficiency.

The total production of all N factories is evaluated as the product of the production efficiencies of all factories P_1 \times P_2 \times \cdots \times P_N taken modulo a prime number M.

Takahashi wants to perform Q simulations. In the j-th simulation (1 \leq j \leq Q), only the production efficiency of factory X_j is changed to a non-negative integer V_j, while the production efficiencies of all other factories remain unchanged. That is, find

P_1 \times \cdots \times P_{X_j - 1} \times V_j \times P_{X_j + 1} \times \cdots \times P_N

modulo M.

Each simulation is independent. Changes made in one simulation do not affect other simulations; each simulation starts from the original production efficiencies P_1, P_2, \ldots, P_N.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 2 \leq M \leq 10^9
  • M is a prime number
  • 1 \leq P_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq X_j \leq N (1 \leq j \leq Q)
  • 0 \leq V_j \leq 10^9 (1 \leq j \leq Q)
  • All inputs are integers

Input

N Q M
P_1 P_2 \ldots P_N
X_1 V_1
X_2 V_2
\vdots
X_Q V_Q
  • The first line contains the number of factories N, the number of simulations Q, and the prime number M, separated by spaces.
  • The second line contains the initial production efficiencies P_1, P_2, \ldots, P_N of each factory, separated by spaces.
  • The j-th line (1 \leq j \leq Q) of the following Q lines contains the index of the factory to be changed X_j and the new production efficiency V_j, separated by spaces.

Output

Output Q lines. The j-th line (1 \leq j \leq Q) should contain the answer to the j-th simulation, that is, the remainder when the product of all factories' production efficiencies, with factory X_j's efficiency replaced by V_j, is divided by M.


Sample Input 1

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

Sample Output 1

4
0
1
3

Sample Input 2

4 5 5
1 5 2 3
2 4
1 7
3 10
4 0
2 5

Sample Output 2

4
0
0
0
0

Sample Input 3

10 10 17
3 8 17 5 21 1 34 9 2 11
3 4
7 6
1 0
5 100
10 17
2 19
8 8
4 12
6 13
9 51

Sample Output 3

0
0
0
0
0
0
0
0
0
0

Sample Input 4

30 25 998244353
123456789 987654321 111111111 222222222 333333333 444444444 555555555 666666666 777777777 888888888 999999999 998244353 1 2 3 4 5 6 7 8 9 10 314159265 271828182 161803398 42 1000000000 999999937 12345 54321
12 7
1 1000000000
30 0
27 998244353
13 123456789
20 876543210
5 5
18 18
22 22
24 314159265
2 1
11 999999999
14 0
25 271828182
28 999999937
3 333333333
7 777777777
16 4
26 42
29 12345
10 888888888
4 222222222
19 7
21 9
6 444444444

Sample Output 4

643945267
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0

Sample Input 5

1 1 2
1000000000
1 0

Sample Output 5

0
E - Fluctuation Value of Digits

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 433 点

問題文

高橋君は、1 から N までの番号が書かれた N 枚のカードを持っています。この中から、揺れ値がちょうど K であるカードが何枚あるかを知りたいと考えています。

ここで、正の整数の「揺れ値」を次のように定義します。

正の整数を、通常の十進法(先頭に余分な 0 を付けない表記)で表したときの桁数を L とし、最上位桁から順に各桁の数字を d_1, d_2, \ldots, d_L(d_1 \geq 1)とします。このとき、揺れ値は

\sum_{i=1}^{L-1} |d_i - d_{i+1}|

で定義されます。1 桁の数(L = 1)の場合、この和の項は存在しないため揺れ値は 0 です。

例えば、3517 は 4 桁で d_1 = 3, d_2 = 5, d_3 = 1, d_4 = 7 なので、揺れ値は |3-5| + |5-1| + |1-7| = 2 + 4 + 6 = 12 です。また、7 は 1 桁なので揺れ値は 0 です。

1 以上 N 以下の整数のうち、揺れ値がちょうど K であるものの個数を求めてください。

制約

  • 1 \leq N \leq 10^{18}
  • 0 \leq K \leq 153
  • N および K は整数である

入力

N K

カードの枚数を表す整数 N と、目標とする揺れ値を表す整数 K が、スペース区切りで 1 行で与えられる。

出力

1 以上 N 以下の整数のうち、揺れ値がちょうど K であるものの個数を 1 行で出力せよ。


入力例 1

20 1

出力例 1

2

入力例 2

100 0

出力例 2

18

入力例 3

1234567 12

出力例 3

73745

入力例 4

987654321012345678 80

出力例 4

3107339199338170

入力例 5

1 0

出力例 5

1

Score : 433 pts

Problem Statement

Takahashi has N cards numbered from 1 to N. He wants to know how many of these cards have a fluctuation value of exactly K.

Here, the "fluctuation value" of a positive integer is defined as follows:

Let L be the number of digits when a positive integer is represented in standard decimal notation (without leading zeros), and let the digits from the most significant to the least significant be d_1, d_2, \ldots, d_L (d_1 \geq 1). The fluctuation value is defined as:

\sum_{i=1}^{L-1} |d_i - d_{i+1}|

For a single-digit number (L = 1), there are no terms in this sum, so the fluctuation value is 0.

For example, 3517 has 4 digits with d_1 = 3, d_2 = 5, d_3 = 1, d_4 = 7, so its fluctuation value is |3-5| + |5-1| + |1-7| = 2 + 4 + 6 = 12. Also, 7 is a single-digit number, so its fluctuation value is 0.

Find the number of integers between 1 and N, inclusive, whose fluctuation value is exactly K.

Constraints

  • 1 \leq N \leq 10^{18}
  • 0 \leq K \leq 153
  • N and K are integers.

Input

N K

The integer N representing the number of cards and the integer K representing the target fluctuation value are given in a single line, separated by a space.

Output

Print the number of integers between 1 and N, inclusive, whose fluctuation value is exactly K in a single line.


Sample Input 1

20 1

Sample Output 1

2

Sample Input 2

100 0

Sample Output 2

18

Sample Input 3

1234567 12

Sample Output 3

73745

Sample Input 4

987654321012345678 80

Sample Output 4

3107339199338170

Sample Input 5

1 0

Sample Output 5

1