A - Not Overflow

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

配点 : 100

問題文

整数 N が与えられます。 N-2^{31} 以上かつ 2^{31} 未満ならば Yes を、そうでないならば No を出力してください。

制約

  • -2^{63} \leq N < 2^{63}
  • N は整数である。

入力

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

N

出力

N-2^{31} 以上かつ 2^{31} 未満ならば Yes を、そうでないならば No を出力せよ。


入力例 1

10

出力例 1

Yes

10-2^{31} 以上かつ 2^{31} 未満であるので、Yes を出力します。


入力例 2

-9876543210

出力例 2

No

-9876543210-2^{31} 未満であるので、No を出力します。


入力例 3

483597848400000

出力例 3

No

4835978484000002^{31} 以上であるので、No を出力します。

Score : 100 points

Problem Statement

You are given an integer N. If N is between -2^{31} and 2^{31}-1 (inclusive), print Yes; otherwise, print No.

Constraints

  • -2^{63} \leq N < 2^{63}
  • N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

If N is between -2^{31} and 2^{31}-1 (inclusive), print Yes; otherwise, print No.


Sample Input 1

10

Sample Output 1

Yes

10 is between -2^{31} and 2^{31}-1, so Yes should be printed.


Sample Input 2

-9876543210

Sample Output 2

No

-9876543210 is less than -2^{31}, so No should be printed.


Sample Input 3

483597848400000

Sample Output 3

No

483597848400000 is greater than 2^{31}-1, so No should be printed.

B - 2UP3DOWN

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

配点 : 100

問題文

高橋君が 100 階建てのビルにいます。

高橋君は 2 階分までの上り、または、3 階分までの下りであれば移動には階段を使い、そうでないときエレベーターを使います。

高橋君が X 階から Y 階への移動に使うのは階段ですか?

制約

  • 1 \leq X,Y \leq 100
  • X \neq Y
  • 入力は全ては整数である

入力

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

X Y

出力

移動に使うのが階段ならば Yes、エレベーターならば No を出力せよ。


入力例 1

1 4

出力例 1

No

1 階から 4 階への移動は 3 階分の上りなのでエレベーターを使います。


入力例 2

99 96

出力例 2

Yes

99 階から 96 階への移動は 3 階分の下りなので階段を使います。


入力例 3

100 1

出力例 3

No

Score : 100 points

Problem Statement

Takahashi is in a building with 100 floors.

He uses the stairs for moving up two floors or less or moving down three floors or less, and uses the elevator otherwise.

Does he use the stairs to move from floor X to floor Y?

Constraints

  • 1 \leq X,Y \leq 100
  • X \neq Y
  • All input values are integers.

Input

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

X Y

Output

If Takahashi uses the stairs for the move, print Yes; if he uses the elevator, print No.


Sample Input 1

1 4

Sample Output 1

No

The move from floor 1 to floor 4 involves going up three floors, so Takahashi uses the elevator.


Sample Input 2

99 96

Sample Output 2

Yes

The move from floor 99 to floor 96 involves going down three floors, so Takahashi uses the stairs.


Sample Input 3

100 1

Sample Output 3

No
C - カップリング選抜

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

配点 : 300

問題文

アイドルグループ Bit♡Beat では、新曲のカップリングとしてメンバーの中から 2 人のユニット(選抜)を組むことになりました。

グループには N 人のメンバーがおり、それぞれのメンバーにスキル値が定まっています。 i 人目のメンバー (1\le i\le N) のスキル値は A_i です。

プロデューサーであるあなたは、スキル値の合計が ちょうど S になるような 2 人のメンバーの選び方を探しています。

スキル値の合計がちょうど S になるような 2 人のメンバーの選び方が何通りあるか求めてください。

制約

  • 2\le N\le 5\times 10^5
  • 1\le S \le 10^9
  • 1\le A_i\le 10^9
  • 入力される値は全て整数

入力

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

N S
A_1 A_2 \ldots A_N

出力

スキル値の合計がちょうど S になるような 2 人のメンバーの選び方が何通りあるか出力せよ。


入力例 1

5 11
5 6 9 2 2

出力例 1

3

1 人目と 2 人目がユニットを組むと、スキル値の合計は 5+6=11 となります。

このほかにも、 3 人目と 4 人目、3 人目と 5 人目がユニットを組んでも条件を満たします。

条件を満たす 2 人のメンバーの選び方は 3 通りであるため、 3 を出力してください。


入力例 2

10 1000000000
1 1 1 1 1 1 1 1 1 1

出力例 2

0

条件を満たす 2 人のメンバーの選び方が存在しない場合もあります。


入力例 3

6 14
7 7 7 7 7 7

出力例 3

15

入力例 4

15 8
3 4 10 9 1 1 1 7 8 9 9 3 8 9 7

出力例 4

6
D - Batters

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

配点 : 200

問題文

高橋君は野球をモチーフにしたゲームを作ろうとしましたが、うまくコードが書けなくて困っています。
高橋君の代わりに次の問題を解くプログラムを作ってください。

マス 0, マス 1, マス 2, マス 34 つのマス目があります。はじめマスの上には何もありません。
また、整数 P があり、はじめ P = 0 です。
正の整数からなる数列 A = (A_1, A_2, \dots, A_N) が与えられるので、i = 1, 2, \dots, N について順番に次の操作を行います。

  1. マス 0 に駒を 1 個置く。
  2. マス上のすべての駒を番号が A_i 大きいマスに進める。言い換えると、駒がマス x にあればその駒をマス x + A_i に移動する。
    ただし移動先のマスが存在しない (すなわち x + A_i4 以上になる) 駒たちに関しては、それらを取り除いて P に取り除いた個数を加算する。

すべての操作を行った後の P の値を出力してください。

制約

  • 1 \leq N \leq 100
  • 1 \leq A_i \leq 4
  • 入力される値はすべて整数

入力

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

N
A_1 A_2 \dots A_N

出力

操作終了時点での P の値を出力せよ。


入力例 1

4
1 1 3 2

出力例 1

3

操作を説明すると次のようになり、操作終了時点での P の値は 3 になります。

  • i=1 での操作
    1. マス 0 に駒を置く。この時点でマス 0 にコマが乗っている。
    2. すべての駒を 1 大きいマスに進める。移動を終えた時点でマス 1 に駒が乗っている。
  • i=2 での操作
    1. マス 0 に駒を置く。この時点でマス 0, 1 にコマが乗っている。
    2. すべての駒を 1 大きいマスに進める。移動を終えた時点でマス 1, 2 に駒が乗っている。
  • i=3 での操作
    1. マス 0 に駒を置く。この時点でマス 0, 1, 2 にコマが乗っている。
    2. すべての駒を 3 大きいマスに進める。
      この時、マス 1,2 にある駒は移動先のマスが存在しないため (それぞれ 1+3=4,2+3=5 なので) 、盤上から取り除いて P2 を加算する。P の値は 2 になる。
      移動を終えた時点でマス 3 に駒が乗っている。
  • i=4 での操作
    1. マス 0 に駒を置く。この時点でマス 0, 3 にコマが乗っている。
    2. すべての駒を 2 大きいマスに進める。
      この時、マス 3 にある駒は移動先のマスが存在しないため (3+2=5 なので) 、盤上から取り除いて P1 を加算する。P の値は 3 になる。
      移動を終えた時点でマス 2 に駒が乗っている。

入力例 2

3
1 1 1

出力例 2

0

P の値が操作中に変化しない場合もあります。


入力例 3

10
2 2 4 1 1 1 4 2 2 1

出力例 3

8

Score : 200 points

Problem Statement

Takahashi is trying to create a game inspired by baseball, but he is having difficulty writing the code.
Write a program for Takahashi that solves the following problem.

There are 4 squares called Square 0, Square 1, Square 2, and Square 3. Initially, all squares are empty.
There is also an integer P; initially, P = 0.
Given a sequence of positive integers A = (A_1, A_2, \dots, A_N), perform the following operations for i = 1, 2, \dots, N in this order:

  1. Put a piece on Square 0.
  2. Advance every piece on the squares A_i squares ahead. In other words, if Square x has a piece, move the piece to Square (x + A_i).
    If, however, the destination square does not exist (i.e. x + A_i is greater than or equal to 4) for a piece, remove it. Add to P the number of pieces that have been removed.

Print the value of P after all the operations have been performed.

Constraints

  • 1 \leq N \leq 100
  • 1 \leq A_i \leq 4
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \dots A_N

Output

Print the value of P after all the operations have been performed.


Sample Input 1

4
1 1 3 2

Sample Output 1

3

The operations are described below. After all the operations have been performed, P equals 3.

  • The operations for i=1:
    1. Put a piece on Square 0. Now, Square 0 has a piece.
    2. Advance every piece on the squares 1 square ahead. After these moves, Square 1 has a piece.
  • The operations for i=2:
    1. Put a piece on Square 0. Now, Squares 0 and 1 have a piece.
    2. Advance every piece on the squares 1 square ahead. After these moves, Squares 1 and 2 have a piece.
  • The operations for i=3:
    1. Put a piece on Square 0. Now, Squares 0, 1, and 2 have a piece.
    2. Advance every piece on the squares 3 squares ahead.
      Here, for the pieces on Squares 1 and 2, the destination squares do not exist (since 1+3=4 and 2+3=5), so remove these pieces and add 2 to P. P now equals 2. After these moves, Square 3 has a piece.
  • The operations for i=4:
    1. Put a piece on Square 0. Now, Squares 0 and 3 have a piece.
    2. Advance every piece on the squares 2 squares ahead.
      Here, for the piece on Square 3, the destination square does not exist (since 3+2=5), so remove this piece and add 1 to P. P now equals 3.
      After these moves, Square 2 has a piece.

Sample Input 2

3
1 1 1

Sample Output 2

0

The value of P may not be updated by the operations.


Sample Input 3

10
2 2 4 1 1 1 4 2 2 1

Sample Output 3

8
E - Make Them Narrow

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

配点 : 250

問題文

長さ N の数列 A が与えられます。
A のうち丁度 K 要素を自由に選んで消し、残った要素を順序を保って連結した数列を B とします。
( B の最大値 ) - ( B の最小値 ) としてありうる最小値を求めてください。

制約

  • 入力は全て整数
  • 1 \le K < N \le 2 \times 10^5
  • 1 \le A_i \le 10^9

入力

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

N K
A_1 A_2 \dots A_N

出力

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


入力例 1

5 2
3 1 5 4 9

出力例 1

2

A=(3,1,5,4,9) から丁度 2 要素を自由に選んで消すことを考えます。

  • 例えば 2 要素目の 15 要素目の 9 を消すと、消した後の数列 B=(3,5,4) となります。
    • このとき B の最大値は 5 、最小値は 3 なので ( B の最大値 ) - ( B の最小値 ) =2 となり、これは達成可能な最小値です。

入力例 2

6 5
1 1 1 1 1 1

出力例 2

0

入力例 3

8 3
31 43 26 6 18 36 22 13

出力例 3

18

Score : 250 points

Problem Statement

You are given a sequence A of length N.
Freely choose exactly K elements from A and remove them, then concatenate the remaining elements in their original order to form a new sequence B.
Find the minimum possible value of this: the maximum value of B minus the minimum value of B.

Constraints

  • All inputs are integers.
  • 1 \le K < N \le 2 \times 10^5
  • 1 \le A_i \le 10^9

Input

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

N K
A_1 A_2 \dots A_N

Output

Print the answer as an integer.


Sample Input 1

5 2
3 1 5 4 9

Sample Output 1

2

Consider removing exactly two elements from A=(3,1,5,4,9).

  • For example, if you remove the 2nd element 1 and the 5th element 9, the resulting sequence is B=(3,5,4).
    • In this case, the maximum value of B is 5 and the minimum value is 3, so (maximum value of B) - (minimum value of B) =2, which is the minimum possible value.

Sample Input 2

6 5
1 1 1 1 1 1

Sample Output 2

0

Sample Input 3

8 3
31 43 26 6 18 36 22 13

Sample Output 3

18
F - Lock All Doors

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

配点 : 300

問題文

N + 1 個の部屋が一列に並んでおり、順に 0, 1, \ldots, N の番号が付けられています。

部屋の間には N 個のドアがあり、1, 2, \ldots, N の番号が付けられています。ドア i は部屋 i - 1 と部屋 i の間にあります。

各ドアについて鍵の状態を表す値 L_i が与えられ、L_i = 0 のときドア i の鍵は開いており、L_i = 1 のときドア i の鍵は閉まっています。

高橋君ははじめ部屋 R におり、ドア i の鍵が開いているときに限り、部屋 i - 1 と部屋 i の間を移動することができます。また、高橋君は部屋 i - 1 または部屋 i にいるときに限り、ドア i の鍵に対して 開閉操作 を行うことができます。ドア i の鍵に対して開閉操作を行ったとき、その鍵が開いているときは閉まり、閉まっているときは開きます。

すべてのドアの鍵が閉まった状態にするために行う鍵の開閉操作の回数として考えられる最小値を求めてください。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 0 \leq R \leq N
  • L_i \in \lbrace 0, 1 \rbrace
  • 入力される値はすべて整数

入力

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

N R
L_1 L_2 \ldots L_N

出力

答えを出力せよ。


入力例 1

6 3
1 0 0 1 0 0

出力例 1

6

高橋君は以下のように行動することで 6 回の開閉操作ですべてのドアの鍵が閉まった状態にすることができます。

  • 部屋 2 に移動する。
  • ドア 2 に対して開閉操作を行い、ドア 2 の鍵を閉める。
  • 部屋 3 に移動する。
  • ドア 4 に対して開閉操作を行い、ドア 4 の鍵を開ける。
  • ドア 3 に対して開閉操作を行い、ドア 3 の鍵を閉める。
  • 部屋 4 に移動する。
  • ドア 4 に対して開閉操作を行い、ドア 4 の鍵を閉める。
  • 部屋 5 に移動する。
  • ドア 5 に対して開閉操作を行い、ドア 5 の鍵を閉める。
  • ドア 6 に対して開閉操作を行い、ドア 6 の鍵を閉める。

入力例 2

2 1
0 0

出力例 2

2

入力例 3

8 2
0 1 0 0 1 0 1 1

出力例 3

8

Score : 300 points

Problem Statement

There are N + 1 rooms arranged in a line, numbered 0, 1, \ldots, N in order.

Between the rooms, there are N doors numbered 1, 2, \ldots, N. Door i is between rooms i - 1 and i.

For each door, a value L_i representing the lock state is given. When L_i = 0, door i is unlocked, and when L_i = 1, door i is locked.

Takahashi is initially in room R, and can move between rooms i - 1 and i only when door i is unlocked. Also, he can perform a switching operation on door i only when he is in room i - 1 or room i. When a switching operation is performed on door i, if the door is unlocked, it becomes locked, and if it is locked, it becomes unlocked.

Find the minimum number of switching operations needed to make all doors locked.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 0 \leq R \leq N
  • L_i \in \lbrace 0, 1 \rbrace
  • All input values are integers.

Input

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

N R
L_1 L_2 \ldots L_N

Output

Output the answer.


Sample Input 1

6 3
1 0 0 1 0 0

Sample Output 1

6

Takahashi can make all doors locked with six switching operations by acting as follows:

  • Move to room 2.
  • Perform a switching operation on door 2 to lock door 2.
  • Move to room 3.
  • Perform a switching operation on door 4 to unlock door 4.
  • Perform a switching operation on door 3 to lock door 3.
  • Move to room 4.
  • Perform a switching operation on door 4 to lock door 4.
  • Move to room 5.
  • Perform a switching operation on door 5 to lock door 5.
  • Perform a switching operation on door 6 to lock door 6.

Sample Input 2

2 1
0 0

Sample Output 2

2

Sample Input 3

8 2
0 1 0 0 1 0 1 1

Sample Output 3

8
G - Tiling

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

配点 : 450

問題文

一辺の長さが 1 のマスからなる HW 列のマス目と、N 枚のタイルがあります。
i (1\leq i\leq N) 枚目のタイルは A_i\times B_i の長方形です。
以下の条件をすべてみたすようにタイルをマス目に置くことができるか判定してください。

  • 全てのマスがちょうど 1 枚のタイルで覆われている。
  • 使用されないタイルがあっても良い。
  • 使用するタイルは 回転したり裏返したりして置かれていても良い。ただし、各タイルはマスの線に合わせてマス目からはみ出ることがないように置かれていなければならない。

制約

  • 1\leq N\leq 7
  • 1 \leq H,W \leq 10
  • 1\leq A_i,B_i\leq 10
  • 入力はすべて整数

入力

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

N H W
A_1 B_1
A_2 B_2
\vdots
A_N B_N

出力

問題文中の条件をみたすようにタイルをマス目に置くことができるならば Yes を、そうでないならば No を出力せよ。


入力例 1

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

出力例 1

Yes

2,4,5 枚目のタイルを使用して次のように置くと、マス目の各マスをちょうど 1 枚のタイルで覆うことができます。

よって、Yes を出力します。


入力例 2

1 1 2
2 3

出力例 2

No

マス目からはみ出さないようにタイルを置くことはできません。
よって、No を出力します。


入力例 3

1 2 2
1 1

出力例 3

No

全てのマスを覆うようにタイルを置くことができません。
よって、No を出力します。


入力例 4

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

出力例 4

No

全てのマスはちょうど 1 枚のタイルで覆われている必要があることに注意してください。

Score: 450 points

Problem Statement

There is a grid of H rows and W columns, each cell having a side length of 1, and we have N tiles.
The i-th tile (1\leq i\leq N) is a rectangle of size A_i\times B_i.
Determine whether it is possible to place the tiles on the grid so that all of the following conditions are satisfied:

  • Every cell is covered by exactly one tile.
  • It is fine to have unused tiles.
  • The tiles may be rotated or flipped when placed. However, each tile must be aligned with the edges of the cells without extending outside the grid.

Constraints

  • 1\leq N\leq 7
  • 1 \leq H,W \leq 10
  • 1\leq A_i,B_i\leq 10
  • All input values are integers.

Input

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

N H W
A_1 B_1
A_2 B_2
\ldots
A_N B_N

Output

If it is possible to place the tiles on the grid so that all of the conditions in the problem statement are satisfied, print Yes; otherwise, print No.


Sample Input 1

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

Sample Output 1

Yes

Placing the 2-nd, 4-th, and 5-th tiles as shown below covers every cell of the grid by exactly one tile.

Hence, print Yes.


Sample Input 2

1 1 2
2 3

Sample Output 2

No

It is impossible to place the tile without letting it extend outside the grid.
Hence, print No.


Sample Input 3

1 2 2
1 1

Sample Output 3

No

It is impossible to cover all cells with the tile.
Hence, print No.


Sample Input 4

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

Sample Output 4

No

Note that each cell must be covered by exactly one tile.

H - Stamp

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

配点 : 475

問題文

英大文字からなる長さ N の文字列 S と、英大文字からなる長さ M\ (\leq N) の文字列 T が与えられます。

# のみからなる長さ N の文字列 X があります。 以下の操作を好きな回数行うことで、XS に一致させることができるか判定してください。

  • X の中から連続する M 文字を選び、T で置き換える。

制約

  • 1 \leq N \leq 2\times 10^5
  • 1 \leq M \leq \min(N, 5)
  • S は英大文字からなる長さ N の文字列
  • T は英大文字からなる長さ M の文字列

入力

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

N M
S
T

出力

XS に一致させることができるならば Yes を、できないならば No を出力せよ。


入力例 1

7 3
ABCBABC
ABC

出力例 1

Yes

以下、Xl 文字目から r 文字目までの部分を X[l:r] と表記します。

次のように操作を行うことで、XS に一致させることができます。

  1. X[3:5]T で置き換える。X= ##ABC## になる。 
  2. X[1:3]T で置き換える。X= ABCBC## になる。 
  3. X[5:7]T で置き換える。X= ABCBABC になる。 

入力例 2

7 3
ABBCABC
ABC

出力例 2

No

どのように操作を行っても、XS に一致させることはできません。


入力例 3

12 2
XYXXYXXYYYXY
XY

出力例 3

Yes

Score : 475 points

Problem Statement

You are given two strings: S, which consists of uppercase English letters and has length N, and T, which also consists of uppercase English letters and has length M\ (\leq N).

There is a string X of length N consisting only of the character #. Determine whether it is possible to make X match S by performing the following operation any number of times:

  • Choose M consecutive characters in X and replace them with T.

Constraints

  • 1 \leq N \leq 2\times 10^5
  • 1 \leq M \leq \min(N, 5)
  • S is a string consisting of uppercase English letters with length N.
  • T is a string consisting of uppercase English letters with length M.

Input

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

N M
S
T

Output

Print Yes if it is possible to make X match S; print No otherwise.


Sample Input 1

7 3
ABCBABC
ABC

Sample Output 1

Yes

Below, let X[l:r] denote the part from the l-th through the r-th character of X.

You can make X match S by operating as follows.

  1. Replace X[3:5] with T. X becomes ##ABC##.
  2. Replace X[1:3] with T. X becomes ABCBC##.
  3. Replace X[5:7] with T. X becomes ABCBABC.

Sample Input 2

7 3
ABBCABC
ABC

Sample Output 2

No

No matter how you operate, it is impossible to make X match S.


Sample Input 3

12 2
XYXXYXXYYYXY
XY

Sample Output 3

Yes
I - Buildings 2

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

配点 : 550

問題文

ビル 1, ビル 2, \ldots, ビル NN 棟がこの順で東西に一列に並んでおり、ビル 1 が最も西に、ビル N が最も東に建っています。ビル i\ (1\leq i\leq N) の高さは H_i です。

整数の組 (i,j)\ (1\leq i\lt j\leq N) に対して、以下の条件を満たすとき ビル i からビル j を見ることができます。

  • ビル i とビル j の間にビル j より高いビルが存在しない。すなわち、H_k\gt H_j を満たす整数 k\ (i\lt k\lt j) が存在しない。

Q 個の質問に答えてください。i 番目の質問では整数の組 (l_i,r_i)\ (l_i\lt r_i) が与えられるので、ビル r_i より東にあるビル(ビル r_i+1, ビル r_i+2,\ldots,ビル N )のうちビル l_i とビル r_i の両方から見ることができるものの個数を答えてください。

制約

  • 2\leq N\leq 2\times 10^5
  • 1\leq Q\leq 2\times 10^5
  • 1\leq H_i\leq N
  • H_i\neq H_j\ (i\neq j)
  • 1\leq l_i\lt r_i\leq N
  • 入力は全て整数

入力

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

N Q
H_1 H_2 \ldots H_N
l_1 r_1
l_2 r_2
\vdots
l_Q r_Q

出力

Q 行出力せよ。i\ (1\leq i\leq Q) 行目には i 番目の質問に対する答えを出力せよ。


入力例 1

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

出力例 1

2
0
1
  • 1 つ目の質問について、ビル 2 より東にあるビルのうち ビル 1 とビル 2 の両方から見ることができるものはビル 3,52 つです。
  • 2 つ目の質問について、ビル 5 より東にあるビルは存在しません。
  • 3 つ目の質問について、ビル 4 より東にあるビルのうち、ビル 1,4 の両方から見ることができるビルはビル 51 つです。

入力例 2

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

出力例 2

1
3
1
2
1
0
1
1
0
0

Score : 550 points

Problem Statement

There are N buildings, building 1, building 2, \ldots, building N, arranged in this order in a straight line from west to east. Building 1 is the westernmost, and building N is the easternmost. The height of building i\ (1\leq i\leq N) is H_i.

For a pair of integers (i,j)\ (1\leq i\lt j\leq N), building j can be seen from building i if the following condition is satisfied.

  • There is no building taller than building j between buildings i and j. In other words, there is no integer k\ (i\lt k\lt j) such that H_k > H_j.

You are given Q queries. In the i-th query, given a pair of integers (l_i,r_i)\ (l_i\lt r_i), find the number of buildings to the east of building r_i (that is, buildings r_i + 1, r_i + 2, \ldots, N) that can be seen from both buildings l_i and r_i.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq H_i \leq N
  • H_i\neq H_j\ (i\neq j)
  • 1 \leq l_i < r_i \leq N
  • All input values are integers.

Input

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

N Q
H_1 H_2 \ldots H_N
l_1 r_1
l_2 r_2
\vdots
l_Q r_Q

Output

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


Sample Input 1

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

Sample Output 1

2
0
1
  • For the first query, among the buildings to the east of building 2, buildings 3 and 5 can be seen from both buildings 1 and 2, so the answer is 2.
  • For the second query, there are no buildings to the east of building 5.
  • For the third query, among the buildings to the east of building 4, building 5 can be seen from both buildings 1 and 4, so the answer is 1.

Sample Input 2

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

Sample Output 2

1
3
1
2
1
0
1
1
0
0