#P4816. Closest Equals
Closest Equals
Closest Equals
题面翻译
现在有一个序列 ,还有个查询 。对于每一个查询,请找出距离最近的两个元素 和 ,并且满足以下条件:
;
。
两个数字的距离是他们下标之差的绝对值 。
题目描述
You are given sequence and queries ( ). For each query you need to print the minimum distance between such pair of elements and ( ), that:
- both indexes of the elements lie within range [ ], that is, ;
- the values of the elements are equal, that is .
The text above understands distance as .
输入格式
The first line of the input contains a pair of integers , ( ) — the length of the sequence and the number of queries, correspondingly.
The second line contains the sequence of integers ( ).
Next lines contain the queries, one per line. Each query is given by a pair of numbers ( ) — the indexes of the query range limits.
输出格式
Print integers — the answers to each query. If there is no valid match for some query, please print -1 as an answer to this query.
样例 #1
样例输入 #1
5 3
1 1 2 3 2
1 5
2 4
3 5
样例输出 #1
1
-1
2
样例 #2
样例输入 #2
6 5
1 2 1 3 2 3
4 6
1 3
2 5
2 4
1 6
样例输出 #2
2
2
3
-1
2