souvenirs

Souvenirs

Artsem is on vacation and wants to buy souvenirs for his two teammates. There are \(n\) souvenir shops along the street. In \(i^{th}\) shop Artsem can buy one souvenir for \(a_i\) dollars, and he cannot buy more than one souvenir in one shop. He doesn't want to introduce envy in his team, so he wants to buy two souvenirs with least possible difference in price.

Artsem has visited the shopping street \(m\) times. For some strange reason on the \(i^{th}\) day only shops with numbers from \(l_i\) to \(r_i\) were operating (weird? yes it is, but have you ever tried to come up with a reasonable legend for a range query problem?). For each visit, Artsem wants to know the minimum possible difference in prices of two different souvenirs he can buy in the opened shops.

In other words, for each Artsem's visit you should find the minimum possible value of \(|a_s-a_t|\) where \(l_i \leq s, t \leq r_i, s \ne t\).

Limits

\(2 \leq n \leq 10^5\)
\(1 \leq m \leq 3*10^5\)
\(0 \leq a_i \leq 10^9\)
\(0 \leq l_i \leq r_i \leq n\)

Subtasks # Score Constraits
1 21 \(a_i\) is in non-decreasing order
2 30 \(n \leq 1000\)
\(m \leq 1000\)
3 49 -

Input format

The first line contains \(n\).

The next line contains \(n\) integers, where the \(i^{th}\) integer represents \(a_i\)

The next line contains \(m\)

The next \(m\) lines contains 2 integers, where the \(i^{th}\) of these lines represents \(l_{i}\) and \(r_i\)

Output format

Print the answer to each query in a separate line.

Samples

Sample Input 1 Sample Output 1
8
3 1 4 1 5 9 2 6
4
1 8
1 3
4 8
5 7
0
1
1
3


Submitting .cpp to 'souvenirs'


You're not logged in! Click here to login

Time Limit: 3 Seconds
Memory Limit: 1024MB
Your best score: 0
Source: Codeforces 765F
Editorial: 1

Subtask Score
1 21
2 30
3 49
4 0