Mediana

Time limit: 7s Memory limit: 512MB Input: Output:

You are given a sequence of NN natural numbers a0,a1,...,aNβˆ’1a_0, a_1, ..., a_{N - 1}, all having values between 11 and NN.

On this sequence you may perform any number of operations (possibly 00) of the type (l,r)(l, r), where 0≀l≀r<N0 \leq l \leq r < N. As a result of an operation (l,r)(l, r), the entire subsequence (contiguous subarray) al,al+1,...,ara_l, a_{l + 1}, ..., a_r is replaced, within the whole sequence, by just the lower median element1^1 of that subarray.

1^1 The lower median element of a sequence of MM numbers v0,v1,...,vMβˆ’1v_0, v_1, ..., v_{M - 1} is the element on position ⌊Mβˆ’12βŒ‹\lfloor \frac{M - 1}{2} \rfloor (indexing the sequence starting from 00) after sorting the sequence (for example, the lower median element of the sequence 4,1,3,24, 1, 3, 2 is 22, and the lower median element of the sequence 2,7,5,10,12, 7, 5, 10, 1 is 55).

For a sequence VV obtained by applying the described operation (zero or more times) and a number XX, we denote by f(V,X)f(V, X) the maximum length of a contiguous subsequence of VV in which all numbers have the value XX. Also, given the function f(V,X)f(V, X), we denote by answerXanswer_X the maximum value of this function for a given value XX, considering all sequences VV that can be obtained by applying (possibly repeatedly, or not at all) the operation described above.

Requirement

Besides the given sequence aa of NN numbers, you are also given QQ queries li,ril_i, r_i, where 0≀li≀ri<N0 \leq l_i \leq r_i < N, for every ii from 00 to Qβˆ’1Q - 1. For each query, you must find βˆ‘X=1NanswerXβ‹…X\sum_{X=1}^{N} answer_X \cdot X, considering only the subsequence ali,ali+1,...,aria_{l_i}, a_{l_i + 1}, ..., a_{r_i} 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 TT times and receives as parameters:

  • NN: the length of the initial sequence;
  • QQ: the number of queries;
  • aa: the NN values of the initial sequence (indexed from 00);
  • ll and rr: representing the endpoints of the QQ queries, indexed from 00, query number ii being represented by the pair li,ril_i, r_i;

and must return a sequence containing βˆ‘X=1NanswerXβ‹…X\sum_{X=1}^{N} answer_X \cdot X for each of the QQ queries, in the order in which they are given.

Constraints

  • 1≀T,N,Q≀100Β 0001 \leq T, N, Q \leq 100 \ 000
  • 1≀ai≀N1 \leq a_i \leq N, for 0≀i<N0 \leq i < N
  • 0≀li≀ri<N0 \leq l_i \leq r_i < N, for 0≀i<Q0 \leq i < Q
  • Let MM denote the number of distinct values in the sequence aa (1≀M≀N1 \leq M \leq N)
  • Let SNS_N denote the sum of all values of NN over the TT tests, SQS_Q the sum of all values of QQ over the TT tests, and SNQS_{NQ} the sum of all products Nβ‹…QN \cdot Q over the TT tests.
  • 1≀SN,SQ≀100Β 0001 \leq S_N, S_Q \leq 100 \ 000
# Score Constraints
1 4 M=2M = 2, SNQ≀2β‹…106S_{NQ} \leq 2 \cdot 10^6
2 5 N,M,Q≀10N, M, Q \leq 10
3 13 SN,SQ≀50S_N, S_Q \leq 50
4 17 SN≀200,SQ≀500S_N \leq 200, S_Q \leq 500
5 47 SNQ≀2β‹…106S_{NQ} \leq 2 \cdot 10^6
6 13 SNQ≀2β‹…107S_{NQ} \leq 2 \cdot 10^7
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: 2,1,4,2,52, 1, 4, 2, 5. Let's analyze the case for X=2X = 2, where we want to obtain the longest possible contiguous subsequence consisting only of the value 22. We can choose to apply an operation on the elements a1,a2,a3a_1, a_2, a_3 (i.e., the values 1,4,21, 4, 2). After sorting this subsequence (1,2,41, 2, 4), the median element is 22. Replacing the subsequence with its median, the sequence becomes 2,2,52, 2, 5. We stop here, because we have obtained a contiguous subsequence containing all the initial occurrences of 22. It is not necessary for the entire sequence to be turned into values of 22, it is enough that we obtained a contiguous subsequence of length 22 made up exclusively of this value. So, answer2=2answer_2 = 2. The values 1,41, 4 and 55 cannot form subsequences longer than length 11, so answer1=1answer_1 = 1, answer4=1answer_4 = 1, answer5=1answer_5 = 1. The value 33 does not occur at all (answer3=0answer_3 = 0). The sum for the first query is: 1β‹…1+2β‹…2+0β‹…3+1β‹…4+1β‹…5=141 \cdot 1 + 2 \cdot 2 + 0 \cdot 3 + 1 \cdot 4 + 1 \cdot 5 = 14.
  • For the second query, we restrict ourselves to the first 33 elements: 2,1,42, 1, 4. Here no value repeats, and by no operation can we obtain contiguous subsequences longer than 11. Thus, answer1=1answer_1 = 1, answer2=1answer_2 = 1, answer4=1answer_4 = 1, while for the rest of the values (33 and 55) the answer is 00. The sum for the second query is: 1β‹…1+1β‹…2+0β‹…3+1β‹…4+0β‹…5=71 \cdot 1 + 1 \cdot 2 + 0 \cdot 3 + 1 \cdot 4 + 0 \cdot 5 = 7.

For the second example, similarly, the answer to the first query is 66, and to the second is 77.

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