Under construction

Still being built. Things may change

Hot Potato

Time limit2sMemory limit512 MB

Summary
For each query range, find the smallest X maximizing the gap between ending and not ending, given a functional graph walk from a uniform random start.
Level

Hard8 of 10

Topics
Graph, Probability, Math, Binary search
Solved
No attempts yet

Problem

Hot potato is a game children play at school. The rules are simple. The child holding the potato throws it to another child, and at some point the teacher, who is not watching, announces that the game is over. Whoever holds the potato at that moment loses.

A teacher proposed a variation played in the cafeteria line. The children are numbered 1 to NN by their position in the line, and child 1 stands at the front. Each child receives a slip of paper with one number on it, and whenever a child receives the potato, that child must pass it to the child at the position written on the slip. The game ends with the teacher winning if the potato lands on a position at most XX. The teacher fixes XX before the game starts, and 1≤X≤N1 \le X \le N. If the potato never lands on a position at most XX, the game never ends and the children win instead: the next day they all get a discount in the cafeteria.

The teacher starts the game by throwing the potato to one of the children. His aim is poor, so all he can guarantee is that he throws it to one of the children in positions LL through RR, each with the same probability. The position the potato first lands on counts as a position the potato lands on, so if the first child to receive it has a number at most XX, the game ends right there.

The teacher is considering several candidate ranges, and for each one he wants the XX that makes the game as fair as possible. Fair here means the difference between the probability that the game ends and the probability that it does not end is as small as possible. Given every child's slip and the candidate ranges, report that XX for each range. When several values of XX are equally fair, answer with the one closer to the front of the line, that is, the smallest XX.

Input

The first line contains two integers NN and QQ. The second line contains NN integers p1,p2,…,pNp_1, p_2, \dots, p_N, where pip_i is the number on the slip child ii received. Each of the next QQ lines contains two integers LL and RR, one range the teacher is considering.

Constraints

  • 2≤N≤500002 \le N \le 50000
  • 1≤Q≤1000001 \le Q \le 100000
  • 1≤pi≤N1 \le p_i \le N
  • 1≤L≤R≤N1 \le L \le R \le N

Output

Print QQ lines. Line ii contains the integer XX the teacher should choose for the ii-th range.

Examples4

  1. Example 1

    Input
    9 4
    2 3 4 5 6 7 4 9 5
    1 3
    3 5
    2 8
    7 9
    
    Expected output
    1
    3
    3
    1
    
  2. Example 2

    Input
    3 3
    1 3 3
    1 1
    1 2
    2 3
    
    Expected output
    1
    1
    2
    
  3. Example 3

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

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