#P7448. [Ynoi2007] rdiq

[Ynoi2007] rdiq

Problem Description

Given a sequence aa of length nn.

There are mm queries. For each query, an interval [l,r][l,r] is given. Compute {(ai,aj):li<jrai>aj}|\{(a_i,a_j) : l\le i<j\le r \wedge a_i>a_j\}|.

Input Format

The first line contains an integer nn.

The second line contains nn integers, where the ii-th integer aia_i denotes the value at position ii of the sequence.

The third line contains an integer mm.

Then follow mm lines, each line contains two integers separated by two spaces, denoting l,rl,r, which represent a query. It is guaranteed that lrl\le r.

Output Format

Output mm lines. The ii-th line contains one integer, representing the answer to the ii-th query.

5
2 1 3 2 1
4
2 4
1 5
3 5
2 2
1
3
3
0

Hint

Idea: nzhtl1477, Solution: nzhtl1477&ccz181078, Code: ccz181078, Data: w33z8kqrqk8zzzx33&nzhtl1477.

For the first query, the set is {(3,2)}\{(3,2)\}.

For the second and third queries, the set is {(2,1),(3,1),(3,2)}\{(2,1),(3,1),(3,2)\}.

For the fourth query, the set is an empty set.

For 100%100\% of the testdata, the Constraints are 1n1051\le n\le 10^5, 1m5×1051\le m\le 5\times 10^5, 1ain1\le a_i\le n.

Translated by ChatGPT 5