#P5501. [LnOI2019] 来者不拒,去者不追

[LnOI2019] 来者不拒,去者不追

Description

Given a sequence aa of length nn. There are mm queries. For each query, you need to find the sum of the “Abbi value” of all numbers in the interval [l,r][l,r].

The Abbi value is defined as follows: if aia_i is the kk-th smallest in the query interval [l,r][l,r], then its “Abbi value” equals k×aik \times a_i.

To avoid ambiguity, here is an example:

Given the sequence {1,2,2,3}\{1,2,2,3\}, then 11 is the 11-st smallest, 22 is the 22-nd smallest, and 33 is the 44-th smallest. The sum of Abbi values of the sequence is:

1×1+2×2+2×2+3×4=21.1 \times 1+2 \times 2+2 \times 2+3 \times 4=21.

Input Format

The first line contains two integers, nn and mm, representing the length of the sequence and the number of queries.

The second line contains nn numbers. The ii-th number is aia_i, representing the initial value of the sequence.

The next mm lines each contain two numbers ll and rr, representing a query interval.

Output Format

For each query, output one line with the answer.

4 2
1 2 2 3
1 4
1 2
21
5
10 5
8 6 9 8 1 1 3 10 7 9
5 8
1 3
5 7
9 9
5 6

51
49
11
7
2

Hint

Constraints

For the first 2 test points, 1n,m10001 \le n,m \le 1000, time limit 1s1\text{s}.

For the next 14 test points, 1n,ai,m1000001 \le n,a_i,m \le 100000, 1lrn1 \le l \le r \le n, time limit 1s1\text{s}.

For the last 2 test points, 1ai1000001 \le a_i \le 100000, 1lrn1 \le l \le r \le n, 1n,m5000001 \le n,m \le 500000, time limit 3s3\text{s}.

It is recommended to use fast input. It is recommended to enable O2O2 optimization.

The testdata has been strengthened.

Translated by ChatGPT 5