Under construction

Still being built. Things may change

Stones

Time limit1sMemory limit1024 MB

Summary
Given a permutation, count the starting stones that make stone p turn white exactly at step k when each step takes the smallest-valued black neighbor.
Level

Hard9 of 10

Topics
Tree, Divide and conquer, Greedy
Solved
No attempts yet

Problem

Bob has nn black stones laid out in a row, numbered from 11 to nn. Stone ii has an integer aia_i written on it. Each number from 11 to nn is written on exactly one stone, so the values aia_i form a permutation. The neighbors of stone ii are stones i−1i-1 and i+1i+1, if they exist.

Bob performs the following nn steps:

  • In step 1, Bob picks any ii from 11 to nn and paints stone ii white.
  • In steps 2 through nn, Bob looks at the black stones that neighbor at least one white stone. He picks the stone jj among them with the smallest aja_j and paints it white.

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

Alice has chosen qq pairs of values pjp_j and kjk_j. For each pair, she wants to know how many choices of the stone in step 1 make stone pjp_j turn white exactly at step kjk_j.

Help Bob answer Alice's qq queries.

Input

The first line contains two integers nn (2≤n≤1052 \le n \le 10^5), the number of stones, and qq (1≤q≤1051 \le q \le 10^5), the number of queries.

The second line contains the integers a1,a2,…,ana_1, a_2, \dots, a_n (1≤ai≤n1 \le a_i \le n, all aia_i are distinct).

Each of the next qq lines contains a query, given as two integers pjp_j and kjk_j (1≤pj≤n1 \le p_j \le n, 1≤kj≤n1 \le k_j \le n): the number of a stone and the step at which that stone must be painted white.

Output

For each query, print the number of values ii such that if stone ii is painted white in step 1, then stone pjp_j is painted white in step kjk_j. Print one answer per line.

Examples2

  1. Example 1

    Input
    6 4
    1 4 6 5 2 3
    3 1
    2 2
    6 3
    4 3
    
    Expected output
    1
    2
    1
    2
    
  2. Example 2

    Input
    5 3
    5 2 3 4 1
    2 3
    4 4
    3 2
    
    Expected output
    0
    1
    1