Background
Translated from ROIR 2023 D2T3。
Problem Description
In front of Bob there are n black stones arranged in a row, numbered from 1 to n. On the i-th stone there is an integer ai, and the integers on the n stones form a permutation. We call the adjacent stones of the i-th stone the (i−1)-th and the (i+1)-th stones (if they exist).
Bob performs n operations according to the following steps:
- In the first step, choose any i (1≤i≤n) and paint the i-th stone white.
- In steps 2 to n, among all black stones that have at least one adjacent white stone, choose the stone j with the smallest ai (that is, there may be many black stones whose adjacent stones include at least one white stone, but you must choose the one with the smallest ai), and paint it white.
It is easy to see that after all steps are completed, all n stones will be white.
Alice chose q pairs of values pj and kj. For each pair p and k, she wants to know how many different choices of the first step (i.e., which stone is painted white first) make the p-th stone become white at step k.
Help Bob answer Alice's q queries.
The first line contains two integers n and q, representing the number of stones and the number of queries.
The second line contains n integers a1,a2,…,an, representing the integers written on the stones.
The next q lines each contain a pair of integers pj and kj.
For each query, output one integer, representing the answer to the query.
6 4
1 4 6 5 2 3
3 1
2 2
6 3
4 3
1
2
1
2
5 3
5 2 3 4 1
2 3
4 4
3 2
0
1
1
Hint
In the explanation of the sample below, the bold numbers are the stones that are painted white.
In the first sample:
- If the first step chooses stone 1:
- Step 1: [1,4,6,5,2,3];
- Step 2: [1,4,6,5,2,3];
- Step 3: [1,4,6,5,2,3];
- Step 4: [1,4,6,5,2,3];
- Step 5: [1,4,6,5,2,3];
- Step 6: [1,4,6,5,2,3]。
- If the first step chooses stone 2:
- Step 1: [1,4,6,5,2,3];
- Step 2: [1,4,6,5,2,3];
- Step 3: [1,4,6,5,2,3];
- Step 4: [1,4,6,5,2,3];
- Step 5: [1,4,6,5,2,3];
- Step 6: [1,4,6,5,2,3]。
- If the first step chooses stone 3:
- Step 1: [1,4,6,5,2,3];
- Step 2: [1,4,6,5,2,3];
- Step 3: [1,4,6,5,2,3];
- Step 4: [1,4,6,5,2,3];
- Step 5: [1,4,6,5,2,3];
- Step 6: [1,4,6,5,2,3]。
- If the first step chooses stone 4:
- Step 1: [1,4,6,5,2,3];
- Step 2: [1,4,6,5,2,3];
- Step 3: [1,4,6,5,2,3];
- Step 4: [1,4,6,5,2,3];
- Step 5: [1,4,6,5,2,3];
- Step 6: [1,4,6,5,2,3]。
- If the first step chooses stone 5:
- Step 1: [1,4,6,5,2,3];
- Step 2: [1,4,6,5,2,3];
- Step 3: [1,4,6,5,2,3];
- Step 4: [1,4,6,5,2,3];
- Step 5: [1,4,6,5,2,3];
- Step 6: [1,4,6,5,2,3]。
- If the first step chooses stone 6:
- Step 1: [1,4,6,5,2,3];
- Step 2: [1,4,6,5,2,3];
- Step 3: [1,4,6,5,2,3];
- Step 4: [1,4,6,5,2,3];
- Step 5: [1,4,6,5,2,3];
- Step 6: [1,4,6,5,2,3]。
This problem uses bundled testdata.
| Subtask ID |
Points |
Special Property |
| 1 |
20 |
n,q≤300 |
| 2 |
17 |
n≤3000 |
| 3 |
12 |
n≤50000,q≤10 |
| 4 |
6 |
The values of ai are increasing |
| 5 |
16 |
All ki are equal |
| 6 |
15 |
All pi are equal |
| 7 |
14 |
No special property |
For 100% of the data, 2≤n≤105, 1≤q≤105, 1≤ai≤n and all ai are distinct, 1≤pj,kj≤n。
Translated by ChatGPT 5