Let be a sequence of distinct integers. A bucket is a set of values. Consider the following process:
- Start with buckets placed in a row, where the bucket at position is , for .
- While there is more than one bucket, choose any two adjacent buckets and such that is a set of consecutive integers, and replace them, in the same place in the row, with the single bucket . If no such pair of buckets exists, the process stops.
The sequence is called compressible if the choices in step can be made so that the process ends with a single bucket.
For example, and can be merged, and so can and , but and cannot. The sequence is compressible:
The sequence is not compressible, even though its values form the set : no two adjacent buckets can be merged in the first iteration of step , therefore the process stops.
Task
You are given a sequence of distinct integers and queries of the form , with . For each query, find the number of pairs with such that the subarray 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);
- : the length of the sequence;
- : the number of queries;
- : a vector of length , where for ;
- , : two vectors of length , where query is , for ;
- the function must return a vector of length , whose element at index is the answer to query ;
- 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 : ;
- line : ;
- line (): .
The sample grader calls solve and prints the returned values to standard output, one per line.
The grader used for evaluation may differ from the sample grader.
Restrictions
- .
- , and the values are distinct.
- for every query.
- The sequence is called a mountain if there is a position , , such that .
| # | Points | Restrictions |
|---|---|---|
| 0 | 0 | Examples. |
| 1 | 2 | , the query is , . |
| 2 | 3 | , the query is , . |
| 3 | 4 | , the query is , . |
| 4 | 8 | , the query is , the sequence is a mountain. |
| 5 | 9 | , the query is , the sequence is compressible. |
| 6 | 11 | , the query is . |
| 7 | 4 | |
| 8 | 6 | |
| 9 | 2 | |
| 10 | 13 | The sequence is a mountain. |
| 11 | 17 | The sequence 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 subarrays of length , the only compressible subarrays are , and . The subarray , that is , is not compressible, even though its values form the set . The four queries contain , , and 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 subarrays of length , the compressible subarrays are , , , , and , so the answer is .