Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
0 と 1 の 2 種類の文字からなる文字列 s が与えられます。
s に含まれる 0 を 1 に、1 を 0 に置き換えた文字列を出力してください。
制約
- s の長さは 1 以上 10 以下
- s は
0と1の 2 種類の文字からなる
入力
入力は以下の形式で標準入力から与えられる。
s
出力
答えを 1 行で出力せよ。
入力例 1
01
出力例 1
10
s の 1 文字目は 1 なので、1 文字目に出力すべき文字は 0 です。
s の 2 文字目は 0 なので、2 文字目に出力すべき文字は 1 です。
入力例 2
1011
出力例 2
0100
入力例 3
100100001
出力例 3
011011110
Score : 100 points
Problem Statement
You are given a string s consisting of two kinds of characters, 0 and 1.
Print the string obtained by replacing 0 with 1 and 1 with 0 in s.
Constraints
- The length of s is between 1 and 10, inclusive.
- s consists of two kinds of characters,
0and1.
Input
The input is given from Standard Input in the following format:
s
Output
Print the answer in a single line.
Sample Input 1
01
Sample Output 1
10
The 1-st character of s is 1, so the 1-st character to print is 0.
The 2-nd character of s is 0, so the 2-nd character to print is 1.
Sample Input 2
1011
Sample Output 2
0100
Sample Input 3
100100001
Sample Output 3
011011110
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
英小文字のみからなる長さ 3 の文字列 S が与えられます。
S の各文字を並び替えて得られる文字列は、何種類ありますか?
制約
- S は英小文字のみからなる長さ 3 の文字列
入力
入力は以下の形式で標準入力から与えられる。
S
出力
S の各文字を並び替えて得られる文字列の種類数を出力せよ。
入力例 1
aba
出力例 1
3
S= aba の各文字を並び替えて得られる文字列は、aab, aba, baa の 3 通りです。
入力例 2
ccc
出力例 2
1
S= ccc の各文字を並び替えて得られる文字列は、ccc の 1 通りのみです。
入力例 3
xyz
出力例 3
6
S= xyz の各文字を並び替えて得られる文字列は、xyz, xzy, yxz, yzx, zxy, zyx の 6 通りです。
Score : 100 points
Problem Statement
You are given a string S of length 3 consisting of lowercase English letters.
How many different strings can be obtained by permuting the characters in S?
Constraints
- S is a string S of length 3 consisting of lowercase English letters.
Input
Input is given from Standard Input in the following format:
S
Output
Print the number of different strings that can be obtained by permuting the characters in S.
Sample Input 1
aba
Sample Output 1
3
By permuting the characters in S= aba, three different strings can be obtained: aab, aba, baa.
Sample Input 2
ccc
Sample Output 2
1
By permuting the characters in S= ccc, just one string can be obtained: ccc.
Sample Input 3
xyz
Sample Output 3
6
By permuting the characters in S= xyz, six different strings can be obtained: xyz, xzy, yxz, yzx, zxy, zyx.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
1 から N の番号が付いた N 人の人がいます。
また、1 から M の番号が付いた M 種類の服があります。人 i は服 F_i を着ています。
次の 2 個の質問に Yes か No で答えてください。
- 質問 1: N 人全員が異なる種類の服を着ていますか?
- 質問 2: M 種類の服全てについて、その服を着ている人が少なくとも 1 人ずついますか?
制約
- 1 \leq N \leq 100
- 1 \leq M \leq 100
- 1 \leq F_i \leq M
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M F_1 F_2 \dots F_N
出力
2 行出力せよ。i 行目には質問 i の答えが Yes であれば Yes を、No であれば No を出力せよ。
入力例 1
3 4 1 2 4
出力例 1
Yes No
全員が異なる種類の服を着ているので、1 番目の質問の答えは Yes です。
また、服 3 を着ている人は存在しないので、2 番目の質問の答えは No です。
入力例 2
4 2 1 2 1 2
出力例 2
No Yes
入力例 3
4 4 1 3 2 1
出力例 3
No No
入力例 4
5 5 1 3 4 2 5
出力例 4
Yes Yes
Score : 200 points
Problem Statement
There are N people numbered 1 through N.
There are M types of clothes numbered 1 through M. Person i is wearing clothes F_i.
Answer the following two questions with Yes or No.
- Question 1: Are all N people wearing different types of clothes?
- Question 2: For every one of the M types of clothes, is there at least one person wearing that type?
Constraints
- 1 \leq N \leq 100
- 1 \leq M \leq 100
- 1 \leq F_i \leq M
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M F_1 F_2 \dots F_N
Output
Output two lines. The i-th line should contain Yes if the answer to question i is Yes, and No if it is No.
Sample Input 1
3 4 1 2 4
Sample Output 1
Yes No
Everyone is wearing a different type of clothes, so the answer to question 1 is Yes.
There is no person wearing clothes 3, so the answer to question 2 is No.
Sample Input 2
4 2 1 2 1 2
Sample Output 2
No Yes
Sample Input 3
4 4 1 3 2 1
Sample Output 3
No No
Sample Input 4
5 5 1 3 4 2 5
Sample Output 4
Yes Yes
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
AtCoder Land の入り口には 1 つのチケット売り場があり、来園客はこのチケット売り場の前に一列に並んで順にチケットを購入します。 チケットの購入手続きには一人当たり A 秒かかり、列の先頭の人がチケットを購入し終わると、(存在すれば)次の人がすぐさま購入手続きを開始します。
現在チケット売り場に並んでいる人はおらず、今から N 人の人が順にチケットを買いに来ます。 具体的には、i 番目の人は今から T_i 秒後にチケット売り場を訪れ、既に列が存在すればその最後尾に並び、存在しなければすぐさま購入手続きを開始します。 ここで、T_1< T_2< \dots < T_N です。
各 i\ (1\leq i\leq N) について、i 番目の人がチケットを購入し終わるのは今から何秒後か求めてください。
制約
- 1\leq N \leq 100
- 0\leq T_1< T_2< \dots < T_N\leq 10^6
- 1\leq A\leq 10^6
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N A T_1 T_2 \dots T_N
出力
N 行出力せよ。 i\ (1\leq i \leq N) 行目には、i 番目の人がチケットを購入し終わるのは今から何秒後かを整数として出力せよ。
入力例 1
3 4 0 2 10
出力例 1
4 8 14
時系列順に以下のように物事が進行します。
- 0 秒後:1 番目の人がチケット売り場を訪れ、チケットの購入手続きを開始する。
- 2 秒後:2 番目の人がチケット売り場を訪れ、1 番目の人の後ろに並ぶ。
- 4 秒後:1 番目の人がチケットを購入し終え、2 番目の人が購入手続きを開始する。
- 8 秒後:2 番目の人がチケットを購入し終える。
- 10 秒後:3 番目の人がチケット売り場を訪れ、チケットの購入手続きを開始する。
- 14 秒後:3 番目の人がチケットを購入し終える。
入力例 2
3 3 1 4 7
出力例 2
4 7 10
時系列順に以下のように物事が進行します。
- 1 秒後:1 番目の人がチケット売り場を訪れ、チケットの購入手続きを開始する。
- 4 秒後:1 番目の人がチケットを購入し終えると同時に、2 番目の人がチケット売り場を訪れ、チケットの購入手続きを開始する。
- 7 秒後:2 番目の人がチケットを購入し終えると同時に、3 番目の人がチケット売り場を訪れ、チケットの購入手続きを開始する。
- 10 秒後:3 番目の人がチケットを購入し終える。
入力例 3
10 50000 120190 165111 196897 456895 540000 552614 561627 743796 757613 991216
出力例 3
170190 220190 270190 506895 590000 640000 690000 793796 843796 1041216
Score : 200 points
Problem Statement
At the entrance of AtCoder Land, there is a single ticket booth where visitors line up to purchase tickets one by one. The purchasing process takes A seconds per person. Once the person at the front of the line finishes purchasing their ticket, the next person (if any) immediately starts their purchasing process.
Currently, there is no one in line at the ticket booth, and N people will come to buy tickets one after another. Specifically, the i-th person will arrive at the ticket booth T_i seconds from now. If there is already a line, they will join the end of it; if not, they will start the purchasing process immediately. Here, T_1 < T_2 < \dots < T_N.
For each i\ (1 \leq i \leq N), determine how many seconds from now the i-th person will finish purchasing their ticket.
Constraints
- 1 \leq N \leq 100
- 0 \leq T_1 < T_2 < \dots < T_N \leq 10^6
- 1 \leq A \leq 10^6
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A T_1 T_2 \dots T_N
Output
Print N lines. The i-th line should contain the number of seconds from now that the i-th person will finish purchasing their ticket.
Sample Input 1
3 4 0 2 10
Sample Output 1
4 8 14
The events proceed in the following order:
- At 0 seconds: The 1st person arrives at the ticket booth and starts the purchasing process.
- At 2 seconds: The 2nd person arrives at the ticket booth and joins the line behind the 1st person.
- At 4 seconds: The 1st person finishes purchasing their ticket, and the 2nd person starts the purchasing process.
- At 8 seconds: The 2nd person finishes purchasing their ticket.
- At 10 seconds: The 3rd person arrives at the ticket booth and starts the purchasing process.
- At 14 seconds: The 3rd person finishes purchasing their ticket.
Sample Input 2
3 3 1 4 7
Sample Output 2
4 7 10
The events proceed in the following order:
- At 1 second: The 1st person arrives at the ticket booth and starts the purchasing process.
- At 4 seconds: The 1st person finishes purchasing their ticket, and the 2nd person arrives at the ticket booth and starts the purchasing process.
- At 7 seconds: The 2nd person finishes purchasing their ticket, and the 3rd person arrives at the ticket booth and starts the purchasing process.
- At 10 seconds: The 3rd person finishes purchasing their ticket.
Sample Input 3
10 50000 120190 165111 196897 456895 540000 552614 561627 743796 757613 991216
Sample Output 3
170190 220190 270190 506895 590000 640000 690000 793796 843796 1041216
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
あるイベントには N 人が参加し、i 番目の人の交通費は A_i 円でした。
イベントの主催者である高橋くんは、交通費補助額の上限額 x を設定して、人 i には交通費補助額として \min(x,A_i) 円を支給することとしました。ここで x は非負整数である必要があります。
高橋くんの予算が M 円であり、N 人に渡す交通費補助額の総和を M 円以下にしたいとき、交通費補助額の上限額 x は最大でいくらにできますか?
ただし、交通費補助額の上限額を無限に大きくできる場合は代わりにそのことを報告してください。
制約
- 1\leq N\leq 2\times 10^5
- 1\leq M \leq 2\times 10^{14}
- 1\leq A_i \leq 10^9
- 入力される数値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M
A_1 A_2 \ldots A_{N}
出力
予算の条件を満たすときの交通費補助額の上限額 x の最大値を整数として出力せよ。
ただし、交通費補助額の上限額を無限に大きくできる場合は代わりに infinite と出力せよ。
入力例 1
4 8 1 3 2 4
出力例 1
2
交通費補助額の上限額を 2 円にすると、N 人に渡す交通費補助額の総和は \min(2,1) + \min(2,3) + \min(2,2) + \min(2,4) = 7 円となり、予算の 8 円以下となります。
交通費補助額の上限額を 3 円にすると、N 人に渡す交通費補助額の総和は \min(3,1) + \min(3,3) + \min(3,2) + \min(3,4) = 9 円となり、予算の 8 円を超えてしまいます。
よって、交通費補助額の上限額の最大値は 2 円となります。
入力例 2
3 20 5 3 2
出力例 2
infinite
交通費補助額の上限額を無限に大きくできます。
入力例 3
10 23 2 5 6 5 2 1 7 9 7 2
出力例 3
2
Score : 300 points
Problem Statement
There are N people participating in an event, and the transportation cost for the i-th person is A_i yen.
Takahashi, the organizer of the event, decided to set a maximum limit x for the transportation subsidy. The subsidy for person i will be \min(x, A_i) yen. Here, x must be a non-negative integer.
Given that Takahashi's budget is M yen, and he wants the total transportation subsidy for all N people to be at most M yen, what is the maximum possible value of the subsidy limit x?
If the subsidy limit can be made infinitely large, report that instead.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^{14}
- 1 \leq A_i \leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M
A_1 A_2 \ldots A_{N}
Output
Print the maximum value of the subsidy limit x that satisfies the budget condition, as an integer.
If the subsidy limit can be made infinitely large, print infinite instead.
Sample Input 1
4 8 1 3 2 4
Sample Output 1
2
If the subsidy limit is set to 2 yen, the total transportation subsidy for all N people is \min(2,1) + \min(2,3) + \min(2,2) + \min(2,4) = 7 yen, which is within the budget of 8 yen.
If the subsidy limit is set to 3 yen, the total transportation subsidy for all N people is \min(3,1) + \min(3,3) + \min(3,2) + \min(3,4) = 9 yen, which exceeds the budget of 8 yen.
Therefore, the maximum possible value of the subsidy limit is 2 yen.
Sample Input 2
3 20 5 3 2
Sample Output 2
infinite
The subsidy limit can be made infinitely large.
Sample Input 3
10 23 2 5 6 5 2 1 7 9 7 2
Sample Output 3
2
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 250 点
問題文
正整数 N が与えられます。
N 以下の正整数であって回文立方数であるものの最大値を求めてください。
ただし、正整数 K は以下の 2 つの条件を満たすとき、またそのときに限り回文立方数であると定義します。
- ある正整数 x が存在し、x^3 = K を満たす。
- K を先頭に 0 をつけずに 10 進表記した文字列が回文となる。より厳密には、0 以上 9 以下の整数 A_0, A_1, \ldots, A_{L-2} および 1 以上 9 以下の整数 A_{L-1} を用いて K = \sum_{i = 0}^{L-1} A_i10^i と表記したときに i = 0, 1, \ldots, L-1 に対して A_i = A_{L-1-i} を満たす。
制約
- N は 10^{18} 以下の正整数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
答えを出力せよ。
入力例 1
345
出力例 1
343
343 は回文立方数であり、344, 345 は回文立方数ではありません。したがって、343 が答えとなります。
入力例 2
6
出力例 2
1
入力例 3
123456789012345
出力例 3
1334996994331
Score: 250 points
Problem Statement
You are given a positive integer N.
Find the maximum value of a palindromic cube number not greater than N.
Here, a positive integer K is defined to be a palindromic cube number if and only if it satisfies the following two conditions:
- There is a positive integer x such that x^3 = K.
- The decimal representation of K without leading zeros is a palindrome. More precisely, if K is represented as K = \sum_{i = 0}^{L-1} A_i10^i using integers A_0, A_1, \ldots, A_{L-2} between 0 and 9, inclusive, and an integer A_{L-1} between 1 and 9, inclusive, then A_i = A_{L-1-i} for all i = 0, 1, \ldots, L-1.
Constraints
- N is a positive integer not greater than 10^{18}.
Input
The input is given from Standard Input in the following format:
N
Output
Print the answer.
Sample Input 1
345
Sample Output 1
343
343 is a palindromic cube number, while 344 and 345 are not. Thus, the answer is 343.
Sample Input 2
6
Sample Output 2
1
Sample Input 3
123456789012345
Sample Output 3
1334996994331
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
長さ N の数列 A=(A_1,A_2,\dots,A_N) が与えられます。この A に以下を施すことを「操作」と呼びます。
- まず、 1 \le i \le N を満たす整数 i を選択する。
- 次に、以下の 2 つのうちどちらかを選択し、実行する。
- A_i に 1 を加算する。
- A_i から 1 を減算する。
Q 個の質問に答えてください。
i 個目の質問は以下です。
- 「操作」を 0 回以上何度でも使って A の要素を全て X_i にする時、必要な「操作」の最小回数を求めてください。
制約
- 入力は全て整数
- 1 \le N,Q \le 2 \times 10^5
- 0 \le A_i \le 10^9
- 0 \le X_i \le 10^9
入力
入力は以下の形式で標準入力から与えられる。
N Q A_1 A_2 \dots A_N X_1 X_2 \vdots X_Q
出力
Q 行にわたって出力せよ。
出力のうち i 行目には、 i 個目の質問に対する答えを整数として出力せよ。
入力例 1
5 3 6 11 2 5 5 5 20 0
出力例 1
10 71 29
A=(6,11,2,5,5) であり、この入力には 3 つの質問が含まれます。
1 つ目の質問について、 A に以下のように 10 回の「操作」を施すことで、 A の要素を全て 5 にすることができます。
- A_1 から 1 減算する。
- A_2 から 1 減算することを 6 度繰り返す。
- A_3 に 1 加算することを 3 度繰り返す。
9 回以下の「操作」で A の要素を全て 5 にすることはできません。
2 つ目の質問について、 A に 71 回の「操作」を施すことで、 A の要素を全て 20 にすることができます。
3 つ目の質問について、 A に 29 回の「操作」を施すことで、 A の要素を全て 0 にすることができます。
入力例 2
10 5 1000000000 314159265 271828182 141421356 161803398 0 777777777 255255255 536870912 998244353 555555555 321654987 1000000000 789456123 0
出力例 2
3316905982 2811735560 5542639502 4275864946 4457360498
出力が 32bit 整数に収まらない場合もあります。
Score : 400 points
Problem Statement
You are given a sequence of length N: A=(A_1,A_2,\dots,A_N). The following action on this sequence is called an operation.
- First, choose an integer i such that 1 \le i \le N.
- Next, choose and do one of the following.
- Add 1 to A_i.
- Subtract 1 from A_i.
Answer Q questions.
The i-th question is the following.
- Consider performing zero or more operations to change every element of A to X_i. Find the minimum number of operations required to do so.
Constraints
- All values in input are integers.
- 1 \le N,Q \le 2 \times 10^5
- 0 \le A_i \le 10^9
- 0 \le X_i \le 10^9
Input
Input is given from Standard Input in the following format:
N Q A_1 A_2 \dots A_N X_1 X_2 \vdots X_Q
Output
Print Q lines.
The i-th line should contain the answer to the i-th question as an integer.
Sample Input 1
5 3 6 11 2 5 5 5 20 0
Sample Output 1
10 71 29
We have A=(6,11,2,5,5) and three questions in this input.
For the 1-st question, you can change every element of A to 5 in 10 operations as follows.
- Subtract 1 from A_1.
- Subtract 1 from A_2 six times.
- Add 1 to A_3 three times.
It is impossible to change every element of A to 5 in 9 or fewer operations.
For the 2-nd question, you can change every element of A to 20 in 71 operations.
For the 3-rd question, you can change every element of A to 0 in 29 operations.
Sample Input 2
10 5 1000000000 314159265 271828182 141421356 161803398 0 777777777 255255255 536870912 998244353 555555555 321654987 1000000000 789456123 0
Sample Output 2
3316905982 2811735560 5542639502 4275864946 4457360498
The output may not fit into 32-bit integers.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 450 点
問題文
文字列 X,Y が与えられます。文字列の列 S_1,S_2,\dots を以下で定義します。
- S_1=X
- S_2=Y
- i\geq 3 のとき、S_i は S_{i-1} と S_{i-2} をこの順に連結したもの
各 i=1,2,\ldots,Q について以下の問題に答えてください。
問題:整数 L_i,R_i と文字 C_i が与えられる。 S_{10^{18}} の L_i 文字目から R_i 文字目までに文字 C_i が何個含まれるか求めよ。
制約
- X,Y は英小文字からなる長さ 1 以上 10^4 以下の文字列
- 1 \leq Q \leq 10^5
- 1 \leq L_i \leq R_i \leq 10^{18}
- C_i は英小文字
- 与えられる数値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
X Y Q L_1 R_1 C_1 L_2 R_2 C_2 \vdots L_Q R_Q C_Q
出力
Q 行出力せよ。
i 行目には S_{10^{18}} の L_i 文字目から R_i 文字目までに文字 C_i が何個含まれるかを出力せよ。
入力例 1
a b 6 2 7 a 1 3 b 3 7 b 1 9 c 1 1000000000000000000 b 1000000000000000000 1000000000000000000 a
出力例 1
3 2 3 0 618033988749894848 1
S_3,S_4,S_5 はそれぞれba, bab, babba となります。
S_{10^{18}} は babbababbabba... であり、その 2 文字目から 7 文字目に a は 3 個含まれます。
Score : 450 points
Problem Statement
You are given strings X and Y. Define a sequence of strings S_1, S_2, \dots as follows.
- S_1 = X
- S_2 = Y
- For i \geq 3, S_i is the concatenation of S_{i-1} and S_{i-2} in this order.
For each i = 1, 2, \ldots, Q, answer the following problem.
Problem: You are given integers L_i, R_i and a character C_i. Find how many times character C_i appears in the L_i-th through R_i-th characters of S_{10^{18}}.
Constraints
- X and Y are strings of lowercase English letters of length between 1 and 10^4, inclusive.
- 1 \leq Q \leq 10^5
- 1 \leq L_i \leq R_i \leq 10^{18}
- C_i is a lowercase English letter.
- All given numerical values are integers.
Input
The input is given from Standard Input in the following format:
X Y Q L_1 R_1 C_1 L_2 R_2 C_2 \vdots L_Q R_Q C_Q
Output
Output Q lines.
The i-th line should contain how many times character C_i appears in the L_i-th through R_i-th characters of S_{10^{18}}.
Sample Input 1
a b 6 2 7 a 1 3 b 3 7 b 1 9 c 1 1000000000000000000 b 1000000000000000000 1000000000000000000 a
Sample Output 1
3 2 3 0 618033988749894848 1
S_3, S_4, S_5 are ba, bab, babba, respectively.
S_{10^{18}} is babbababbabba..., and the second through seventh characters contain three occurrences of a.
Time Limit: 3 sec / Memory Limit: 1024 MiB
配点 : 550 点
問題文
N 個のティーポットが横一列に並んでおり、左から順に 1 から N までの番号が付けられています。
整数列 (a_1, \dots, a_N) があり、はじめその値は a_1 = \dots = a_N = -1 です。
あなたは以下の条件をすべて満たすように、それぞれのティーポットに紅茶かコーヒーのいずれか 1 つを入れます。
- どの隣り合う 2 つのティーポットについても、少なくとも片方には紅茶を入れる。
- 1 \leq i \leq N を満たす整数 i について、a_i \neq -1 である場合、ティーポット 1, \dots, i のうちちょうど a_i 個にコーヒーを入れる。
クエリが Q 個与えられるので、与えられた順に処理してください。
j 番目のクエリ (1 \leq j \leq Q) は以下の通りです。
- a_{X_j} の値を Y_j に変更する。その後、条件を満たすようなティーポットの満たし方の個数を 998244353 で割ったあまりを出力する。
制約
- 2 \leq N \leq 2 \times 10^5
- 1 \leq Q \leq 2 \times 10^5
- 1 \leq X_j \leq N (1 \leq j \leq Q)
- -1 \leq Y_j \leq X_j (1 \leq j \leq Q)
- 入力される値は全て整数である。
入力
入力は以下の形式で標準入力から与えられる。
N Q X_1 Y_1 \vdots X_Q Y_Q
出力
Q 行出力せよ。
j 行目 (1 \leq j \leq Q) には、j 番目のクエリで出力するべき値を出力せよ。
入力例 1
5 6 1 1 4 2 1 0 4 -1 5 1 5 5
出力例 1
5 3 1 8 4 0
- 1 番目のクエリでの操作後、a = (1,\ -1,\ -1,\ -1,\ -1) となります。条件を満たす入れ方は以下の 5 通りです。
- ティーポット 1 にコーヒーを入れ、ほかのティーポットに紅茶を入れる。
- ティーポット 1, 3 にコーヒーを入れ、ほかのティーポットに紅茶を入れる。
- ティーポット 1, 3, 5 にコーヒーを入れ、ほかのティーポットに紅茶を入れる。
- ティーポット 1, 4 にコーヒーを入れ、ほかのティーポットに紅茶を入れる。
- ティーポット 1, 5 にコーヒーを入れ、ほかのティーポットに紅茶を入れる。
- 2 番目のクエリでの操作後、a = (1,\ -1,\ -1,\ 2,\ -1) となります。条件を満たす入れ方は以下の 3 通りです。
- ティーポット 1, 3 にコーヒーを入れ、ほかのティーポットに紅茶を入れる。
- ティーポット 1, 3, 5 にコーヒーを入れ、ほかのティーポットに紅茶を入れる。
- ティーポット 1, 4 にコーヒーを入れ、ほかのティーポットに紅茶を入れる。
- 3 番目のクエリでの操作後、a = (0,\ -1,\ -1,\ 2,\ -1) となります。条件を満たす入れ方は以下の 1 通りです。
- ティーポット 2, 4 にコーヒーを入れ、ほかのティーポットに紅茶を入れる。
- 4 番目のクエリでの操作後、a = (0,\ -1,\ -1,\ -1,\ -1) となります。条件を満たす入れ方は以下の 8 通りです。
- どのティーポットにもコーヒーを入れず、すべてのティーポットに紅茶を入れる。
- ティーポット 2 にコーヒーを入れ、ほかのティーポットに紅茶を入れる。
- ティーポット 2, 4 にコーヒーを入れ、ほかのティーポットに紅茶を入れる。
- ティーポット 2, 5 にコーヒーを入れ、ほかのティーポットに紅茶を入れる。
- ティーポット 3 にコーヒーを入れ、ほかのティーポットに紅茶を入れる。
- ティーポット 3, 5 にコーヒーを入れ、ほかのティーポットに紅茶を入れる。
- ティーポット 4 にコーヒーを入れ、ほかのティーポットに紅茶を入れる。
- ティーポット 5 にコーヒーを入れ、ほかのティーポットに紅茶を入れる。
- 5 番目のクエリでの操作後、a = (0,\ -1,\ -1,\ -1,\ 1) となります。条件を満たす入れ方は以下の 4 通りです。
- ティーポット 2 にコーヒーを入れ、ほかのティーポットに紅茶を入れる。
- ティーポット 3 にコーヒーを入れ、ほかのティーポットに紅茶を入れる。
- ティーポット 4 にコーヒーを入れ、ほかのティーポットに紅茶を入れる。
- ティーポット 5 にコーヒーを入れ、ほかのティーポットに紅茶を入れる。
- 6 番目のクエリでの操作後、a = (0,\ -1,\ -1,\ -1,\ 5) となります。条件を満たす入れ方は 0 通りです。
Score : 550 points
Problem Statement
There are N teapots arranged in a row, numbered from 1 to N from left to right.
There is a sequence of integers (a_1, \dots, a_N), initially with values a_1 = \dots = a_N = -1.
You will fill each teapot with either tea or coffee so that the following conditions are all satisfied:
- For any two adjacent teapots, at least one of them contains tea.
- For any integer i satisfying 1 \leq i \leq N, if a_i \neq -1, then exactly a_i of teapots 1, \dots, i contain coffee.
You are given Q queries, which you should process in the given order.
The j-th query (1 \leq j \leq Q) is as follows:
- Change the value of a_{X_j} to Y_j. Then, print the number, modulo 998244353, of ways to fill the teapots satisfying the conditions.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq Q \leq 2 \times 10^5
- 1 \leq X_j \leq N (1 \leq j \leq Q)
- -1 \leq Y_j \leq X_j (1 \leq j \leq Q)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N Q X_1 Y_1 \vdots X_Q Y_Q
Output
Print Q lines.
The j-th line (1 \leq j \leq Q) should contain the value to be printed for the j-th query.
Sample Input 1
5 6 1 1 4 2 1 0 4 -1 5 1 5 5
Sample Output 1
5 3 1 8 4 0
- After the operation in the first query, a = (1,\ -1,\ -1,\ -1,\ -1). The ways to fill the teapots satisfying the conditions are the following five ways:
- Put coffee in teapot 1, and tea in the others.
- Put coffee in teapots 1, 3, and tea in the others.
- Put coffee in teapots 1, 3, 5, and tea in the others.
- Put coffee in teapots 1, 4, and tea in the others.
- Put coffee in teapots 1, 5, and tea in the others.
- After the operation in the second query, a = (1,\ -1,\ -1,\ 2,\ -1). The ways to fill the teapots satisfying the conditions are the following three ways:
- Put coffee in teapots 1, 3, and tea in the others.
- Put coffee in teapots 1, 3, 5, and tea in the others.
- Put coffee in teapots 1, 4, and tea in the others.
- After the operation in the third query, a = (0,\ -1,\ -1,\ 2,\ -1). The ways to fill the teapots satisfying the conditions are the following one way:
- Put coffee in teapots 2, 4, and tea in the others.
- After the operation in the fourth query, a = (0,\ -1,\ -1,\ -1,\ -1). The ways to fill the teapots satisfying the conditions are the following eight ways:
- Put coffee in none of the teapots and tea in all of them.
- Put coffee in teapot 2, and tea in the others.
- Put coffee in teapots 2, 4, and tea in the others.
- Put coffee in teapots 2, 5, and tea in the others.
- Put coffee in teapot 3, and tea in the others.
- Put coffee in teapots 3, 5, and tea in the others.
- Put coffee in teapot 4, and tea in the others.
- Put coffee in teapot 5, and tea in the others.
- After the operation in the fifth query, a = (0,\ -1,\ -1,\ -1,\ 1). The ways to fill the teapots satisfying the conditions are the following four ways:
- Put coffee in teapot 2, and tea in the others.
- Put coffee in teapot 3, and tea in the others.
- Put coffee in teapot 4, and tea in the others.
- Put coffee in teapot 5, and tea in the others.
- After the operation in the sixth query, a = (0,\ -1,\ -1,\ -1,\ 5). The number of ways to fill the teapots satisfying the conditions is zero.