The accent is on ’pe’.–Dan The Badger, Uncountable Talents
Task
Dan The Badger and Sorin The Golden Child gathered in the studio one day to solve a problem. Dan told Sorin that for any array the accent falls on the third smallest number. Thus, we can denote f(b1,b2,...,bk) to be the third smallest number (including duplicates) among b1, b2,..., bk. For example, f(1,2,3)=3 and f(2,1,1,3)=2.
Now, they have an array [a1,a2,…,an] and q queries of the form (l,r) for which they have to determine the following:
i=l∑r−2j=i+2∑rf(ai,ai+1,...,aj)
The first line of input contains the numbers n and q (3≤n,q≤5⋅105).
The second line contains n numbers a1, a2,..., an (1≤ai≤106).
The next q lines contain two numbers, l and r (1≤l,r≤n and r≥l+2), denoting a query.
Output data
The first q lines will contain one number each, representing the answer for the corresponding query.
Example
stdin
6 4
1 2 4 3 4 4
2 4
1 4
3 6
1 6
stdout
4
11
12
37
Explanation
For the first query, we have f(2,4,3)=4.
For the second query, we have f(1,2,4)+f(2,4,3)+f(1,2,4,3)=4+4+3=11.
For the third query, we have f(4,3,4)+f(3,4,4)+f(4,3,4,4)=4+4+4=12