#P11015. Inversion Pair

Inversion Pair

Background

.

Problem Description

For a sequence pp, we define W(p)\mathrm{W}(p) as the number of inversion pairs in pp.

Note: An inversion pair here means a pair that satisfies pi>pjp_i > p_j and i<ji < j.

Now you are given a sequence aa, which is a permutation of the integers 1n1 \sim n. That is, for any 1in1 \le i \le n, we have 1ain1 \le a_i \le n, all aia_i are integers, and they are all distinct.

There is a graph with nn nodes, numbered 1n1 \sim n. For any two nodes with indices i,j (1i<jn)i, j \ (1 \le i < j \le n), we add an undirected edge between them with weight W([ai,ai+1,,aj1,aj])\mathrm{W}([a_i, a_{i+1}, \dots, a_{j-1}, a_j]).

There are qq queries. Each query gives two nodes numbered x,yx, y, and you need to find the shortest path between them.

Input Format

The first line contains two integers n,qn, q.

The second line contains nn integers representing the sequence aa.

The next qq lines each contain two integers, representing one query x,yx, y.

Output Format

For each query:

Output one integer, the length of the required shortest path.

3 2
3 1 2
1 3
2 2
1
0

Hint

For all testdata, it is guaranteed that: 2n3×1052 \le n \le 3 \times 10^5, 1q3×1051 \le q \le 3 \times 10^5, 1x,yn1 \le x, y \le n.

Subtask\text{Subtask} nn \le qq \le Score Special Property
00 100100 3030 None
11 3×1053 \times 10^5 7070

Translated by ChatGPT 5