#d11403. 不降子数组游戏

不降子数组游戏

Non-decreasing Subarray Game

Problem Description

Yuto and Platina are about to play a non-decreasing-subarray game.

Specifically, given an array AA of length NN, and bound indices LL and RR.

Yuto first chooses a number in [L,R][L, R] and shows it to Platina.

Then Platina also chooses a number in [L,R][L, R].

Suppose Yuto chooses aa and Platina chooses bb.

The score of this round is the number of non-decreasing subarrays of A[min⁡(a,b):max⁡(a,b)]A[\min(a,b):\max(a,b)] (A[l:r]A[l:r] denotes the new array formed by the contiguous segment of AA from index ll to rr).

Note: a subarray of array BB is obtained by deleting some (possibly empty) prefix and suffix of BB.

Yuto wants the score to be as small as possible, while Platina wants it as large as possible.

They will play on the array QQ times. For each game, output the final score.

Input Format

The first line contains a positive integer NN, the length of array AA.

The next line contains NN positive integers, representing A1,A2,…,ANA_1, A_2, \dots, A_N.

The third line contains a positive integer QQ, the number of games.

The next QQ lines each contain two positive integers LL and RR.

For all data: 1≤N,Q≤5×1051 \le N, Q \le 5 \times 10^5, 1≤Ai≤1091 \le A_i \le 10^9, and 1≤L≤R≤N1 \le L \le R \le N.

Output Format

For each game, output one positive integer: the final score of the game.

Sample Input

8
7 10 3 1 9 5 5 2
5
1 5
2 2
5 8
1 8
3 5

Sample Output

4
1
4
7
3