A - You should output ARC, though this is ABC.

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

整数 R,C22 列からなる行列 A が与えられるので、 A_{R,C} を出力してください。

制約

  • 入力は全て整数
  • 1 \le R,C \le 2
  • 0 \le A_{i,j} \le 100

入力

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

R C
A_{1,1} A_{1,2}
A_{2,1} A_{2,2}

出力

答えを整数として出力せよ。


入力例 1

1 2
1 0
0 1

出力例 1

0

A_{1,2}=0 です。


入力例 2

2 2
1 2
3 4

出力例 2

4

A_{2,2}=4 です。


入力例 3

2 1
90 80
70 60

出力例 3

70

A_{2,1}=70 です。

Score : 100 points

Problem Statement

Given integers R, C, and a 2 \times 2 matrix A, print A_{R,C}.

Constraints

  • All values in input are integers.
  • 1 \le R,C \le 2
  • 0 \le A_{i,j} \le 100

Input

Input is given from Standard Input in the following format:

R C
A_{1,1} A_{1,2}
A_{2,1} A_{2,2}

Output

Print the answer as an integer.


Sample Input 1

1 2
1 0
0 1

Sample Output 1

0

We have A_{1,2}=0.


Sample Input 2

2 2
1 2
3 4

Sample Output 2

4

We have A_{2,2}=4.


Sample Input 3

2 1
90 80
70 60

Sample Output 3

70

We have A_{2,1}=70.

B - Light It Up

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

xy 平面上に N 人の人 1,2,\dots,N がおり、人 i は座標 (X_i,Y_i) にいます。
このうち、 K 人の人 A_1,A_2,\dots,A_K に同じ強さの明かりを持たせます。
座標 (x,y) にいる人が強さ R の明かりを持っている時、その明かりによって中心 (x,y) 、半径 R の円の内部全体(境界を含む)が照らされます。
すべての人が少なくとも 1 つの明かりによって照らされるために必要な明かりの強さの最小値を求めてください。

制約

  • 入力は全て整数
  • 1 \le K < N \le 1000
  • 1 \le A_1 < A_2 < \dots < A_K \le N
  • |X_i|,|Y_i| \le 10^5
  • i \neq j ならば (X_i,Y_i) \neq (X_j,Y_j)

入力

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

N K
A_1 A_2 \dots A_K
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N

出力

答えを実数として出力せよ。
出力された解と想定解との絶対誤差または相対誤差が 10^{-5} 以下であるならば、出力は正しいと見なされる。


入力例 1

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

出力例 1

2.23606797749978969

この入力では人が 4 人おり、そのうち人 2,3 が明かりを持ちます。
R \ge \sqrt{5} \approx 2.236068 である時、すべての人が少なくとも 1 つの明かりによって照らされます。


入力例 2

2 1
2
-100000 -100000
100000 100000

出力例 2

282842.712474619009

入力例 3

8 3
2 6 8
-17683 17993
93038 47074
58079 -57520
-41515 -89802
-72739 68805
24324 -73073
71049 72103
47863 19268

出力例 3

130379.280458974768

Score : 200 points

Problem Statement

There are N people numbered 1, 2, \dots, N in the xy-plane. Person i is at the coordinates (X_i, Y_i).
K of these people, Persons A_1, A_2, \dots, A_K, will receive lights of the same strength.
When a person at coordinates (x, y) has a light of strength R, it lights up the interior of a circle of radius R centered at (x, y) (including the boundary).
Find the minimum strength of the lights needed for every person to be lit by at least one light.

Constraints

  • All values in input are integers.
  • 1 \le K < N \le 1000
  • 1 \le A_1 < A_2 < \dots < A_K \le N
  • |X_i|,|Y_i| \le 10^5
  • (X_i,Y_i) \neq (X_j,Y_j), if i \neq j.

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 \dots A_K
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N

Output

Print the answer as a real number.
Your output will be considered correct if its absolute or relative error from the judge's output is at most 10^{-5}.


Sample Input 1

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

Sample Output 1

2.23606797749978969

This input contains four people. Among them, Persons 2 and 3 will have lights.
Every person will be lit by at least one light if R \ge \sqrt{5} \approx 2.236068.


Sample Input 2

2 1
2
-100000 -100000
100000 100000

Sample Output 2

282842.712474619009

Sample Input 3

8 3
2 6 8
-17683 17993
93038 47074
58079 -57520
-41515 -89802
-72739 68805
24324 -73073
71049 72103
47863 19268

Sample Output 3

130379.280458974768
C - ±1 Operation 1

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

整数 X が与えられます。この X に以下を施すことを「操作」と呼びます。

  • 以下の 2 つのうちどちらかを選択し、実行する。
    • X1 を加算する。
    • X から 1 を減算する。

初項 A 、公差 D 、項数 N の等差数列 S に含まれる数を「良い数」と呼びます。
「操作」を 0 回以上何度でも使って X を「良い数」にする時、必要な「操作」の最小回数を求めてください。

制約

  • 入力は全て整数
  • -10^{18} \le X,A \le 10^{18}
  • -10^6 \le D \le 10^6
  • 1 \le N \le 10^{12}

入力

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

X A D N

出力

答えを整数として出力せよ。


入力例 1

6 2 3 3

出力例 1

1

A=2,D=3,N=3 であるため、 S=(2,5,8) です。
X=6 を「良い数」にするためには、 X から 1 を減算することを 1 度行えば良いです。
0 回の操作で X を「良い数」にすることはできません。


入力例 2

0 0 0 1

出力例 2

0

D=0 である場合もあります。また、操作を 1 回も必要としない場合もあります。


入力例 3

998244353 -10 -20 30

出力例 3

998244363

入力例 4

-555555555555555555 -1000000000000000000 1000000 1000000000000

出力例 4

444445

Score : 300 points

Problem Statement

You are given an integer X. The following action on this integer is called an operation.

  • Choose and do one of the following.
    • Add 1 to X.
    • Subtract 1 from X.

The terms in the arithmetic progression S with N terms whose initial term is A and whose common difference is D are called good numbers.
Consider performing zero or more operations to make X a good number. Find the minimum number of operations required to do so.

Constraints

  • All values in input are integers.
  • -10^{18} \le X,A \le 10^{18}
  • -10^6 \le D \le 10^6
  • 1 \le N \le 10^{12}

Input

Input is given from Standard Input in the following format:

X A D N

Output

Print the answer as an integer.


Sample Input 1

6 2 3 3

Sample Output 1

1

Since A=2,D=3,N=3, we have S=(2,5,8).
You can subtract 1 from X once to make X=6 a good number.
It is impossible to make X good in zero operations.


Sample Input 2

0 0 0 1

Sample Output 2

0

We might have D=0. Additionally, no operation might be required.


Sample Input 3

998244353 -10 -20 30

Sample Output 3

998244363

Sample Input 4

-555555555555555555 -1000000000000000000 1000000 1000000000000

Sample Output 4

444445
D - ±1 Operation 2

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

長さ N の数列 A=(A_1,A_2,\dots,A_N) が与えられます。この A に以下を施すことを「操作」と呼びます。

  • まず、 1 \le i \le N を満たす整数 i を選択する。
  • 次に、以下の 2 つのうちどちらかを選択し、実行する。
    • A_i1 を加算する。
    • 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_31 加算することを 3 度繰り返す。

9 回以下の「操作」で A の要素を全て 5 にすることはできません。

2 つ目の質問について、 A71 回の「操作」を施すことで、 A の要素を全て 20 にすることができます。

3 つ目の質問について、 A29 回の「操作」を施すことで、 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.

E - Lucky Numbers

Time Limit: 4 sec / Memory Limit: 1024 MB

配点 : 500

問題文

長さ N-1 の整数列 S = (S_1, S_2, \ldots, S_{N-1}) および、「ラッキーナンバー」として M 個の相異なる整数 X_1, X_2, \ldots, X_M が与えられます。

長さ N の整数列 A = (A_1, A_2, \ldots, A_N) であって、次の条件を満たすものを「良い数列」と呼びます。

すべての i = 1, 2, \ldots, N-1 について、A_i + A_{i+1} = S_i が成り立つ。

良い数列 A1 つ選ぶときの、A の要素のうちラッキーナンバーであるものの個数(すなわち、A_i \in \lbrace X_1, X_2, \ldots, X_M \rbrace となる 1 以上 N 以下の整数 i の個数)としてあり得る最大値を求めてください。

制約

  • 2 \leq N \leq 10^5
  • 1 \leq M \leq 10
  • -10^9 \leq S_i \leq 10^9
  • -10^9 \leq X_i \leq 10^9
  • X_1 \lt X_2 \lt \cdots \lt X_M
  • 入力はすべて整数

入力

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

N M
S_1 S_2 \ldots S_{N-1}
X_1 X_2 \ldots X_M

出力

良い数列 A1 つ選ぶときの、A の要素のうちラッキーナンバーであるものの個数としてありうる最大値を出力せよ。


入力例 1

9 2
2 3 3 4 -4 -7 -4 -1
-1 5

出力例 1

4

良い数列 A として A = (3, -1, 4, -1, 5, -9, 2, -6, 5) を選ぶと、A の要素のうちラッキーナンバーであるものは A_2, A_4, A_5, A_94 個となり、これが考えられる中で最大です。


入力例 2

20 10
-183260318 206417795 409343217 238245886 138964265 -415224774 -499400499 -313180261 283784093 498751662 668946791 965735441 382033304 177367159 31017484 27914238 757966050 878978971 73210901
-470019195 -379631053 -287722161 -231146414 -84796739 328710269 355719851 416979387 431167199 498905398

出力例 2

8

Score : 500 points

Problem Statement

You are given a sequence of N-1 integers S = (S_1, S_2, \ldots, S_{N-1}), and M distinct integers X_1, X_2, \ldots, X_M, which are called lucky numbers.

A sequence of N integers A = (A_1, A_2, \ldots, A_N) satisfying the following condition is called a good sequence.

A_i + A_{i+1} = S_i holds for every i = 1, 2, \ldots, N-1.

Find the maximum possible number of terms that are lucky numbers in a good sequence A, that is, the maximum possible number of integers i between 1 and N such that A_i \in \lbrace X_1, X_2, \ldots, X_M \rbrace.

Constraints

  • 2 \leq N \leq 10^5
  • 1 \leq M \leq 10
  • -10^9 \leq S_i \leq 10^9
  • -10^9 \leq X_i \leq 10^9
  • X_1 \lt X_2 \lt \cdots \lt X_M
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
S_1 S_2 \ldots S_{N-1}
X_1 X_2 \ldots X_M

Output

Print the maximum possible number of terms that are lucky numbers in a good sequence A.


Sample Input 1

9 2
2 3 3 4 -4 -7 -4 -1
-1 5

Sample Output 1

4

A good sequence A = (3, -1, 4, -1, 5, -9, 2, -6, 5) contains four terms that are lucky numbers: A_2, A_4, A_5, A_9, which is the maximum possible count.


Sample Input 2

20 10
-183260318 206417795 409343217 238245886 138964265 -415224774 -499400499 -313180261 283784093 498751662 668946791 965735441 382033304 177367159 31017484 27914238 757966050 878978971 73210901
-470019195 -379631053 -287722161 -231146414 -84796739 328710269 355719851 416979387 431167199 498905398

Sample Output 2

8
F - Pre-order and In-order

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

1, 2, \ldots, N と番号づけられた N 個の頂点を持つ二分木を考えます。 ここで、二分木とは各頂点が高々 2 個の子を持つ根付き木です。より具体的には、二分木の各頂点は高々 1 個の左の子と高々 1 個の右の子を持ちます。

頂点 1 を根とする二分木であって、下記の条件を満たすものが存在するかを判定し、存在する場合はその一例を示してください。

  • すべての頂点を深さ優先探索における行きがけ順(pre-order)で並べた列が (P_1, P_2, \ldots, P_N) である。
  • すべての頂点を深さ優先探索における通りがけ順(in-order)で並べた列が (I_1, I_2, \ldots, I_N) である。

制約

  • 2 \leq N \leq 2 \times 10^5
  • N は整数
  • (P_1, P_2, \ldots, P_N)(1, 2, \ldots, N) の順列
  • (I_1, I_2, \ldots, I_N)(1, 2, \ldots, N) の順列

入力

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

N
P_1 P_2 \ldots P_N
I_1 I_2 \ldots I_N

出力

問題文中の条件を満たすような頂点 1 を根とする二分木が存在しない場合は -1 を出力せよ。
存在する場合は、条件を満たす二分木の一例を下記の形式にしたがって N 行にわたって出力せよ。 すなわち、i = 1, 2, \ldots, N について、i 行目には頂点 i の左の子の番号 L_i と右の子の番号 R_i を出力せよ。 ただし、左の子(または右の子)を持たない場合は L_i(または R_i )として 0 を出力せよ。
条件を満たすような頂点 1 を根とする二分木が複数存在する場合は、そのうちどれを出力しても正解となる。

L_1 R_1
L_2 R_2
\vdots
L_N R_N

入力例 1

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

出力例 1

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

次の画像に示す、頂点 1 を根とする二分木が問題文中の条件を満たします。


入力例 2

2
2 1
1 2

出力例 2

-1

問題文中の条件を満たすような頂点 1 を根とする二分木は存在しません。よって -1 を出力します。

Score : 500 points

Problem Statement

Consider a binary tree with N vertices numbered 1, 2, \ldots, N. Here, a binary tree is a rooted tree where each vertex has at most two children. Specifically, each vertex in a binary tree has at most one left child and at most one right child.

Determine whether there exists a binary tree rooted at Vertex 1 satisfying the conditions below, and present such a tree if it exists.

  • The depth-first traversal of the tree in pre-order is (P_1, P_2, \ldots, P_N).
  • The depth-first traversal of the tree in in-order is (I_1, I_2, \ldots, I_N).

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • N is an integer.
  • (P_1, P_2, \ldots, P_N) is a permutation of (1, 2, \ldots, N).
  • (I_1, I_2, \ldots, I_N) is a permutation of (1, 2, \ldots, N).

Input

Input is given from Standard Input in the following format:

N
P_1 P_2 \ldots P_N
I_1 I_2 \ldots I_N

Output

If there is no binary tree rooted at Vertex 1 satisfying the conditions in Problem Statement, print -1.
Otherwise, print one such tree in N lines as follows. For each i = 1, 2, \ldots, N, the i-th line should contain L_i and R_i, the indices of the left and right children of Vertex i, respectively. Here, if Vertex i has no left (right) child, L_i (R_i) should be 0.
If there are multiple binary trees rooted at Vertex 1 satisfying the conditions, any of them will be accepted.

L_1 R_1
L_2 R_2
\vdots
L_N R_N

Sample Input 1

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

Sample Output 1

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

The binary tree rooted at Vertex 1 shown in the following image satisfies the conditions.


Sample Input 2

2
2 1
1 2

Sample Output 2

-1

No binary tree rooted at Vertex 1 satisfies the conditions, so -1 should be printed.

G - Constrained Nim

Time Limit: 4 sec / Memory Limit: 1024 MB

配点 : 600

問題文

高橋君と青木君が、いくつかの石からなる N 個の山を用いて石とりゲームで対戦します。

はじめ、i = 1, 2, \ldots, N について、i 番目の山は A_i 個の石からなります。 高橋君からはじめ、2 人は交互に次の行動をくりかえします。

石が 1 個以上残っている山を 1 つ選び、その山から 1 個以上の石を取り除く。

ただし、このゲームには M 種類の禁じ手があり、禁じ手に該当する行動を行うことはできません。
i = 1, 2, \ldots, M について、i 種類目の禁じ手は「ちょうど X_i 個の石からなる山からちょうど Y_i 個の石を取り除くこと」です。

先に行動を行うことができなくなった方のプレイヤーの負けとなり、負けなかった方のプレイヤーの勝ちとなります。 両者が自身が勝つために最適な戦略をとるとき、どちらのプレイヤーが勝つかを答えてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^{18}
  • 1 \leq Y_i \leq X_i \leq 10^{18}
  • i \neq j \Rightarrow (X_i, Y_i) \neq (X_j, Y_j)
  • 入力はすべて整数

入力

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

N M
A_1 A_2 \ldots A_N
X_1 Y_1
X_2 Y_2
\vdots
X_M Y_M

出力

両者が自身が勝つために最適な戦略をとるとき、高橋君が勝つならば Takahashi と、青木君が勝つならば Aoki と出力せよ。


入力例 1

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

出力例 1

Takahashi

i = 1, 2, 3 について、i 番目の山にある石の個数を A'_i とし、 それぞれの山にある石の個数を数列 A' = (A'_1, A'_2, A'_3) を用いて表すことにします。

ゲームが始まる前の時点では、A' = (1, 2, 4) です。ゲームの進行の一例として次のものがあります。

  • まず、高橋君が 3 番目の山から石を 1 個取り除く。その結果、A' = (1, 2, 3) となる。
  • 次に、青木君が 2 番目の山から石を 2 個取り除く。その結果、A' = (1, 0, 3) となる。
  • さらに、高橋君が 3 番目の山から石を 2 個取り除く。その結果、A' = (1, 0, 1) となる。

その後の時点で、1 番目と 3 番目の山にはまだ石が 1 個ずつ残っていますが、ちょうど 1 個の石からなる山からちょうど 1 個の石を取り除くことは 4 種類目の禁じ手に該当するため、青木君は行動を行うことができません。したがって、高橋君の勝ちとなります。


入力例 2

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

出力例 2

Aoki

Score : 600 points

Problem Statement

Takahashi and Aoki will play a game against each other using N piles of stones.

Initially, for each i = 1, 2, \ldots, N, the i-th pile is composed of A_i stones. The players alternately perform the following action, with Takahashi going first.

  • Choose a pile with at least one stone remaining, and remove one or more stones.

However, there are M forbidden moves.
For each i = 1, 2, \ldots, M, it is not allowed to remove exactly Y_i stones from a pile composed of exactly X_i stones.

The first player to become unable to perform the action loses, resulting in the other player's victory. Which player will win when both players employ the optimal strategy for the victory?

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^{18}
  • 1 \leq Y_i \leq X_i \leq 10^{18}
  • i \neq j \Rightarrow (X_i, Y_i) \neq (X_j, Y_j)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 \ldots A_N
X_1 Y_1
X_2 Y_2
\vdots
X_M Y_M

Output

If Takahashi will win when both players employ the optimal strategy for the victory, print Takahashi; if Aoki will win, print Aoki.


Sample Input 1

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

Sample Output 1

Takahashi

For each i = 1, 2, 3, let A'_i be the number of stones remaining in the i-th pile. Now, we use the sequence A' = (A'_1, A'_2, A'_3) to represent the numbers of stones remaining in the piles.

Before the start of the game, we have A' = (1, 2, 4). One possible progression of the game is as follows.

  • First, Takahashi removes 1 stone from the 3-rd pile. Now, A' = (1, 2, 3).
  • Next, Aoki removes 2 stones from the 2-nd pile. Now, A' = (1, 0, 3).
  • Then, Takahashi removes 2 stones from the 3-rd pile. Now, A' = (1, 0, 1).

At this point, the 1-st and 3-rd piles still have 1 stone each, but it is forbidden ー as the fourth forbidden move ー to remove exactly 1 stone from a pile composed of exactly 1 stone, so Aoki cannot play. Thus, Takahashi wins.


Sample Input 2

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

Sample Output 2

Aoki
Ex - Range Harvest Query

Time Limit: 8 sec / Memory Limit: 1024 MB

配点 : 600

問題文

N 本の木があります。0 日目にはどの木にも実は一つもなっていません。
1 日目以降の毎朝、それぞれの i = 1, 2, \ldots, N について、i 番目の木に i 個の実が増えます。

高橋君は Q 回の収穫作業をします。 i = 1, 2, \ldots, Q について、i 回目の収穫作業は D_i 日目の夜に行われ、その時点で L_i 番目から R_i 番目の木になっているすべての実を収穫します。

Q 回の収穫作業のそれぞれについて、高橋君が収穫する実の個数を 998244353 で割ったあまりを出力してください。

制約

  • 1 \leq N \leq 10^{18}
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq D_1 \lt D_2 \lt \cdots \lt D_Q \leq 10^{18}
  • 1 \leq L_i \leq R_i \leq N
  • 入力はすべて整数

入力

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

N Q
D_1 L_1 R_1
D_2 L_2 R_2
\vdots
D_Q L_Q R_Q

出力

Q 行出力せよ。 i = 1, 2, \ldots, Q について、i 行目には高橋君が i 回目の収穫作業で収穫する実の個数を 998244353 で割ったあまりを出力せよ。


入力例 1

5 3
2 2 3
3 3 4
5 1 5

出力例 1

10
15
50

i = 1, 2, 3, 4, 5 について、i 番目の木になっている実の個数を A_i とし、 それぞれの木になっている実の個数を数列 A = (A_1, A_2, A_3, A_4, A_5) を用いて表すことにします。

  • 0 日目、A = (0, 0, 0, 0, 0) です。
  • 1 日目の朝、それぞれの木に新たに実がなり、A = (1, 2, 3, 4, 5) となります。
  • 2 日目の朝、それぞれの木に新たに実がなり、A = (2, 4, 6, 8, 10) となります。
  • 2 日目の夜、高橋君は 1 回目の収穫を行います。4 + 6 = 10 個の木の実を収穫し、A = (2, 0, 0, 8, 10) となります。
  • 3 日目の朝、それぞれの木に新たに実がなり、A = (3, 2, 3, 12, 15) となります。
  • 3 日目の夜、高橋君は 2 回目の収穫を行います。3 + 12 = 15 個の木の実を収穫し、A = (3, 2, 0, 0, 15) となります。
  • 4 日目の朝、それぞれの木に新たに実がなり、A = (4, 4, 3, 4, 20) となります。
  • 5 日目の朝、それぞれの木に新たに実がなり、A = (5, 6, 6, 8, 25) となります。
  • 5 日目の夜、高橋君は 3 回目の収穫を行います。5 + 6 + 6 + 8 + 25 = 50 個の木の実を収穫し、A = (0, 0, 0, 0, 0) となります。

入力例 2

711741968710511029 1
82803157126515475 516874290286751784 588060532191410838

出力例 2

603657470

998244353 で割ったあまりを出力することに注意してください。

Score : 600 points

Problem Statement

There are N trees. On Day 0, each tree bears no fruits.
On the morning of Day 1 and subsequent days, the i-th tree bears i new fruits for each i = 1, 2, \ldots, N.

Takahashi will perform Q harvesting. For each i = 1, 2, \ldots, Q, the i-th harvesting takes place on the night of Day D_i, collecting all fruits remaining on the L_i-th through R_i-th trees at that point.

For each of the Q harvesting, print the number of fruits Takahashi will collect, modulo 998244353.

Constraints

  • 1 \leq N \leq 10^{18}
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq D_1 \lt D_2 \lt \cdots \lt D_Q \leq 10^{18}
  • 1 \leq L_i \leq R_i \leq N
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N Q
D_1 L_1 R_1
D_2 L_2 R_2
\vdots
D_Q L_Q R_Q

Output

Print Q lines. For each i = 1, 2, \ldots, Q, the i-th line should contain the number of fruits Takahashi will collect in the i-th harvesting, modulo 998244353.


Sample Input 1

5 3
2 2 3
3 3 4
5 1 5

Sample Output 1

10
15
50

For each i = 1, 2, 3, 4, 5, let A_i be the number of fruits remaining on the i-th tree. Now, we use the sequence A = (A_1, A_2, A_3, A_4, A_5) to represent the numbers of fruits remaining in the trees.

  • On Day 0, we have A = (0, 0, 0, 0, 0).
  • On the morning of Day 1, each tree bears new fruits, and we have A = (1, 2, 3, 4, 5).
  • On the morning of Day 2, each tree bears new fruits, and we have A = (2, 4, 6, 8, 10).
  • On the night of Day 2, Takahashi performs the 1-st harvesting. 4 + 6 = 10 fruits are collected, and we have A = (2, 0, 0, 8, 10).
  • On the morning of Day 3, each tree bears new fruits, and we have A = (3, 2, 3, 12, 15).
  • On the night of Day 3, Takahashi performs the 2-nd harvesting. 3 + 12 = 15 fruits are collected, and we have A = (3, 2, 0, 0, 15).
  • On the morning of Day 4, each tree bears new fruits, and we have A = (4, 4, 3, 4, 20).
  • On the morning of Day 5, each tree bears new fruits, and we have A = (5, 6, 6, 8, 25).
  • On the night of Day 5, Takahashi performs the 3-rd harvesting. 5 + 6 + 6 + 8 + 25 = 50 fruits are collected, and we have A = (0, 0, 0, 0, 0).

Sample Input 2

711741968710511029 1
82803157126515475 516874290286751784 588060532191410838

Sample Output 2

603657470

Be sure to print the numbers modulo 998244353.