#P8078. [WC2022] 秃子酋长
[WC2022] 秃子酋长
Background
5s 512M -O2
Problem Description
It is said that in the Minsk Aerospace Bureau, there is a powerful Bald Chieftain.
The Bald Chieftain has boundless magic power. He has no hair on his head, and his head is very hard. He can also run fairly fast.
One day, the Peashooter came to the Minsk Aerospace Bureau.
To test this newcomer, the Bald Chieftain gave him the following problem:
Given a permutation of length , there are queries. In each query, for the interval , consider the numbers in this interval after sorting. Take the positions of these numbers in the original sequence, and compute the sum of the absolute differences between the positions of adjacent numbers in the sorted order.
Input Format
The first line contains two integers .
The next line contains integers, representing the elements of the sequence in order.
Then follow lines, each containing two integers , representing one query.
Output Format
For each query, output one integer per line, representing the answer.
5 2
5 4 2 3 1
3 4
2 5
1
5
Hint
Sample Explanation
For the first query, becomes after sorting. Their positions in the original sequence are . The sum of absolute differences of positions of adjacent elements is .
For the second query, becomes after sorting. Their positions in the original sequence are . The sum of absolute differences of positions of adjacent elements is .
Constraints
For of the testdata, .
For another of the testdata, .
For another of the testdata, .
For another of the testdata, .
For another of the testdata, .
For another of the testdata, .
For the remaining testdata, there are no special restrictions.
For of the testdata, it holds that , , all are distinct, , and all values are integers.
Translated by ChatGPT 5