Excluded Min

아직 제출이 없습니다시간 제한10초메모리 제한512 MB

문제

Ferume asked me if I can solve this faster than O(nnlogn)O(n \sqrt{n} \log n). And it turns out I can! Thanks to him for creating this problem and not letting it live with boring solution.

Let SS be a multiset containing non-negative integers. You can do the following operation on SS arbitrary number of times (possibly zero): choose xx such that there are at least two occurrences of xx in SS, delete one of the occurrences but insert one occurrence of (x1)(x-1) or (x+1)(x+1) instead (you can insert (x1)(x-1) only if it is non-negative). Let F(S)F(S) be the maximum mex(S)mex(S) you can achieve with these operations. Here mex(S)mex(S) is the minimal non-negative integer which is not present in SS.

You are given an array aa of length nn and qq queries \[l;r]\[l;r]. For each query, find F(a_l,a_l+1,,a_r)F(\\{ a\_{l}, a\_{l + 1}, \ldots, a\_{r} \\}).

입력

The first line contains two integers nn, qq (1n,q51051 \le n, q \le 5 \cdot 10^{5}) --- the size of array and the number of queries.

The second line contains the array a_1,a_2,,a_na\_{1}, a\_{2}, \ldots, a\_{n} itself (0a_i51050 \le a\_{i} \le 5 \cdot 10^{5}).

Next qq lines contain queries l_il\_{i} r_ir\_{i} (1l_ir_in1 \le l\_{i} \le r\_{i} \le n).

출력

Print answers to queries in the order they are listed in input on separate lines.