luogu#P16383. [IATI 2025] Self-describing

[IATI 2025] Self-describing

Problem Description

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, \dots, b_{M-1} -- an array in which for all bib_i it is true that the number bib_i appears exactly\textit{exactly} bib_i times in the entire array. [1,2,2][1, 2, 2], [5,5,5,5,5][5,5,5,5,5], [3,1,3,2,3,2][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, \dots, b_{M-1} we define a "self-describing" subarray as a subarray bl,bl+1brb_l, b_{l+1} \dots b_r that is itself "self-describing".

You are given an array a0,a1,,aN1a_0, a_1, \dots, 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, \dots 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 NN, QQ, a1,a2,,aNa_1, a_2, \dots, a_N and QQ queries (l,r)(l, r) in this order, calls your init\verb|init| and then outputs the answers your program gave to all query\verb|query| calls. You are free to modify the local grader.

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

Hint

Constraints

  • 1N,Q3×1051 \leq N, Q \leq 3 \times 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.

Subtasks

Subtask Points Necessary subtasks NN QQ Other constraints
00 - - Example.
11 66 500\leq 500 =1=1 The only query is [1,N][1, N].
22 11 5000\leq 5000 ^
33 3939 121-2 3×105\leq 3\times 10^5
44 1111 030-3 500\leq 500
55 1616 040-4 3×105\leq 3 \times 10^5 5×104\leq 5\times10^4 -
66 2222 050-5 3×105\leq 3\times10^5 5×105\leq 5\times10^5

The points for a subtask are given only if all tests for it and the required subtasks are passed successfully.