Compressible

Time limit: 1.2sMemory limit: 256MBInput: Output:

Let b0,b1,…,bm−1b_0, b_1, \ldots, b_{m-1} be a sequence of distinct integers. A bucket is a set of values. Consider the following process:

  • Start with mm buckets placed in a row, where the bucket at position kk is {bk}\{b_k\}, for 0≤k<m0 \leq k < m.
  • While there is more than one bucket, choose any two adjacent buckets XX and YY such that X∪YX \cup Y is a set of consecutive integers, and replace them, in the same place in the row, with the single bucket X∪YX \cup Y. If no such pair of buckets exists, the process stops.

The sequence bb is called compressible if the choices in step 22 can be made so that the process ends with a single bucket.

For example, {2}\{2\} and {3,4}\{3, 4\} can be merged, and so can {1,2,3}\{1, 2, 3\} and {4,5,6}\{4, 5, 6\}, but {2}\{2\} and {4}\{4\} cannot. The sequence 3,1,2,6,5,43, 1, 2, 6, 5, 4 is compressible:

{3} {1} {2} {6} {5} {4}  →  {3} {1,2} {6} {5} {4}  →  {1,2,3} {6} {5} {4}  →  {1,2,3} {5,6} {4}  →  {1,2,3} {4,5,6}  →  {1,2,3,4,5,6}.\begin{align*} & \{3\}\ \{1\}\ \{2\}\ \{6\}\ \{5\}\ \{4\} \;\to\; \{3\}\ \{1, 2\}\ \{6\}\ \{5\}\ \{4\} \;\to\; \{1, 2, 3\}\ \{6\}\ \{5\}\ \{4\} \\ \;\to\; & \{1, 2, 3\}\ \{5, 6\}\ \{4\} \;\to\; \{1, 2, 3\}\ \{4, 5, 6\} \;\to\; \{1, 2, 3, 4, 5, 6\}. \end{align*}

The sequence 2,4,1,32, 4, 1, 3 is not compressible, even though its values form the set {1,2,3,4}\{1, 2, 3, 4\}: no two adjacent buckets can be merged in the first iteration of step 22, therefore the process stops.

Task

You are given a sequence of NN distinct integers a0,a1,…,aN−1a_0, a_1, \ldots, a_{N-1} and QQ queries of the form (l,r)(l, r), with 0≤l≤r<N0 \leq l \leq r < N. For each query, find the number of pairs (i,j)(i, j) with l≤i≤j≤rl \leq i \leq j \leq r such that the subarray ai,ai+1,…,aja_i, a_{i+1}, \ldots, a_j is compressible.

Implementation details

This is a function-implementation task. Your program must not read from standard input or write to standard output. You must include the header compressible.h and implement the following function, without implementing a main function:

std::vector<long long> solve(int N, int Q, std::vector<int> a, std::vector<int> l, std::vector<int> r);
  • NN: the length of the sequence;
  • QQ: the number of queries;
  • aa: a vector of length NN, where a[k]=aka[k] = a_k for 0≤k<N0 \leq k < N;
  • ll, rr: two vectors of length QQ, where query kk is (l[k],r[k])(l[k], r[k]), for 0≤k<Q0 \leq k < Q;
  • the function must return a vector of length QQ, whose element at index kk is the answer to query (l[k],r[k])(l[k], r[k]);
  • the function is called exactly once per test case.

Sample grader

The sample grader reads the input from standard input in the following format:

  • line 11: NN QQ;
  • line 22: a0a_0 a1a_1 …\ldots aN−1a_{N-1};
  • line 3+k3 + k (0≤k<Q0 \leq k < Q): l[k]l[k] r[k]r[k].

The sample grader calls solve and prints the QQ returned values to standard output, one per line.

The grader used for evaluation may differ from the sample grader.

Restrictions

  • 1≤N,Q≤200 0001 \leq N, Q \leq 200 \ 000.
  • 1≤ai≤1 000 000 0001 \leq a_i \leq 1 \ 000 \ 000 \ 000, and the values aia_i are distinct.
  • 0≤l≤r<N0 \leq l \leq r < N for every query.
  • The sequence aa is called a mountain if there is a position pp, 0≤p<N0 \leq p < N, such that a0<a1<⋯<ap>ap+1>⋯>aN−1a_0 < a_1 < \cdots < a_p > a_{p+1} > \cdots > a_{N-1}.
# Points Restrictions
0 0 Examples.
1 2 Q=1Q = 1, the query is (0,N−1)(0, N - 1), N≤10N \leq 10.
2 3 Q=1Q = 1, the query is (0,N−1)(0, N - 1), N≤400N \leq 400.
3 4 Q=1Q = 1, the query is (0,N−1)(0, N - 1), N≤4 000N \leq 4 \ 000.
4 8 Q=1Q = 1, the query is (0,N−1)(0, N - 1), the sequence aa is a mountain.
5 9 Q=1Q = 1, the query is (0,N−1)(0, N - 1), the sequence aa is compressible.
6 11 Q=1Q = 1, the query is (0,N−1)(0, N - 1).
7 4 N≤400N \leq 400
8 6 N≤4 000N \leq 4 \ 000
9 2 Q≤200Q \leq 200
10 13 The sequence aa is a mountain.
11 17 The sequence aa is compressible.
12 21 No additional constraints.

Example 1

Sample grader input

10 4
3 1 2 6 8 5 7 4 10 9
0 9
1 7
3 6
7 9

Sample grader output

13
8
4
4

Explicație

The sample grader makes the following call:

solve(10, 4, {3, 1, 2, 6, 8, 5, 7, 4, 10, 9}, {0, 1, 3, 7}, {9, 7, 6, 9});

Besides the 1010 subarrays of length 11, the only compressible subarrays are (0,2)(0, 2), (1,2)(1, 2) and (8,9)(8, 9). The subarray (3,6)(3, 6), that is 6,8,5,76, 8, 5, 7, is not compressible, even though its values form the set {5,6,7,8}\{5, 6, 7, 8\}. The four queries contain 10+310 + 3, 7+17 + 1, 4+04 + 0 and 3+13 + 1 compressible subarrays, respectively.

Exemplul 2

Sample grader input

6 1
3 1 2 6 5 4
0 5

Sample grader output

12

Explicație

This is the sequence from the statement. Besides the 66 subarrays of length 11, the compressible subarrays are (0,2)(0, 2), (0,5)(0, 5), (1,2)(1, 2), (3,4)(3, 4), (3,5)(3, 5) and (4,5)(4, 5), so the answer is 1212.

Log In or Sign Up to be able to send submissions!