Stones
Time limit1sMemory limit1024 MB
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 black stones laid out in a row, numbered from to . Stone has an integer written on it. Each number from to is written on exactly one stone, so the values form a permutation. The neighbors of stone are stones and , if they exist.
Bob performs the following steps:
- In step 1, Bob picks any from to and paints stone white.
- In steps 2 through , Bob looks at the black stones that neighbor at least one white stone. He picks the stone among them with the smallest and paints it white.
It is easy to see that after all steps, all stones are white.
Alice has chosen pairs of values and . For each pair, she wants to know how many choices of the stone in step 1 make stone turn white exactly at step .
Help Bob answer Alice's queries.
Input
The first line contains two integers (), the number of stones, and (), the number of queries.
The second line contains the integers (, all are distinct).
Each of the next lines contains a query, given as two integers and (, ): 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 such that if stone is painted white in step 1, then stone is painted white in step . Print one answer per line.