self_describing

Time limit: 3s Memory limit: 1024MB Input: Output:

Elena is yet again tasked with solving a problem involving subarrays with a special property. By now she finds these tasks banal, so she leaves writing a solution to you – the IATI contestants. The problem statement is as follows:

We define a “self-describing” array b0,b1,...,bM1b_0, b_1, ..., b_{M-1} – an array in which for all bib_i it is true that the number bib_i appears exactly bib_i times in the entire array. [1,2,2],[5,5,5,5,5],[3,1,3,2,3,2][1, 2, 2], [5, 5, 5, 5, 5], [3, 1, 3, 2, 3, 2] are all examples of “self-describing” arrays, while [100,1,2,2][100, 1, 2, 2] (there is only one occurrence of 100100), [1,1,1,1,1][1, 1, 1, 1, 1] (there are 55 occurrences of 11) are all examples of non-“self-describing” arrays.

Additionally for an array b0,b1,...,bM1b_0, b_1, ..., b_{M-1} we define a “self-describing” subarray as a subarray bl,bl+1,...,brb_l, b_{l+1}, ..., b_r that is itself “self-describing”.

You are given an array a0,a1,...,aN1a_0, a_1, ..., a_{N-1} and QQ queries (l,r)(l,r) such that lrl \leq r. For each query you should find the number of “self-describing” subarrays (l,r)(l',r') for which llrrl \leq l' \leq r' \leq r for all queries.

Implementation details

You should implement the following two procedures:

void init(int N, int Q, const std::vector<int>& a)

This function will be called once per test and provides your program with the original array as a vector, consisting of the NN values a0,a1,...,aN1a_0, a_1, ..., a_{N-1} in this order.

long long query(int l, int r)

This function will be called QQ times per test and will correspond to a query for the range (l,r)(l,r), it should return the answer to that query.

Local testing

To test your program locally, a local grader and a header file are provided. The local grader reads N,Q,a1,a2,...,aNN,Q,a_1,a_2,...,a_N and QQ queries (l,r)(l, r) in this order, calls your init and then outputs the answers your program gave to all query calls. You are free to modify the local grader.

Constraints and clarifications

  • 1N,Q31051 \leq N, Q \leq 3 \cdot 10^5
  • 1aiN1 \leq a_i \leq N for all 0iN10 \leq i \leq N-1
  • 0lrN10 \leq l \leq r \leq N-1 for all queries.
Subtask Points Necessary subtasks NN QQ Other constraints
0 0 - - Example.
1 6 - 500\leq 500 =1= 1 The only query is [1,N][1, N].
2 6 11 5 000\leq 5 \ 000 =1= 1 The only query is [1,N][1, N].
3 39 121-2 3105\leq 3 \cdot 10^5 =1= 1 The only query is [1,N][1, N].
4 11 030-3 3105\leq 3 \cdot 10^5 500\leq 500 -
5 16 040-4 3105\leq 3 \cdot 10^5 5104\leq 5 \cdot 10^4 -
6 22 050-5 3105\leq 3 \cdot 10^5 3105\leq 3 \cdot 10^5 -

Example

stdin

7 3
1 2 1 2 3 3 3
0 3
2 6
0 6

stdout

3
2
5

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