#P5648. Mivik的神力

Mivik的神力

Description

$\textcolor{black}{\text{M}} \textcolor{red}{\text{ivik}}$ wants to write an article. While writing, he has nn candidate words. The ii-th word has length aia_i. $\textcolor{black}{\text{M}} \textcolor{red}{\text{ivik}}$ can choose to start writing from the ii-th word and write for a total of kk seconds. In the jj-th second, he writes the (i+j1)(i+j-1)-th word (j[1,k])(j\in[1,k]). While writing, he gains happiness points every second. The happiness points in the jj-th second are maxl=ii+j1al\max_{l=i}^{i+j-1} a_l. Now please help him compute, for each writing process, the sum of happiness points he gains.

One-sentence statement: Given a sequence and multiple queries (l,q)(l,q), compute

i=ll+q1maxljiaj\sum_{i=l}^{l+q-1} \max_{l\le j\le i}a_j

The testdata requires mandatory online processing.

Input Format

The first line contains two integers nn and tt, representing the number of words and the number of queries.

The second line contains nn integers. The ii-th integer represents aia_i.

In the next tt lines, each line contains two integers uu and vv, where l=1+(ulastans)modnl=1+(u \oplus lastans)\bmod n and q=1+(v(lastans+1))mod(nl+1)q=1+(v \oplus (lastans+1))\bmod (n-l+1). This represents a query asking for the sum of happiness points when starting from the ll-th second and writing for qq seconds.

lastanslastans denotes the answer to the previous query, with initial value lastans=0lastans=0.

Output Format

For each query, output one line containing the answer.

3 2
1 2 3
1 1
1 2
2
3

Hint

Sample Explanation

For the first query 1,11,1, after decoding we get l=2,q=1l=2,q=1. Then according to the statement, the answer is the maximum value on interval [2,2][2,2], which is 22.

For the first query 1,21,2, after decoding we get l=1,q=2l=1,q=2. Then the answer is the sum of the maximum values on intervals [1,1][1,1] and [1,2][1,2], which is 33.


For 20%20\% of the testdata, n1000n \leq 1000 and t1000t \leq 1000.

For 100%100\% of the testdata, n500000n\leq 500000, t500000t\leq 500000, and 1ai109(i[1,n])1 \leq a_i\leq 10^9(i\in [1,n]).

Translated by ChatGPT 5