E - Count Mountain-Shaped Temperature Patterns Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 433 点

問題文

高橋君は気象データの分析を行っています。ある観測地点で N 日間にわたって記録された日ごとの気温データ S_1, S_2, \ldots, S_N が与えられます。気温は整数値で表されます。

高橋君は、連続するちょうど K 日間の気温データが「山型パターン」を形成しているかどうかに興味があります。

具体的には、開始位置 l(1 \leq l \leq N-K+1)に対して、区間 S_l, S_{l+1}, \ldots, S_{l+K-1} が以下の条件を満たすとき、この区間は山型パターンであるといいます。

  • ある位置 p(l < p < l+K-1)が存在して、

S_l < S_{l+1} < \cdots < S_p > S_{p+1} > \cdots > S_{l+K-1}

が成り立つ。すなわち、区間の左端から位置 p まで気温が狭義単調増加し、位置 p から区間の右端まで気温が狭義単調減少する。

ここで、p は区間の左端 l でも右端 l+K-1 でもない位置であるため、増加部分と減少部分はそれぞれ少なくとも1ステップ存在します。この条件を満たすには K \geq 3 が必要です。

高橋君は Q 個のクエリ K_1, K_2, \ldots, K_Q のそれぞれについて、長さ K_j の連続する区間のうち山型パターンの条件を満たすものがいくつあるかを求めたいと考えています。各 K_j に対する山型パターンの個数を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq N
  • -10^9 \leq S_i \leq 10^9(1 \leq i \leq N)
  • 3 \leq K_j \leq N(1 \leq j \leq Q)
  • K_1, K_2, \ldots, K_Q はすべて異なる
  • 入力はすべて整数

入力

N Q
S_1 S_2 \ldots S_N
K_1
K_2
\vdots
K_Q
  • 1 行目には、日数 N とクエリの数 Q がスペース区切りで与えられる。
  • 2 行目には、各日の気温 S_1, S_2, \ldots, S_N がスペース区切りで与えられる。
  • 3 行目から Q 行にわたり、各クエリの値 K_j が 1 つずつ与えられる。

出力

Q 行出力せよ。j 行目には、クエリ K_j に対する山型パターンの個数を出力せよ。


入力例 1

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

出力例 1

2
2
1

入力例 2

6 2
1 2 3 4 5 6
3
6

出力例 2

0
0

入力例 3

15 5
1 2 4 3 2 1 2 3 5 4 4 3 2 6 1
3
4
5
7
10

出力例 3

3
3
3
0
0

入力例 4

30 8
-5 -3 0 4 7 6 2 -1 0 3 6 9 8 5 1 -2 -2 1 5 4 3 2 10 9 8 7 6 0 -1 -3
3
4
6
8
10
12
15
20

出力例 4

4
7
9
4
0
0
0
0

入力例 5

3 1
-1000000000 0 -1000000000
3

出力例 5

1

Score : 433 pts

Problem Statement

Takahashi is analyzing weather data. He is given daily temperature data S_1, S_2, \ldots, S_N recorded over N days at a certain observation point. Temperatures are represented as integer values.

Takahashi is interested in whether the temperature data over exactly K consecutive days forms a "mountain pattern."

Specifically, for a starting position l (1 \leq l \leq N-K+1), the interval S_l, S_{l+1}, \ldots, S_{l+K-1} is called a mountain pattern if it satisfies the following condition:

  • There exists a position p (l < p < l+K-1) such that

S_l < S_{l+1} < \cdots < S_p > S_{p+1} > \cdots > S_{l+K-1}

holds. That is, the temperature is strictly monotonically increasing from the left end of the interval to position p, and strictly monotonically decreasing from position p to the right end of the interval.

Since p is neither the left end l nor the right end l+K-1 of the interval, both the increasing part and the decreasing part consist of at least one step. This condition requires K \geq 3.

Takahashi wants to determine, for each of Q queries K_1, K_2, \ldots, K_Q, how many consecutive intervals of length K_j satisfy the mountain pattern condition. Find the number of mountain patterns for each K_j.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq N
  • -10^9 \leq S_i \leq 10^9 (1 \leq i \leq N)
  • 3 \leq K_j \leq N (1 \leq j \leq Q)
  • K_1, K_2, \ldots, K_Q are all distinct
  • All input values are integers

Input

N Q
S_1 S_2 \ldots S_N
K_1
K_2
\vdots
K_Q
  • The first line contains the number of days N and the number of queries Q, separated by a space.
  • The second line contains the temperatures for each day S_1, S_2, \ldots, S_N, separated by spaces.
  • The following Q lines each contain a single query value K_j.

Output

Output Q lines. On the j-th line, output the number of mountain patterns for query K_j.


Sample Input 1

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

Sample Output 1

2
2
1

Sample Input 2

6 2
1 2 3 4 5 6
3
6

Sample Output 2

0
0

Sample Input 3

15 5
1 2 4 3 2 1 2 3 5 4 4 3 2 6 1
3
4
5
7
10

Sample Output 3

3
3
3
0
0

Sample Input 4

30 8
-5 -3 0 4 7 6 2 -1 0 3 6 9 8 5 1 -2 -2 1 5 4 3 2 10 9 8 7 6 0 -1 -3
3
4
6
8
10
12
15
20

Sample Output 4

4
7
9
4
0
0
0
0

Sample Input 5

3 1
-1000000000 0 -1000000000
3

Sample Output 5

1