luogu#P5012. 水の数列

水の数列

Background

CYJian{\rm CYJian} came up with a very fun game...

Problem Description

CYJian{\rm CYJian} now gives you a sequence of length NN. You may choose a number xx, and then obtain a score. The larger the score, the better.

The score is computed as follows:

First, mark all numbers that are less than or equal to xx. Then, your score is the sum of the squares of the lengths of every consecutive marked segment.

CYJian{\rm CYJian} thought this was too easy, the answer is obviously just the maximum value so he changed the score to be the original score divided by the number you chose.

CYJian{\rm CYJian} still thought this was too easy, so he requires that the number of segments obtained by your chosen number must be within the range ll to rr.

CYJian{\rm CYJian} still thought this was too easy, so he added TT queries.

CYJian{\rm CYJian} still thought this was too easy, so he decided to enforce online queries.

Input Format

The first line contains two positive integers nn, TT.

The second line contains nn positive integers NumiNum_i, representing the sequence given by CYJian{\rm CYJian}.

In the next TT lines, each line contains four positive integers aa, bb, xx, yy. You need to obtain the actual ll and rr as follows:

l = (a * lastans + x - 1) % n + 1;
r = (b * lastans + y - 1) % n + 1;
if (l > r) swap(l, r);

Here, lastans{\rm lastans} denotes the product of the maximum score (the original score) from the previous query and the xx that achieves this maximum score. In particular, for the first query we define lastans=0{\rm lastans}=0.

Output Format

Output TT lines. Each line contains two positive integers, representing the maximum score (the original score) that can be obtained for this query and the xx that achieves this maximum score. In particular, if there is no solution, output "1 1-1\ -1" (in this case, lastanslastans is 11). If there are multiple solutions, output the one with the largest chosen number.

CYJianCYJian wants to know whether you are guessing, so you also need to tell him LL, RR, and lastansmodnlastans \bmod n for this query.

See the sample for details.

5 3
3 5 1 2 4
233 666 1 3
555 999 2 3
123 987 233 888
25 5
1 3 0
10 4
2 3 0
-1 -1
3 3 0

Hint

${\rm Subtask\ 1(30\ pts)}:\qquad 1 \leq N,T \leq 10^2$

${\rm Subtask\ 2(30\ pts)}:\qquad 1 \leq N,T \leq 10^3$

${\rm Subtask\ 3(40\ pts)}:\qquad 1 \leq N \leq 10^6 \qquad 1 \leq T \leq 10^3$

1Numi1061 \leq Num_i \leq 10^6

All other input numbers are within the range of int{\rm int}.

Translated by ChatGPT 5