#d1205. Closest Equals
Closest Equals
Closest Equals
Problem Description
You are given a sequence indexed from to , and then queries.
Each query gives a range ; find the minimum distance between two equal elements and within the range — that is, find a pair such that:
- ;
- ();
and minimize . If no two equal elements exist in the range, output .
Input Format
The first line contains two integers with .
The second line contains integers with .
Each of the next lines contains two integers with .
Output Format
Output lines, each containing one integer: the answer to the query.
Sample Input 1
5 3
1 1 2 3 2
1 5
2 4
3 5
Sample Output 1
1
-1
2
Sample Input 2
6 5
1 2 1 3 2 3
4 6
1 3
2 5
2 4
1 6
Sample Output 2
2
2
3
-1
2