VK Cup 2015 - Qualification Round 1
D
Closest Equals
Codeforces
2000
原题
← C→
翻译中…

You are given sequence $*a*_{1}, *a*_{2}, ..., *a*_{*n*}$ and $*m*$ queries $*l*_{*j*}, *r*_{*j*}$ ($1 ≤ *l*_{*j*} ≤ *r*_{*j*} ≤ *n*$). For each query you need to print the minimum distance between such pair of elements $*a*_{*x*}$ and $*a*_{*y*}$ ($*x* ≠ *y*$), that:

翻译中…
  • both indexes of the elements lie within range [$*l*_{*j*}, *r*_{*j*}$], that is, $*l*_{*j*} ≤ *x*, *y* ≤ *r*_{*j*}$;
  • the values of the elements are equal, that is $*a*_{*x*} = *a*_{*y*}$.
翻译中…

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·10^{5}$) — the length of the sequence and the number of queries, correspondingly.

翻译中…

The second line contains the sequence of integers $*a*_{1}, *a*_{2}, ..., *a*_{*n*}$ ($- 10^{9} ≤ *a*_{*i*} ≤ 10^{9}$).

翻译中…

Next $*m*$ lines contain the queries, one per line. Each query is given by a pair of numbers $*l*_{*j*}, *r*_{*j*}$ ($1 ≤ *l*_{*j*} ≤ *r*_{*j*} ≤ *n*$) — 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.

翻译中…

样例

Input #1
5 3
1 1 2 3 2
1 5
2 4
3 5
Output #1
1
-1
2
InputCopy OutputCopy
Input #2
6 5
1 2 1 3 2 3
4 6
1 3
2 5
2 4
1 6
Output #2
2
2
3
-1
2
InputCopy OutputCopy
题目信息
题号
522D
时间限制
3 s
内存限制
256 MB
标签
*special
数据结构

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。