FIICode 2025 Qualifying Round - Elevi | Accent

This was the problem page during the contest. Access the current page here.
Time limit: 5s Memory limit: 512MB Input: Output:

The accent is on ’pe’.–Dan The Badger, Uncountable Talents\textit{The accent is on 'pe'.} \\ \rule{6cm}{0.4pt} \\ \textit{--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)f(b_1,b_2,...,b_k) to be the third smallest number (including duplicates) among b1b_1, b2b_2,..., bkb_k. For example, f(1,2,3)=3f(1,2,3)=3 and f(2,1,1,3)=2f(2,1,1,3)=2.

Now, they have an array [a1,a2,,an][a_1,a_2,\ldots, a_n] and qq queries of the form (l,r)(l,r) for which they have to determine the following:

i=lr2j=i+2rf(ai,ai+1,...,aj)\displaystyle \sum_{i = l}^{r-2} \sum_{j = i+2}^r f(a_i,a_{i+1},...,a_{j})

Input data

The first line of input contains the numbers nn and qq (3n,q51053 \le n,q \le 5 \cdot 10^5).

The second line contains nn numbers a1a_1, a2a_2,..., ana_n (1ai1061 \le a_i \le 10^6).

The next qq lines contain two numbers, ll and rr (1l,rn1 \le l,r \le n and rl+2r \ge l+2), denoting a query.

Output data

The first qq 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)=4f(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=11f(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=12f(4,3,4) + f(3,4,4) + f(4,3,4,4) = 4 + 4 + 4 = 12

Log in or sign up to be able to send submissions!