Min difference queries

4.7

6 votes
Advanced Data Structures, Data Structures, Medium, Query, Segment Trees, 普通-難しい
Problem

You have a sequence a of length n and you want to answer q queries determined by 2 integers l and r. You take all numbers present in subsequence (al,al+1,,ar), let them be b1,b2,,bt and number of occurrenses of each number in this subsequence be c1,c2,,ct respectively. The answer for per query is equal to min|cicj| for 1i<jt or 1 if t=1.

Input

The first line contains 2 integers - n and q (1n,q100000).

The second line contains n numbers - a1,a2,,an (1ai109).

Each of the next q lines contains 2 integers - ui,vi (1ui,vin).

You will need to answer the queries online so the information is encoded, if the answer for (i1)th query is last (initially last=0) then the ith query is :

li=((ui+last)modn)+1

ri=((vi+last)modn)+1

Note that "last" can be -1 in some cases.

Output

Output q lines, the ith line contains the answer for the ith query.

Sample Input
6 4
1 2 2 3 3 3
1 4
6 6
2 4
5 4
Sample Output
0
-1
1
1
Time Limit: 3
Memory Limit: 256
Source Limit:
Explanation

The queries are (2,5),(1,1),(2,4),(1,6) in this order.

For the query (1,6), b=(1,2,3) and c=(1,2,3) so the answer 1.

For the query (1,1), t=1 implies that the answer is 1.

Editor Image

?