#P12505. 「CEOI2025」等于 mex

「CEOI2025」等于 mex

Description

It is well known among Romanian noblemen that the beauty of an integer array a[0],a[1],a[2],,a[m1]a[0], a[1], a[2], \ldots, a[m-1] is the number of positive integers kk for which you can split the array into kk disjoint subarrays (sequences of consecutive elements) such that each element is contained in exactly one subarray and all the subarrays have the same minimum excluded element. The minimum excluded element of an integer array is the smallest strictly positive integer (greater than 00) that does not appear in the array.

You are given an integer array v[0],v[1],,v[n1]v[0], v[1], \ldots, v[n-1] and qq queries of the form (l,r)(l, r), where 0lr<n0 \leq l \leq r < n for all 0i<q0 \leq i < q.

For each query, you have to find the beauty of the array v[l],v[l+1],,v[r]v[l], v[l+1], \ldots, v[r].

Implementation Details

You should implement the following procedure:

std::vector<int> solve(
    int n, std::vector<int>& v,
    int q, std::vector<std::pair<int, int>>& queries);
  • nn: the size of the integer array
  • vv: array of length nn, the initial array
  • qq: the number of queries
  • queriesqueries: array of length qq describing the queries
  • This procedure should return a vector of qq integers containing the answer for each query.
  • This procedure is called exactly once for each test case.

Constraints

  • 1n6000001 \leq n \leq 600\,000
  • 1q6000001 \leq q \leq 600\,000
  • 1v[i]4000001 \leq v[i] \leq 400\,000 for all 0i<n0 \leq i < n
  • 0lr<n0 \leq l \leq r < n for all 0i<q0 \leq i < q

Subtasks

  1. (4 points) 1n101 \leq n \leq 10, 1q1001 \leq q \leq 100
  2. (6 points) 1n,q1001 \leq n, q \leq 100
  3. (17 points) 1n,q10001 \leq n, q \leq 1000
  4. (10 points) 1n,q1000001 \leq n, q \leq 100\,000 and 1v[i]21 \leq v[i] \leq 2 for all 0i<n0 \leq i < n
  5. (30 points) 1n,q750001 \leq n, q \leq 75\,000
  6. (33 points) No additional constraints.

Example 1

Consider the following call:

solve(10, {1, 1, 2, 2, 3, 3, 1, 2, 3, 4}, 2, {{0, 5}, {0, 8}})

In this sample n=10n=10 and there are 22 queries for which:

  • l=0l = 0 and r=5r = 5
  • l=0l = 0 and r=8r = 8

For the first query, we can split the interval in only one subarray, which is from position 00 to position 55.

In the second query, kk could be either 11 or 22. A possibility of splitting into 11 subarray is by choosing the subarray from position 00 to position 88. A possibility of splitting into 22 subarrays is by choosing the subarray from position 00 to position 55 and from position 66 to position 88.

The answer for the first query is 11 and for the second query, it is 22, so the call to solve will return {1, 2}.

Sample grader

The sample grader reads the input in the following format:

  • line 11: n qn\ q
  • line 22: v[0] v[1]  v[n1]v[0]\ v[1]\ \ldots\ v[n-1]
  • line 3+i3+i: l rl\ r for all 0i<q0 \leq i < q

and outputs qq lines, the result of the call to function solve with the corresponding parameters.