You can apply two types of operations on a sequence of natural numbers:
- Choose two numbers with and set every value from to to .
- Choose two numbers with and add to every value from to .
For example, if we start with the sequence , we could apply the following sequence of operations:
- Apply operation 1 for . .
- Apply operation 2 for . .
- Apply operation 2 for . .
- Apply operation 1 for . .
Task
Given and a sequence of natural numbers. You are given queries of the form . What is the minimum number of operations needed to make ? After each query, the sequence will reset to its initial state.
Input data
The first line of the input file egale.in
contains two integers, and . The second line contains the sequence of natural numbers. The next lines each contain a triplet .
Output data
For each from to , the -th line of the output file egale.out
will contain a single integer, the answer to the -th query.
Constraints and clarifications
- for each from to
- and for each query
# | Points | Constraints |
---|---|---|
1 | 10 | for each query and |
2 | 15 | for each query |
3 | 19 | |
4 | 12 | |
5 | 25 | |
6 | 19 | No additional constraints |
Example 1
egale.in
11 5
1 2 1 2 1 0 5 2 3 1 6
1 4 1
1 4 2
1 11 0
1 11 4
6 9 5
egale.out
2
2
1
5
6
Explanation
For the first query, we need to find the minimum number of operations to make all elements in the sequence equal to . The only way to do this in 2 operations is to apply operation 1 to the entire sequence, followed by operation 2 to the entire sequence.
Example 2
egale.in
10 10
3 2 9 4 10 9 1 0 8 6
3 3 7
4 5 9
5 8 9
1 6 3
5 5 7
3 10 2
3 4 1
7 8 5
3 8 10
4 7 1
egale.out
8
10
10
4
8
3
2
5
11
2
Example 3
egale.in
5 4
0 1 0 0 1
1 1 0
1 2 0
1 3 0
3 4 0
egale.out
0
1
1
0
Explanation
This example checks for each query.
For the first query, all elements are already , so no operations are needed.
For the second query, we can apply operation 1 on the interval .