QOJ.ac

QOJ

时间限制: 5 s 内存限制: 512 MB 总分: 100

#2408. Bald Chieftain

统计

Legend has it that there is a powerful Bald Chieftain at the Minsk Aerospace Agency. The Bald Chieftain possesses immense magical power; he has no hair on his head, his head is exceptionally hard, and he is quite fast at running. One day, the Peashooter arrived at the Minsk Aerospace Agency. To test this newcomer, the Bald Chieftain posed the following problem: Given a permutation $a_1, \dots, a_n$ of length $n$, there are $m$ queries. Each query asks for the sum of the absolute differences of the positions in the original sequence of adjacent elements after sorting the elements in the range $[l, r]$.

Input

The first line contains two integers $n, m$. The next line contains $n$ integers representing the elements of the sequence $a$. The following $m$ lines each contain two integers $l, r$ representing a query.

Output

For each query, output one integer per line representing the answer.

Constraints

  • For 10% of the data, $n, m \le 10^3$;
  • For another 10% of the data, $n, m \le 5 \times 10^4$;
  • For another 10% of the data, $n, m \le 10^5$;
  • For another 10% of the data, $n, m \le 2 \times 10^5$;
  • For another 20% of the data, $|a_i - i| \le 10$;
  • For another 20% of the data, $m = \frac{n(n-1)}{2}$;
  • For the remaining data, there are no special restrictions.
  • For 100% of the data, $1 \le n, m \le 5 \times 10^5$, $1 \le a_i \le n$, all $a_i$ are distinct, $1 \le l \le r \le n$, and all values are integers.

Examples

Input 1

5 2
5 4 2 3 1
3 4
2 5

Output 1

1
5

Note

For the first query, the elements in the range are $2, 3$. After sorting, they are $2, 3$. Their positions in the original sequence are $3, 4$. The sum of the absolute differences of the positions of adjacent elements is $|3 - 4| = 1$.

For the second query, the elements in the range are $4, 2, 3, 1$. After sorting, they are $1, 2, 3, 4$. Their positions in the original sequence are $5, 3, 4, 2$. The sum of the absolute differences of the positions of adjacent elements is $|5 - 3| + |3 - 4| + |4 - 2| = 5$.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.