For an array of integers b=[b_1,b_2,\ldots,b_m], let denote the smallest non-negative integer that does not appear in the array .
Define as an array of length such that
In other words, the -th element of the array is equal to the MEX of the prefix of array of length .
Also define the function :
and for k\ge 1
That is, g(k,b) is the result of applying the function to array exactly times.
You are given an array a=[a_1,a_2,\ldots,a_n].
You need to answer independent queries. Each query is given by five integers , , , , .
For each query, consider the array
and construct
For this query, you need to find the value
The queries do not modify the original array .
The first line contains two integers and (1 \le n,q \le 3 \cdot 10^5) — the length of the array and the number of queries, respectively.
The second line contains integers a_1,a_2,\ldots,a_n (0 \le a_i \le n).
Each of the next lines contains five integers , , , , (1 \le L \le R \le n, 1 \le k \le 10^9, 1 \le l \le r \le R-L+1).
For each query, output one integer — the sum
where
Print the answers to the queries in separate lines.
Example 1
In the first query, we have
The prefix MEX values of this array are
Since k=1, we have d=[0,1,3]. We need to find the sum of the elements from position to position :
In the second query, we have
After the first application of function , we get
Apply function once more:
Thus,
so the sum of all its elements is
In the third query, consider the array
Its only prefix has MEX equal to , so
Therefore, the answer is .
Subtask ( points): L = R for all queries.
Subtask ( points): a_i = a_{i+1} for all 1 \le i < n.
Subtask ( points): k = 10^9 for all queries.
Subtask ( points): n,q,k \le 100.
Subtask ( points): n,q \le 1000.
Subtask ( points): l \ne r, k \ne 2 for all queries.
Subtask ( points): L = 1, k \ne 2 for all queries.
Subtask ( points): k = 2 for all queries.
Subtask ( points): L = 1 for all queries.
Subtask ( points): No additional constraints.