luogu#P10100. [ROIR 2023] 石头 (Day 2)

[ROIR 2023] 石头 (Day 2)

Background

Translated from ROIR 2023 D2T3

Problem Description

In front of Bob there are nn black stones arranged in a row, numbered from 11 to nn. On the ii-th stone there is an integer aia_i, and the integers on the nn stones form a permutation. We call the adjacent stones of the ii-th stone the (i1)(i-1)-th and the (i+1)(i+1)-th stones (if they exist).

Bob performs nn operations according to the following steps:

  • In the first step, choose any ii (1in1 \le i \le n) and paint the ii-th stone white.
  • In steps 22 to nn, among all black stones that have at least one adjacent white stone, choose the stone jj with the smallest aia_i (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 aia_i), and paint it white.

It is easy to see that after all steps are completed, all nn stones will be white.

Alice chose qq pairs of values pjp_j and kjk_j. For each pair pp and kk, she wants to know how many different choices of the first step (i.e., which stone is painted white first) make the pp-th stone become white at step kk.

Help Bob answer Alice's qq queries.

Input Format

The first line contains two integers nn and qq, representing the number of stones and the number of queries.

The second line contains nn integers a1,a2,,ana_1, a_2, \dots, a_n, representing the integers written on the stones.

The next qq lines each contain a pair of integers pjp_j and kjk_j.

Output Format

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 11:
    • Step 11: [1,4,6,5,2,3][\bold1, \gray4, \gray6, \gray5, \gray2, \gray3]
    • Step 22: [1,4,6,5,2,3][\bold1, \bold4, \gray6, \gray5, \gray2, \gray3]
    • Step 33: [1,4,6,5,2,3][\bold1, \bold4, \bold6, \gray5, \gray2, \gray3]
    • Step 44: [1,4,6,5,2,3][\bold1, \bold4, \bold6, \bold5, \gray2, \gray3]
    • Step 55: [1,4,6,5,2,3][\bold1, \bold4, \bold6, \bold5, \bold2, \gray3]
    • Step 66: [1,4,6,5,2,3][\bold1, \bold4, \bold6, \bold5, \bold2, \bold3]
  • If the first step chooses stone 22:
    • Step 11: [1,4,6,5,2,3][\gray1, \bold4, \gray6, \gray5, \gray2, \gray3]
    • Step 22: [1,4,6,5,2,3][\bold1, \bold4, \gray6, \gray5, \gray2, \gray3]
    • Step 33: [1,4,6,5,2,3][\bold1, \bold4, \bold6, \gray5, \gray2, \gray3]
    • Step 44: [1,4,6,5,2,3][\bold1, \bold4, \bold6, \bold5, \gray2, \gray3]
    • Step 55: [1,4,6,5,2,3][\bold1, \bold4, \bold6, \bold5, \bold2, \gray3]
    • Step 66: [1,4,6,5,2,3][\bold1, \bold4, \bold6, \bold5, \bold2, \bold3]
  • If the first step chooses stone 33:
    • Step 11: [1,4,6,5,2,3][\gray1, \gray4, \bold6, \gray5, \gray2, \gray3]
    • Step 22: [1,4,6,5,2,3][\gray1, \bold4, \bold6, \gray5, \gray2, \gray3]
    • Step 33: [1,4,6,5,2,3][\bold1, \bold4, \bold6, \gray5, \gray2, \gray3]
    • Step 44: [1,4,6,5,2,3][\bold1, \bold4, \bold6, \bold5, \gray2, \gray3]
    • Step 55: [1,4,6,5,2,3][\bold1, \bold4, \bold6, \bold5, \bold2, \gray3]
    • Step 66: [1,4,6,5,2,3][\bold1, \bold4, \bold6, \bold5, \bold2, \bold3]
  • If the first step chooses stone 44:
    • Step 11: [1,4,6,5,2,3][\gray1, \gray4, \gray6, \bold5, \gray2, \gray3]
    • Step 22: [1,4,6,5,2,3][\gray1, \gray4, \gray6, \bold5, \bold2, \gray3]
    • Step 33: [1,4,6,5,2,3][\gray1, \gray4, \gray6, \bold5, \bold2, \bold3]
    • Step 44: [1,4,6,5,2,3][\gray1, \gray4, \bold6, \bold5, \bold2, \bold3]
    • Step 55: [1,4,6,5,2,3][\gray1, \bold4, \bold6, \bold5, \bold2, \bold3]
    • Step 66: [1,4,6,5,2,3][\bold1, \bold4, \bold6, \bold5, \bold2, \bold3]
  • If the first step chooses stone 55:
    • Step 11: [1,4,6,5,2,3][\gray1, \gray4, \gray6, \gray5, \bold2, \gray3]
    • Step 22: [1,4,6,5,2,3][\gray1, \gray4, \gray6, \gray5, \bold2, \bold3]
    • Step 33: [1,4,6,5,2,3][\gray1, \gray4, \gray6, \bold5, \bold2, \bold3]
    • Step 44: [1,4,6,5,2,3][\gray1, \gray4, \bold6, \bold5, \bold2, \bold3]
    • Step 55: [1,4,6,5,2,3][\gray1, \bold4, \bold6, \bold5, \bold2, \bold3]
    • Step 66: [1,4,6,5,2,3][\bold1, \bold4, \bold6, \bold5, \bold2, \bold3]
  • If the first step chooses stone 66:
    • Step 11: [1,4,6,5,2,3][\gray1, \gray4, \gray6, \gray5, \gray2, \bold3]
    • Step 22: [1,4,6,5,2,3][\gray1, \gray4, \gray6, \gray5, \bold2, \bold3]
    • Step 33: [1,4,6,5,2,3][\gray1, \gray4, \gray6, \bold5, \bold2, \bold3]
    • Step 44: [1,4,6,5,2,3][\gray1, \gray4, \bold6, \bold5, \bold2, \bold3]
    • Step 55: [1,4,6,5,2,3][\gray1, \bold4, \bold6, \bold5, \bold2, \bold3]
    • Step 66: [1,4,6,5,2,3][\bold1, \bold4, \bold6, \bold5, \bold2, \bold3]

This problem uses bundled testdata.

Subtask ID Points Special Property
11 2020 n,q300n, q \le 300
22 1717 n3000n \le 3000
33 1212 n50000,q10n \le 50000, q \le 10
44 66 The values of aia_i are increasing
55 1616 All kik_i are equal
66 1515 All pip_i are equal
77 1414 No special property

For 100%100\% of the data, 2n1052 \le n \le 10^5, 1q1051 \le q \le 10^5, 1ain1 \le a_i \le n and all aia_i are distinct, 1pj,kjn1 \le p_j, k_j \le n

Translated by ChatGPT 5