5295: Closest Equals

内存限制:256 MB 时间限制:2 S 标准输入输出
题目类型:传统 评测方式:文本比较 上传者:
提交:0 通过:0

题目描述

D. Closest Equals
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given sequence a1,a2,...,an and m queries lj,rj (1≤ljrjn). For each query you need to print the minimum distance between such pair of elements ax and ay (xy), that:
  • both indexes of the elements lie within range [lj,rj], that is, ljx,yrj;
  • the values of the elements are equal, that is ax=ay.
The text above understands distance as |x-y|.
Input
The first line of the input contains a pair of integers n, m (1≤n,m≤5·105) − the length of the sequence and the number of queries, correspondingly.
The second line contains the sequence of integers a1,a2,...,an (-109ai≤109).
Next m lines contain the queries, one per line. Each query is given by a pair of numbers lj,rj (1≤ljrjn) − the indexes of the query range limits.
Output
Print m integers − the answers to each query. If there is no valid match for some query, please print -1 as an answer to this query.
Examples
Input
5 3
1 1 2 3 2
1 5
2 4
3 5
Output
1
-1
2
Input
6 5
1 2 1 3 2 3
4 6
1 3
2 5
2 4
1 6
Output
2
2
3
-1
2

输入样例 复制


输出样例 复制