A - Is it rated?

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

現在 AtCoder で開催されている AtCoder Regular Contest (ARC) には、Div. 1 と Div. 22 種類が存在します。 ARC Div. 1 では レーティング1600 以上 2999 以下の人が、ARC Div. 2 ではレーティングが 1200 以上 2399 以下の人がそれぞれ Rated 対象 となります。

正整数 R, X が与えられます。

レーティングが R の人は ARC Div. X において Rated 対象ですか?

制約

  • 1\leq R \leq 4229
  • 1\leq X \leq 2
  • 入力は全て整数

入力

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

R X

出力

レーティングが R の人が ARC Div. X において Rated 対象ならば Yes を、そうでないならば No を出力せよ。


入力例 1

2000 1

出力例 1

Yes

20001600 以上 2999 以下であるため、レーティングが 2000 の人は ARC Div. 1 において Rated 対象です。


入力例 2

1000 1

出力例 2

No

10001600 未満であるため、レーティングが 1000 の人は ARC Div. 1 において Rated 対象ではありません。


入力例 3

1500 2

出力例 3

Yes

15001200 以上 2399 以下であるため、レーティングが 1500 の人は ARC Div. 2 において Rated 対象です。


入力例 4

2800 2

出力例 4

No

28002399 より大きいため、レーティングが 2800 の人は ARC Div. 2 において Rated 対象ではありません。

Score : 100 points

Problem Statement

AtCoder Regular Contest (ARC) currently has two divisions: Div. 1 and Div. 2. In ARC Div. 1, participants whose rating is between 1600 and 2999, inclusive, are rated. In ARC Div. 2, participants whose rating is between 1200 and 2399, inclusive, are rated.

You are given positive integers R and X.

Determine whether a person with rating R is rated in ARC Div. X.

Constraints

  • 1 \le R \le 4229
  • 1 \le X \le 2
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

R X

Output

If a person with rating R is rated in ARC Div. X, output Yes; otherwise, output No.


Sample Input 1

2000 1

Sample Output 1

Yes

Because 2000 lies between 1600 and 2999, a person with rating 2000 is rated in ARC Div. 1.


Sample Input 2

1000 1

Sample Output 2

No

Because 1000 is less than 1600, a person with rating 1000 is not rated in ARC Div. 1.


Sample Input 3

1500 2

Sample Output 3

Yes

Because 1500 lies between 1200 and 2399, a person with rating 1500 is rated in ARC Div. 2.


Sample Input 4

2800 2

Sample Output 4

No

Because 2800 exceeds 2399, a person with rating 2800 is not rated in ARC Div. 2.

B - Not All

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

長さ N の整数列 A=(A_1,A_2,\dots,A_N) および正整数 M が与えられます。

A の末尾の要素を削除するという操作を 0 回以上 N 回以下行うことで、以下の条件が満たされないようにしたいです。

  • 条件A には 1 以上 M 以下の整数がすべて含まれている。

必要な操作回数の最小値を求めてください。

なお、本問題の制約下において、操作を 0 回以上 N 回以下行うことで上述の条件が満たされないようにすることが必ず可能であることが証明できます。

制約

  • 1\leq M \leq N \leq 100
  • 1\leq A_i \leq M
  • 入力は全て整数

入力

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

N M
A_1 A_2 \dots A_N

出力

条件が満たされなくなるために必要な操作回数の最小値を出力せよ。


入力例 1

5 3
3 2 3 1 2

出力例 1

2

最初、A=(3,2,3,1,2) です。このとき、A には 1 以上 3 以下の整数がすべて含まれるため条件を満たします。

A の末尾の要素を削除する操作を 1 回行うと、A=(3,2,3,1) となります。このとき、A には 1 以上 3 以下の整数がすべて含まれるため条件を満たします。

A の末尾の要素を削除する操作をもう 1 回行うと、A=(3,2,3) となります。このとき、A には 1 が含まれないため条件を満たしません。

よって、条件が満たされなくなるために必要な操作回数の最小値は 2 です。


入力例 2

4 3
1 3 1 3

出力例 2

0

A には最初から 2 が含まれず条件を満たさないため、操作を 1 回も行う必要がありません。


入力例 3

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

出力例 3

6

Score : 200 points

Problem Statement

You are given an integer sequence A = (A_1, A_2, \dots, A_N) of length N and a positive integer M.

Your goal is to make the following condition false by performing this operation between 0 and N times (inclusive): remove the last element of A.

  • Condition: A contains every integer from 1 through M.

Find the minimum number of operations required.

Under the constraints of this problem, it can be proved that it is always possible to make the condition false by performing the operation between 0 and N times.

Constraints

  • 1 \le M \le N \le 100
  • 1 \le A_i \le M
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N M
A_1 A_2 \dots A_N

Output

Output the minimum number of operations required to make the condition false.


Sample Input 1

5 3
3 2 3 1 2

Sample Output 1

2

Initially, A = (3,2,3,1,2). Since A contains every integer from 1 through 3, the condition holds.

If you perform the operation once, A = (3,2,3,1). The condition still holds.

If you perform the operation once more, A = (3,2,3). The integer 1 is missing, so the condition no longer holds.

Therefore, the minimum required number of operations is 2.


Sample Input 2

4 3
1 3 1 3

Sample Output 2

0

Since A initially lacks the integer 2, the condition is already false, so no operation is needed.


Sample Input 3

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

Sample Output 3

6
C - Sum of Product

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

長さ N の整数列 A=(A_1,A_2,\dots,A_N) が与えられます。

\displaystyle \sum_{1\leq i< j\leq N} A_iA_j の値を求めてください。

制約

  • 2\leq N \leq 3\times 10^5
  • 1\leq A_i \leq 10^4
  • 入力は全て整数

入力

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

N
A_1 A_2 \dots A_N

出力

答えを出力せよ。


入力例 1

3
4 2 3

出力例 1

26

\displaystyle \sum_{1\leq i< j\leq N} A_iA_j=A_1A_2+A_1A_3+A_2A_3=4\cdot 2+4\cdot 3+2\cdot 3=26 です。


入力例 2

2
9 45

出力例 2

405

入力例 3

10
7781 8803 8630 9065 8831 9182 8593 7660 7548 8617

出力例 3

3227530139

Score : 300 points

Problem Statement

You are given a length-N integer sequence A = (A_1, A_2, \dots, A_N).

Compute the value of \displaystyle \sum_{1\leq i< j\leq N} A_iA_j.

Constraints

  • 2 \le N \le 3 \times 10^5
  • 1 \le A_i \le 10^4
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N
A_1 A_2 \dots A_N

Output

Output the answer.


Sample Input 1

3
4 2 3

Sample Output 1

26

We have \displaystyle \sum_{1\leq i< j\leq N} A_iA_j=A_1A_2+A_1A_3+A_2A_3=4\cdot 2+4\cdot 3+2\cdot 3=26.


Sample Input 2

2
9 45

Sample Output 2

405

Sample Input 3

10
7781 8803 8630 9065 8831 9182 8593 7660 7548 8617

Sample Output 3

3227530139
D - Escape Route

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

ある日、映画館を訪れた高橋君は、館内の全ての床に、最寄りの非常口の方向を示す矢印を描くことにしました。

H マス、横 W マスのグリッドが与えられます。グリッドの上から i 行目、左から j 列目のマスを (i, j) と呼びます。
各マスの状態は、次のいずれかの文字 S_{i,j} で表されます。

  • . : 通路マス
  • # : 壁マス
  • E : 非常口

任意の通路マス (i, j) に対して、(i, j) と最寄りの非常口との距離 d(i, j) を次のように定義します。

  • (i, j) からスタートして、上下左右に隣接する 壁マスでないマス へ移動することを繰り返して非常口に到達する際の、必要な最小の移動回数。

ここで、入力として与えられるグリッドにおいて、全ての通路マスに対し d(i, j) が定義可能であることが保証されます。すなわち、すべての通路マス (i, j) において、通路マスのみを経由することで到達可能な非常口が少なくとも 1 つ存在します。

このとき、以下の条件を満たすように、すべての通路マスに対して「上下左右いずれかの方向の矢印」を書き込んでください。

  • 各通路マス (i, j) について、(i,j) にいる状態から開始して、以下の操作を d(i, j) 回行うことで非常口に到達することができる。
    • 現在いるマスに書かれた矢印の方向に従って 1 マス移動する。(ただし、壁マスやグリッドの外へは移動できない。)

制約

  • 2 \leq H \leq 1000
  • 2 \leq W \leq 1000
  • S_{i,j}., # または E
  • 全ての通路マス (i, j) について、そこからいずれかの非常口に到達することが可能である
  • H, W は整数である

入力

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

H W
S_{1,1}S_{1,2}\dots S_{1,W}
S_{2,1}S_{2,2}\dots S_{2,W}
\vdots
S_{H,1}S_{H,2}\dots S_{H,W}

出力

矢印を書き込んだ後のマス (i, j) の状態を T_{i,j} とする。T_{i,j} は次のいずれかである。

  • ^ : 上矢印が書かれた通路マス
  • v : 下矢印が書かれた通路マス
  • < : 左矢印が書かれた通路マス
  • > : 右矢印が書かれた通路マス
  • # : 壁マス
  • E : 非常口

これらを以下の形式で出力せよ。

T_{1,1}T_{1,2}\dots T_{1,W}
T_{2,1}T_{2,2}\dots T_{2,W}
\vdots
T_{H,1}T_{H,2}\dots T_{H,W}

答えが複数ある場合は、どれを出力しても正答とみなされる。


入力例 1

3 4
...E
.#..
....

出力例 1

>>>E
^#>^
>>>^

出力例の (2, 3) について問題文の条件が成立することを確認します。(2, 3) と最寄りの非常口の距離は 2 です。そして、出力例のように書き込まれた矢印に従って移動することで 2 回の移動で非常口に到達することができます。
他の通路マスについても問題文の条件が成立することが確認できます。


入力例 2

3 2
##
##
##

出力例 2

##
##
##

通路マスおよび非常口が存在しない場合もあります。


入力例 3

7 20
....................
..#..#..####..#E##..
..#..#..#..#..#.....
..E###..#..#..####..
.....#..#..E.....#..
.....#..####..####..
....................

出力例 3

>v<<<<<>>>>>>>>v<<<<
>v#^<#^^####v^#E##vv
>v#^<#v^#>v#vv#^<<<<
>>E###vv#>v#vv####^<
>>^<<#vv#>>E<<<<<#^<
>>^<<#vv####^<####^<
>>^<<<<<>>>>^<<<<<^<

Score : 400 points

Problem Statement

One day, Takahashi visited a cinema and decided to draw arrows on every floor tile, each pointing toward the nearest emergency exit.

You are given a grid with H rows and W columns. Let (i, j) denote the cell at the i-th row from the top and the j-th column from the left.
Each cell is represented by one of the following characters S_{i,j}:

  • . : corridor cell
  • # : wall cell
  • E : emergency exit

For any corridor cell (i, j), define d(i, j), the distance to the nearest emergency exit, as follows:

  • Starting from (i, j), repeatedly move to an adjacent non-wall cell in one of the four cardinal directions (up, down, left, right) until you reach an emergency exit. d(i, j) is the minimum number of moves required.

It is guaranteed that d(i, j) is definable for every corridor cell in the given grid; that is, every corridor cell (i, j) has at least one emergency exit reachable by passing through only corridor cells.

Write exactly one arrow (up, down, left, or right) in every corridor cell so that the following condition holds:

  • For every corridor cell (i, j), if you start at (i, j) and perform the following action d(i, j) times, you reach an emergency exit:
    • Move one cell in the direction of the arrow written in your current cell. (You cannot move into a wall cell or outside the grid.)

Constraints

  • 2 \le H \le 1000
  • 2 \le W \le 1000
  • Each S_{i,j} is ., #, or E.
  • Every corridor cell (i, j) has at least one reachable emergency exit.
  • H and W are integers.

Input

The input is given from Standard Input in the following format:

H W
S_{1,1}S_{1,2}\dots S_{1,W}
S_{2,1}S_{2,2}\dots S_{2,W}
\vdots
S_{H,1}S_{H,2}\dots S_{H,W}

Output

Let T_{i,j} be the state of cell (i, j) after writing the arrows. T_{i,j} is one of the following:

  • ^ : corridor cell with an upward arrow
  • v : corridor cell with a downward arrow
  • < : corridor cell with a leftward arrow
  • > : corridor cell with a rightward arrow
  • # : wall cell
  • E : emergency exit

Output the grid in the following format:

T_{1,1}T_{1,2}\dots T_{1,W}
T_{2,1}T_{2,2}\dots T_{2,W}
\vdots
T_{H,1}T_{H,2}\dots T_{H,W}

If multiple solutions exist, any of them will be accepted.


Sample Input 1

3 4
...E
.#..
....

Sample Output 1

>>>E
^#>^
>>>^

Let us verify the condition for (2, 3) in the sample output. The distance from (2, 3) to the nearest emergency exit is 2, and following the arrows written in the sample output leads to an emergency exit in two moves.
The condition can be verified for every other corridor cell as well.


Sample Input 2

3 2
##
##
##

Sample Output 2

##
##
##

There may be cases with no corridor cells or emergency exits.


Sample Input 3

7 20
....................
..#..#..####..#E##..
..#..#..#..#..#.....
..E###..#..#..####..
.....#..#..E.....#..
.....#..####..####..
....................

Sample Output 3

>v<<<<<>>>>>>>>v<<<<
>v#^<#^^####v^#E##vv
>v#^<#v^#>v#vv#^<<<<
>>E###vv#>v#vv####^<
>>^<<#vv#>>E<<<<<#^<
>>^<<#vv####^<####^<
>>^<<<<<>>>>^<<<<<^<
E - Fruit Lineup

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 475

問題文

A 個のリンゴと B 個のオレンジと C 個のバナナと D 個のブドウがあります。
これらの A+B+C+D 個の果物を、以下の条件全てを満たすように左右一列に並べる方法は何通りありますか?答えを 998244353 で割った余りを求めてください。

  • リンゴはすべて、バナナよりも左側に並べる。
  • リンゴはすべて、ブドウよりも左側に並べる。
  • オレンジはすべて、ブドウよりも左側に並べる。

ただし、同じ種類の果物同士は区別できないとします。

制約

  • 1 \leq A \leq 10^6
  • 1 \leq B \leq 10^6
  • 1 \leq C \leq 10^6
  • 1 \leq D \leq 10^6
  • A, B, C, D は全て整数

入力

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

A B C D

出力

果物を問題文の条件を満たすように左右一列に並べる方法の個数を 998244353 で割った余りを出力せよ。


入力例 1

1 1 1 1

出力例 1

5

問題文の条件を満たす果物の並べ方は次の 5 通りです。

  • リンゴ, オレンジ, バナナ, ブドウ
  • リンゴ, オレンジ, ブドウ, バナナ
  • リンゴ, バナナ, オレンジ, ブドウ
  • オレンジ, リンゴ, バナナ, ブドウ
  • オレンジ, リンゴ, ブドウ, バナナ

入力例 2

1 2 4 8

出力例 2

2211

入力例 3

834150 21994 467364 994225

出力例 3

947921688

Score : 475 points

Problem Statement

You have A apples, B oranges, C bananas, and D grapes.
How many ways are there to arrange these A+B+C+D fruits in a single row from left to right so that all of the following conditions hold? Find the count modulo 998244353.

  • Every apple is placed to the left of every banana.
  • Every apple is placed to the left of every grape.
  • Every orange is placed to the left of every grape.

Here, the apples are indistinguishable; the same goes for the oranges, the bananas, and the grapes.

Constraints

  • 1 \le A \le 10^6
  • 1 \le B \le 10^6
  • 1 \le C \le 10^6
  • 1 \le D \le 10^6
  • A, B, C, and D are integers.

Input

The input is given from Standard Input in the following format:

A B C D

Output

Output the number, modulo 998244353, of valid arrangements.


Sample Input 1

1 1 1 1

Sample Output 1

5

There are five valid arrangements:

  • apple, orange, banana, grape
  • apple, orange, grape, banana
  • apple, banana, orange, grape
  • orange, apple, banana, grape
  • orange, apple, grape, banana

Sample Input 2

1 2 4 8

Sample Output 2

2211

Sample Input 3

834150 21994 467364 994225

Sample Output 3

947921688
F - Chord Crossing

Time Limit: 3 sec / Memory Limit: 1024 MiB

配点 : 525

問題文

円周上に 2N 個の点が等間隔に並んでおり、ある点から始めて時計回りに 1,2,\dots,2N の番号が付けられています。

これらの点を結ぶ M 本の線分 1,2,\dots,M が存在し、線分 i は点 A_i と点 B_i を結んでいます。 ここで、A_iB_i は相異なる 偶数 です。 また、これらの線分のうちのどの 2 つの線分も共有点を持たないことが保証されます。

Q 個のクエリを処理してください。j 番目のクエリは以下の通りです。

  • 相異なる 2 つの 奇数 C_j, D_j が与えられる。上で与えられた M 本の線分 1,2,\dots,M のうち、点 C_j と点 D_j を結ぶ線分と共有点を持つものの本数を求めよ。

制約

  • 2\leq N \leq 10^6
  • 1\leq M \leq \min\left(\lfloor\frac{N}{2}\rfloor, 2\times 10^5\right)
  • 1\leq Q \leq 2\times 10^5
  • 1\leq A_i< B_i\leq 2N
  • 1\leq C_j< D_j\leq 2N
  • A_i, B_i は偶数
  • C_j, D_j は奇数
  • どの i_1, i_2\ (i_1 \neq i_2) についても、線分 i_1 と線分 i_2 は共有点を持たない
  • 入力は全て整数

入力

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

N M
A_1 B_1
A_2 B_2
\vdots
A_M B_M
Q
C_1 D_1
C_2 D_2
\vdots
C_Q D_Q

出力

Q 行出力せよ。 j\ (1\leq j \leq Q) 行目には、j 番目のクエリに対する答えを出力せよ。


入力例 1

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

出力例 1

1
2
0

上図は入力例 1 を図示したものであり、黒丸は 2N\ (=8) 個の点を、青い実線は最初に与えられる M\ (=2) 本の線分を、赤い点線はクエリで与えられる Q\ (=3) 本の線分をそれぞれ表しています。

  • 1 番目のクエリについて、最初に与えられた M 本の線分のうち、点 1 と点 3 を結ぶ線分と共有点を持つものは線分 11 本です。
  • 2 番目のクエリについて、最初に与えられた M 本の線分のうち、点 3 と点 7 を結ぶ線分と共有点を持つものは線分 1,22 本です。
  • 3 番目のクエリについて、最初に与えられた M 本の線分のうち、点 1 と点 5 を結ぶ線分と共有点を持つものは 0 本です。

入力例 2

20 7
24 34
26 28
18 38
2 14
8 12
30 32
20 22
10
7 29
31 39
9 21
19 29
15 21
11 39
17 21
15 31
5 25
25 31

出力例 2

3
3
4
1
2
2
2
3
3
1

Score : 525 points

Problem Statement

On a circle, 2N points are placed at equal intervals and numbered 1,2,\dots,2N in clockwise order starting from an arbitrary point.

There are M line segments numbered 1,2,\dots,M connecting these points. Segment i connects points A_i and B_i. Here, A_i and B_i are distinct even numbers. It is guaranteed that no two of these segments share a point.

Process Q queries. The j‑th query is as follows:

  • You are given two distinct odd numbers C_j and D_j. Among the M segments 1,2,\dots,M, find how many share a point with the segment connecting points C_j and D_j.

Constraints

  • 2 \le N \le 10^6
  • 1\leq M \leq \min\left(\lfloor\frac{N}{2}\rfloor, 2\times 10^5\right)
  • 1 \le Q \le 2 \times 10^5
  • 1 \le A_i < B_i \le 2N
  • 1 \le C_j < D_j \le 2N
  • A_i and B_i are even.
  • C_j and D_j are odd.
  • For any i_1 and i_2 (i_1 \neq i_2), segments i_1 and i_2 do not share a point.
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N M
A_1 B_1
A_2 B_2
\vdots
A_M B_M
Q
C_1 D_1
C_2 D_2
\vdots
C_Q D_Q

Output

Output Q lines. The j-th line (1\leq j \leq Q) should contain the answer to the j‑th query.


Sample Input 1

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

Sample Output 1

1
2
0

The figure above illustrates Sample Input 1. Black dots are the 2N\ (=8) points, blue solid lines are the initial M\ (=2) segments, and red dashed lines are the Q\ (=3) query segments.

  • For the first query, the segment connecting points 1 and 3 intersects one initial segment: segment 1.
  • For the second query, the segment connecting points 3 and 7 intersects two initial segments: segments 1 and 2.
  • For the third query, the segment connecting points 1 and 5 intersects zero initial segments.

Sample Input 2

20 7
24 34
26 28
18 38
2 14
8 12
30 32
20 22
10
7 29
31 39
9 21
19 29
15 21
11 39
17 21
15 31
5 25
25 31

Sample Output 2

3
3
4
1
2
2
2
3
3
1
G - Range Shuffle Query

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 625

問題文

長さ N の数列 A = (A_1, A_2, \dots, A_N) が与えられます。
Q 個のクエリが与えられるので処理してください。
クエリでは整数 L, R, X が与えられるので次の問題を解いてください。

AL 番目から R 番目までの要素を取り出してできる数列 B=(A_L, A_{L+1}, \dots, A_R) があります。
あなたは以下の一連の操作をちょうど 1 回行います。

  • まず、B から値が X 以上の要素を全て取り除く。
  • 次に、B の要素を自由に並べ替える。

操作後の B としてあり得るものは何通りありますか?答えを 998244353 で割った余りを求めてください。

制約

  • 1 \leq N \leq 2.5 \times 10^5
  • 1 \leq Q \leq 2.5 \times 10^5
  • 1 \leq A_i \leq N
  • 1 \leq L \leq R \leq N
  • 1 \leq X \leq N
  • 入力される値は全て整数

入力

入力は以下の形式で標準入力から与えられる。ここで \mathrm{query}_ii 番目のクエリを意味する。

N Q 
A_1 A_2 \dots A_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

各クエリは以下の形式で与えられる。

L R X

出力

Q 行出力せよ。i 行目には i 番目のクエリの答えを出力せよ。


入力例 1

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

出力例 1

3
1
6

1 番目のクエリについて、操作後の B としてあり得るものは (1,1,2), (1,2,1), (2,1,1)3 通りです。
2 番目のクエリについて、操作後の B としてあり得るものは空の列 ()1 通りです。


入力例 2

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

出力例 2

1
120
6
3
1
2

Score : 625 points

Problem Statement

You are given a length-N sequence A = (A_1, A_2, \dots, A_N).
Process Q queries.
Each query gives you integers L, R, X and asks you to solve the following.

Let B = (A_L, A_{L+1}, \dots, A_R) be the sequence formed by the L-th through R-th elements of A.
Perform the following procedure exactly once:

  • First, remove from B every element whose value is at least X.
  • Then, rearrange the remaining elements of B arbitrarily.

How many distinct sequences B can result? Find the count modulo 998244353.

Constraints

  • 1 \le N \le 2.5 \times 10^5
  • 1 \le Q \le 2.5 \times 10^5
  • 1 \le A_i \le N
  • 1 \le L \le R \le N
  • 1 \le X \le N
  • All input values are integers.

Input

The input is given from Standard Input in the following format, where \mathrm{query}_i denotes the i‑th query:

N Q
A_1 A_2 \dots A_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

Each query is given in the following format:

L R X

Output

Output Q lines. The i-th line should contain the answer to the i‑th query.


Sample Input 1

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

Sample Output 1

3
1
6

For the first query, there are three possible resulting sequences B: (1,1,2), (1,2,1), and (2,1,1).
For the second query, there is one possible resulting sequence B: the empty sequence ().


Sample Input 2

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

Sample Output 2

1
120
6
3
1
2