You are given a sequence of natural numbers , all having values between and .
On this sequence you may perform any number of operations (possibly ) of the type , where . As a result of an operation , the entire subsequence (contiguous subarray) is replaced, within the whole sequence, by just the lower median element of that subarray.
The lower median element of a sequence of numbers is the element on position (indexing the sequence starting from ) after sorting the sequence (for example, the lower median element of the sequence is , and the lower median element of the sequence is ).
For a sequence obtained by applying the described operation (zero or more times) and a number , we denote by the maximum length of a contiguous subsequence of in which all numbers have the value . Also, given the function , we denote by the maximum value of this function for a given value , considering all sequences that can be obtained by applying (possibly repeatedly, or not at all) the operation described above.
Requirement
Besides the given sequence of numbers, you are also given queries , where , for every from to . For each query, you must find , considering only the subsequence of the given sequence as the sequence on which the operations are applied.
Implementation Details
You must implement a single function:
std::vector<int64_t> solve(int N, int Q, std::vector<int> a,
std::vector<int> l, std::vector<int> r)
The solve function will be called by the judge at most times and receives as parameters:
- : the length of the initial sequence;
- : the number of queries;
- : the values of the initial sequence (indexed from );
- and : representing the endpoints of the queries, indexed from , query number being represented by the pair ;
and must return a sequence containing for each of the queries, in the order in which they are given.
Constraints
- , for
- , for
- Let denote the number of distinct values in the sequence ()
- Let denote the sum of all values of over the tests, the sum of all values of over the tests, and the sum of all products over the tests.
| # | Score | Constraints |
|---|---|---|
| 1 | 4 | , |
| 2 | 5 | |
| 3 | 13 | |
| 4 | 17 | |
| 5 | 47 | |
| 6 | 13 | |
| 7 | 1 | No additional restrictions. |
Examples
input
2
5
2 1 4 2 5
2
0 4
0 2
5
1 2 2 1 1
2
1 4
0 4
output
14
7
6
7
Explanations
In the first example:
- For the first query, we apply operations on the whole sequence: . Let's analyze the case for , where we want to obtain the longest possible contiguous subsequence consisting only of the value . We can choose to apply an operation on the elements (i.e., the values ). After sorting this subsequence (), the median element is . Replacing the subsequence with its median, the sequence becomes . We stop here, because we have obtained a contiguous subsequence containing all the initial occurrences of . It is not necessary for the entire sequence to be turned into values of , it is enough that we obtained a contiguous subsequence of length made up exclusively of this value. So, . The values and cannot form subsequences longer than length , so , , . The value does not occur at all (). The sum for the first query is: .
- For the second query, we restrict ourselves to the first elements: . Here no value repeats, and by no operation can we obtain contiguous subsequences longer than . Thus, , , , while for the rest of the values ( and ) the answer is . The sum for the second query is: .
For the second example, similarly, the answer to the first query is , and to the second is .