A - chmin

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 100 点

問題文

長さ N の整数列 A=(A_1,A_2,\dots,A_N) と整数 X が与えられます。
i=1,2,\dots,N の順に以下を行ってください。

  • もし A_i<X なら、 X=A_i に更新した上で 1 を出力する。
  • そうでないなら 0 を出力する。

制約

  • 入力は全て整数
  • 1 \le N,X,A_i \le 100

入力

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

N X
A_1 A_2 \dots A_N

出力

N 行出力せよ。
そのうち k 行目には、 i=k についての出力をせよ。


入力例 1

5 10
6 4 7 1 3

出力例 1

1
1
0
1
0
  • 最初、 X=10 です。
  • i=1 について、 A_1=6<X=10 なので、 X=6 に更新した上で 1 を出力します。
  • i=2 について、 A_2=4<X=6 なので、 X=4 に更新した上で 1 を出力します。
  • i=3 について、 A_3=7 \ge X=4 なので、 0 を出力します。
  • i=4 について、 A_4=1<X=4 なので、 X=1 に更新した上で 1 を出力します。
  • i=5 について、 A_5=3 \ge X=1 なので、 0 を出力します。

入力例 2

1 1
1

出力例 2

0

入力例 3

8 20
9 19 14 17 17 4 18 4

出力例 3

1
0
0
0
0
1
0
0

Score : 100 points

Problem Statement

You are given a length-N integer sequence A=(A_1,A_2,\dots,A_N) and an integer X.
For i=1,2,\dots,N in this order, do the following.

  • If A_i<X, update X=A_i and output 1.
  • Otherwise, output 0.

Constraints

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

Input

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

N X
A_1 A_2 \dots A_N

Output

Output N lines.
The k-th line should contain the output for i=k.


Sample Input 1

5 10
6 4 7 1 3

Sample Output 1

1
1
0
1
0
  • Initially, X=10.
  • For i=1: since A_1=6<X=10, update X=6 and output 1.
  • For i=2: since A_2=4<X=6, update X=4 and output 1.
  • For i=3: since A_3=7 \ge X=4, output 0.
  • For i=4: since A_4=1<X=4, update X=1 and output 1.
  • For i=5: since A_5=3 \ge X=1, output 0.

Sample Input 2

1 1
1

Sample Output 2

0

Sample Input 3

8 20
9 19 14 17 17 4 18 4

Sample Output 3

1
0
0
0
0
1
0
0
B - Tires

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 100 点

問題文

末尾が er または ist であるような文字列 S が与えられます。
S の末尾が er である場合は er を、 ist である場合は ist を出力してください。

制約

  • 2 \le |S| \le 20
  • S は英小文字のみからなる。
  • S の末尾は er または ist である。

入力

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

S

出力

答えを出力せよ。


入力例 1

atcoder

出力例 1

er

S="atcoder" の末尾は er です。


入力例 2

tourist

出力例 2

ist

入力例 3

er

出力例 3

er

Score : 100 points

Problem Statement

You are given a string S ending with er or ist.
If S ends with er, print er; if it ends with ist, print ist.

Constraints

  • 2 \le |S| \le 20
  • S consists of lowercase English letters.
  • S ends with er or ist.

Input

Input is given from Standard Input in the following format:

S

Output

Print the answer.


Sample Input 1

atcoder

Sample Output 1

er

S="atcoder" ends with er.


Sample Input 2

tourist

Sample Output 2

ist

Sample Input 3

er

Sample Output 3

er
C - LOOKUP

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 200 点

問題文

英小文字からなる文字列 S,T が与えられるので、 T が S の(連続する)部分文字列かどうか判定してください。

なお、文字列 X に以下の操作を 0 回以上施して文字列 Y が得られる時、またその時に限り Y は X の(連続する)部分文字列であると言います。

  • 以下の 2 つの操作から 1 つを選択して、その操作を行う。
    • X の先頭の 1 文字を削除する。
    • X の末尾の 1 文字を削除する。

例えば tag は voltage の(連続する)部分文字列ですが、 ace は atcoder の(連続する)部分文字列ではありません。

制約

  • S,T は英小文字からなる
  • 1 \le |S|,|T| \le 100 ( |X| は文字列 X の長さ )

入力

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

S
T

出力

T が S の(連続する)部分文字列なら Yes 、そうでないなら No と出力せよ。


入力例 1

voltage
tag

出力例 1

Yes

tag は voltage の(連続する)部分文字列です。


入力例 2

atcoder
ace

出力例 2

No

ace は atcoder の(連続する)部分文字列ではありません。


入力例 3

gorilla
gorillagorillagorilla

出力例 3

No

入力例 4

toyotasystems
toyotasystems

出力例 4

Yes

S=T である場合もあります。

Score : 200 points

Problem Statement

You are given strings S and T consisting of lowercase English letters. Determine whether T is a (contiguous) substring of S.

A string Y is said to be a (contiguous) substring of X if and only if Y can be obtained by performing the operation below on X zero or more times.

  • Do one of the following.
    • Delete the first character in X.
    • Delete the last character in X.

For instance, tag is a (contiguous) substring of voltage, while ace is not a (contiguous) substring of atcoder.

Constraints

  • S and T consist of lowercase English letters.
  • 1 \le |S|,|T| \le 100 (|X| denotes the length of a string X.)

Input

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

S
T

Output

If T is a (contiguous) substring of S, print Yes; otherwise, print No.


Sample Input 1

voltage
tag

Sample Output 1

Yes

tag is a (contiguous) substring of voltage.


Sample Input 2

atcoder
ace

Sample Output 2

No

ace is not a (contiguous) substring of atcoder.


Sample Input 3

gorilla
gorillagorillagorilla

Sample Output 3

No

Sample Input 4

toyotasystems
toyotasystems

Sample Output 4

Yes

It is possible that S=T.

D - MissingNo.

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 200 点

問題文

ナオヒロ君は N+1 個の連続する整数を 1 個ずつ持っていましたが、そのうち 1 個をなくしてしまいました。

残っている N 個の整数が順不同で A_1,\ldots,A_N として与えられるので、なくした整数を求めてください。

なお、なくした整数が一意に定まるような入力のみが与えられます。

制約

  • 2 \leq N \leq 100
  • 1 \leq A_i \leq 1000
  • 入力は全て整数である
  • なくした整数は一意に定まる

入力

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

N
A_1 A_2 \ldots A_N

出力

答えを出力せよ。


入力例 1

3
2 3 5

出力例 1

4

ナオヒロ君は初め 2,3,4,5 の 4 個の整数を持っており、4 をなくし、2,3,5 が残っていました。

なくした整数である 4 を出力します。


入力例 2

8
3 1 4 5 9 2 6 8

出力例 2

7

入力例 3

16
152 153 154 147 148 149 158 159 160 155 156 157 144 145 146 150

出力例 3

151

Score : 200 points

Problem Statement

Naohiro had N+1 consecutive integers, one of each, but he lost one of them.

The remaining N integers are given in arbitrary order as A_1,\ldots,A_N. Find the lost integer.

The given input guarantees that the lost integer is uniquely determined.

Constraints

  • 2 \leq N \leq 100
  • 1 \leq A_i \leq 1000
  • All input values are integers.
  • The lost integer is uniquely determined.

Input

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

N
A_1 A_2 \ldots A_N

Output

Print the answer.


Sample Input 1

3
2 3 5

Sample Output 1

4

Naohiro originally had four integers, 2,3,4,5, then lost 4, and now has 2,3,5.

Print the lost integer, 4.


Sample Input 2

8
3 1 4 5 9 2 6 8

Sample Output 2

7

Sample Input 3

16
152 153 154 147 148 149 158 159 160 155 156 157 144 145 146 150

Sample Output 3

151
E - Sum of Min Query

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 300 点

問題文

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

Q 個のクエリが与えられるので順に処理してください。 i 番目 (1\le i\le Q) のクエリは以下で説明されます。

文字 c_i と整数 X_i,V_i が与えられる。 c_i= A ならば A_{X_i} を、 c_i= B ならば B_{X_i} を V_i に変更する。その後、 \displaystyle \sum_{k=1}^N \min(A_k,B_k) を出力する。

制約

  • 1\le N,Q \le 2 \times 10^5
  • 1\le A_i,B_i \le 10^9
  • c_i は A か B のいずれか
  • 1\le X_i \le N
  • 1\le V_i \le 10^9
  • 入力される数値は全て整数

入力

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

N Q
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_N
c_1 X_1 V_1
c_2 X_2 V_2
\vdots
c_Q X_Q V_Q

出力

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


入力例 1

4 3
3 1 4 1
2 7 1 8
A 2 3
B 3 3
A 1 7

出力例 1

7
9
9

1 番目のクエリでは A=(3,3,4,1),B=(2,7,1,8) となります。したがって、1 行目には \displaystyle \min(3,2)+\min(3,7) + \min(4 , 1)+\min(1,8) = 7 を出力してください。

2 番目のクエリでは A=(3,3,4,1),B=(2,7,3,8) となります。したがって、 2 行目には \displaystyle \min(3,2)+\min(3,7) + \min(4 , 3)+\min(1,8) = 9 を出力してください。

3 番目のクエリでは A=(7,3,4,1),B=(2,7,3,8) となります。したがって、 3 行目には \displaystyle \min(7,2)+\min(3,7) + \min(4 , 3)+\min(1,8) = 9 を出力してください。


入力例 2

1 3
1
1000000000
A 1 1
A 1 1
A 1 1

出力例 2

1
1
1

入力例 3

5 3
100 100 100 100 100
100 100 100 100 100
A 4 21
A 2 99
B 4 57

出力例 3

421
420
420

Score : 300 points

Problem Statement

You are given length-N integer sequences: A=(A_1,A_2,\ldots,A_N) and B=(B_1,B_2,\ldots,B_N).

You are given Q queries to process in order. The i-th query (1\le i\le Q) is described as follows:

You are given a character c_i and integers X_i,V_i. If c_i= A, change A_{X_i} to V_i; if c_i= B, change B_{X_i} to V_i. Then, output \displaystyle \sum_{k=1}^N \min(A_k,B_k).

Constraints

  • 1\le N,Q \le 2 \times 10^5
  • 1\le A_i,B_i \le 10^9
  • c_i is either A or B.
  • 1\le X_i \le N
  • 1\le V_i \le 10^9
  • All input values are integers.

Input

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

N Q
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_N
c_1 X_1 V_1
c_2 X_2 V_2
\vdots
c_Q X_Q V_Q

Output

Output Q lines. The i-th line (1\le i\le Q) should contain the answer to the i-th query.


Sample Input 1

4 3
3 1 4 1
2 7 1 8
A 2 3
B 3 3
A 1 7

Sample Output 1

7
9
9

After the 1st query, A=(3,3,4,1),B=(2,7,1,8). Therefore, output \displaystyle \min(3,2)+\min(3,7) + \min(4 , 1)+\min(1,8) = 7 on the 1st line.

After the 2nd query, A=(3,3,4,1),B=(2,7,3,8). Therefore, output \displaystyle \min(3,2)+\min(3,7) + \min(4 , 3)+\min(1,8) = 9 on the 2nd line.

After the 3rd query, A=(7,3,4,1),B=(2,7,3,8). Therefore, output \displaystyle \min(7,2)+\min(3,7) + \min(4 , 3)+\min(1,8) = 9 on the 3rd line.


Sample Input 2

1 3
1
1000000000
A 1 1
A 1 1
A 1 1

Sample Output 2

1
1
1

Sample Input 3

5 3
100 100 100 100 100
100 100 100 100 100
A 4 21
A 2 99
B 4 57

Sample Output 3

421
420
420
F - Security 2

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 300 点

問題文

AtCoder 社の入口には特殊なパスコード入力装置があり、 この装置は文字列を 1 つ表示する画面と 2 つのボタンからなります。

画面に表示される文字列を t とします。 t ははじめ空文字列であり、ボタンを押すことで t に以下の変化が起きます。

  • ボタン A を押すと、t の末尾に 0 が追加される。
  • ボタン B を押すと、t に含まれるすべての数字が、それぞれ次の数字に置き換わる。ここで、0 から 8 までの数字については次の数字は 1 大きな数を表す数字とし、9 の次の数字は 0 とする。

たとえば、t が 1984 のときにボタン A を押すと t は 19840 に変化し、さらにボタン B を押すと t は 20951 に変化します。

文字列 S が与えられます。t が空文字列である状態からボタンを 0 回以上押して t を S に一致させるとき、ボタンを最少で何回押す必要があるかを求めてください。

制約

  • S は 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 からなる文字列
  • 1 \leq |S| \leq 5 \times 10^5 (|S| は S の長さをあらわす)

入力

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

S

出力

答えを出力せよ。


入力例 1

21

出力例 1

4

以下の順にボタンを押せば、t が 21 になります。

  1. ボタン A を押す。t は 0 になる。
  2. ボタン B を押す。t は 1 になる。
  3. ボタン A を押す。t は 10 になる。
  4. ボタン B を押す。t は 21 になる。

4 回より少ない回数ボタンを押して t を 21 にすることはできないので、4 を出力します。


入力例 2

407

出力例 2

17

入力例 3

2025524202552420255242025524

出力例 3

150

Score : 300 points

Problem Statement

At the entrance of AtCoder Inc., there is a special pass-code input device. The device consists of a screen displaying one string, and two buttons.

Let t be the string shown on the screen. Initially, t is the empty string. Pressing a button changes t as follows:

  • Pressing button A appends 0 to the end of t.
  • Pressing button B replaces every digit in t with the next digit: for digits 0 through 8 the next digit is the one whose value is larger by 1; the next digit after 9 is 0.

For example, if t is 1984 and button A is pressed, t becomes 19840; if button B is then pressed, t becomes 20951.

You are given a string S. Starting from the empty string, press the buttons zero or more times until t coincides with S. Find the minimum number of button presses required.

Constraints

  • S is a string consisting of 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9.
  • 1 \le |S| \le 5 \times 10^{5}, where |S| is the length of S.

Input

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

S

Output

Output the answer.


Sample Input 1

21

Sample Output 1

4

The following sequence of presses makes t equal to 21.

  1. Press button A. t becomes 0.
  2. Press button B. t becomes 1.
  3. Press button A. t becomes 10.
  4. Press button B. t becomes 21.

It is impossible to obtain 21 with fewer than four presses, so output 4.


Sample Input 2

407

Sample Output 2

17

Sample Input 3

2025524202552420255242025524

Sample Output 3

150
G - Teleport Maze

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 400 点

問題文

H 行 W 列のマス目からなる迷路があります。 上から i 行目、左から j 列目のマスを (i,j) と表記します。 マス (i,j) がどのようなマスであるかは文字 S_{i,j} として与えられ、各文字の意味は以下の通りです。

  • . : 空きマス
  • # : 障害物マス
  • 英小文字(a - z): ワープマス

迷路内では、以下の二種類の行動を好きな順序で何回でも行うことができます。

  • 歩行:現在いるマスから上下左右の四方向のいずれかに 1 マス分進んだマスへ移動する。ただし、障害物マスへ移動することや、マス目の外に移動することはできない。
  • ワープ:ワープマスにいるとき、そのワープマスと同じ文字が書かれたワープマスのうちいずれか好きなマスへと移動する。

マス (1,1) からマス (H,W) へ移動することが可能かどうか判定し、可能ならばそれに必要な最小の合計行動回数を求めてください。

制約

  • 1\leq H,W \leq 1000
  • H\times W \geq 2
  • H,W は整数
  • S_{i,j} は ., #, または英小文字のいずれか
  • S_{1,1}\neq #
  • S_{H,W}\neq #

入力

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

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

出力

マス (1,1) からマス (H,W) へ移動することが可能ならばそれに必要な最小の合計行動回数を、不可能ならば -1 を出力せよ。


入力例 1

3 4
..a.
####
ba#b

出力例 1

5

以下のように行動することで、マス (1,1) からマス (3,4) へ移動することができます。

  1. マス (1,1) からマス (1,2) へ歩行によって移動する。
  2. マス (1,2) からマス (1,3) へ歩行によって移動する。
  3. マス (1,3) からマス (3,2) へワープによって移動する。
  4. マス (3,2) からマス (3,1) へ歩行によって移動する。
  5. マス (3,1) からマス (3,4) へワープによって移動する。

このときの合計行動回数は 5 回であり、これが最小です。


入力例 2

3 4
..a.
####
b.#b

出力例 2

-1

マス (1,1) からマス (3,4) へ移動することは不可能です。


入力例 3

4 4
xxxx
xxxx
xxxx
xxxx

出力例 3

1

入力例 4

7 11
u..#y..#...
k..#.z.#.k.
iju#...#x..
###########
..x#.t.#..n
abc#y..#...
..z#..t#.y.

出力例 4

12

Score : 400 points

Problem Statement

There is a maze consisting of a grid with H rows and W columns. Let (i,j) denote the cell at the i-th row from the top and j-th column from the left. The type of cell (i,j) is given as a character S_{i,j}, where each character has the following meaning:

  • . : Empty cell
  • # : Obstacle cell
  • Lowercase English letter (a - z): Warp cell

In the maze, you can perform the following two types of actions any number of times in any order:

  • Walk: Move from the current cell to a cell that is one cell away in one of the four directions (up, down, left, right). However, you cannot move to an obstacle cell or outside the grid.
  • Warp: When you are at a warp cell, move to any warp cell with the same character written on it.

Determine whether it is possible to move from cell (1,1) to cell (H,W), and if possible, find the minimum total number of actions required.

Constraints

  • 1\leq H,W \leq 1000
  • H\times W \geq 2
  • H and W are integers.
  • S_{i,j} is ., #, or a lowercase English letter.
  • S_{1,1}\neq #
  • S_{H,W}\neq #

Input

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

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

Output

If it is possible to move from cell (1,1) to cell (H,W), print the minimum total number of actions required; otherwise, print -1.


Sample Input 1

3 4
..a.
####
ba#b

Sample Output 1

5

You can move from cell (1,1) to cell (3,4) by performing actions as follows:

  1. Move from cell (1,1) to cell (1,2) by walking.
  2. Move from cell (1,2) to cell (1,3) by walking.
  3. Move from cell (1,3) to cell (3,2) by warping.
  4. Move from cell (3,2) to cell (3,1) by walking.
  5. Move from cell (3,1) to cell (3,4) by warping.

The total number of actions is 5, which is the minimum.


Sample Input 2

3 4
..a.
####
b.#b

Sample Output 2

-1

It is impossible to move from cell (1,1) to cell (3,4).


Sample Input 3

4 4
xxxx
xxxx
xxxx
xxxx

Sample Output 3

1

Sample Input 4

7 11
u..#y..#...
k..#.z.#.k.
iju#...#x..
###########
..x#.t.#..n
abc#y..#...
..z#..t#.y.

Sample Output 4

12
H - Revenge of "The Salary of AtCoder Inc."

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 450 点

問題文

AtCoder社の社員である青木さんの今月の給料は、整数 N と長さ N の数列 A を用いて以下のように決められます。
まず、青木さんに 1 から N までの整数が等確率で出る N 面ダイスと変数 x=0 を渡します。

その後、以下の手順を終了まで繰り返します。

  • ダイスを 1 度振り、出た目を y とする。
    • もし x<y なら A_y 円支給し、 x=y と更新する。
    • そうでないなら終了する。

青木さんの今月の給料は、この手順によって支給された金額の合計です。
青木さんの今月の給料の期待値を {}\bmod{998244353} で求めてください。

期待値 {}\bmod{998244353} の定義 この問題で求める期待値は必ず有理数になることが証明できます。 また、この問題の制約下では、求める期待値を既約分数 \frac yx で表したときに x が 998244353 で割り切れないことが保証されます。 このとき、y\equiv xz\pmod{998244353} を満たす 0\leq z\lt998244353 がただ一つ存在するので、z を出力してください。

制約

  • 入力は全て整数
  • 1 \le N \le 3 \times 10^5
  • 0 \le A_i < 998244353

入力

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

N
A_1 A_2 \dots A_N

出力

答えを出力せよ。


入力例 1

3
3 2 6

出力例 1

776412280

手順の一例は、以下の通りです。

  • 最初、 x=0 である。
  • ダイスを 1 度振り、出た目が 1 であった。 0<1 であるため、 A_1 = 3 円支給し、 x=1 とする。
  • ダイスを 1 度振り、出た目が 3 であった。 1<3 であるため、 A_3 = 6 円支給し、 x=3 とする。
  • ダイスを 1 度振り、出た目が 1 であった。 3 \ge 1 であるため、終了する。

この例では、青木さんの今月の給料は 9 円です。

なお、青木さんの今月の給料の期待値は \frac{49}{9} 円と求めることができ、これを {}\bmod{998244353} 上で表現すると 776412280 となります。


入力例 2

1
998244352

出力例 2

998244352

入力例 3

9
3 14 159 2653 58979 323846 2643383 27950288 419716939

出力例 3

545252774

Score : 450 points

Problem Statement

Aoki, an employee at AtCoder Inc., has his salary for this month determined by an integer N and a sequence A of length N as follows.
First, he is given an N-sided die (dice) that shows the integers from 1 to N with equal probability, and a variable x=0.

Then, the following steps are repeated until terminated.

  • Roll the die once and let y be the result.
    • If x<y, pay him A_y yen and let x=y.
    • Otherwise, terminate the process.

Aoki's salary for this month is the total amount paid through this process.
Find the expected value of Aoki's salary this month, modulo 998244353.

How to find an expected value modulo 998244353 It can be proved that the sought expected value in this problem is always a rational number. Also, the constraints of this problem guarantee that if the sought expected value is expressed as a reduced fraction \frac yx, then x is not divisible by 998244353. Here, there is exactly one 0\leq z\lt998244353 such that y\equiv xz\pmod{998244353}. Print this z.

Constraints

  • All inputs are integers.
  • 1 \le N \le 3 \times 10^5
  • 0 \le A_i < 998244353

Input

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

N
A_1 A_2 \dots A_N

Output

Print the answer.


Sample Input 1

3
3 2 6

Sample Output 1

776412280

Here is an example of how the process goes.

  • Initially, x=0.
  • Roll the die once, and it shows 1. Since 0<1, pay him A_1 = 3 yen and let x=1.
  • Roll the die once, and it shows 3. Since 1<3, pay him A_3 = 6 yen and let x=3.
  • Roll the die once, and it shows 1. Since 3 \ge 1, terminate the process.

In this case, his salary for this month is 9 yen.

It can be calculated that the expected value of his salary this month is \frac{49}{9} yen, whose representation modulo 998244353 is 776412280.


Sample Input 2

1
998244352

Sample Output 2

998244352

Sample Input 3

9
3 14 159 2653 58979 323846 2643383 27950288 419716939

Sample Output 3

545252774
I - Chord Crossing

実行時間制限: 3 sec / メモリ制限: 1024 MiB

配点 : 525 点

問題文

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

これらの点を結ぶ M 本の線分 1,2,\dots,M が存在し、線分 i は点 A_i と点 B_i を結んでいます。 ここで、A_i と B_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 を結ぶ線分と共有点を持つものは線分 1 の 1 本です。
  • 2 番目のクエリについて、最初に与えられた M 本の線分のうち、点 3 と点 7 を結ぶ線分と共有点を持つものは線分 1,2 の 2 本です。
  • 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