egale

Time limit: 0.4s Memory limit: 256MB Input: egale.in Output: egale.out

You can apply two types of operations on a sequence x1,x2,xkx_1, x_2, \dots x_k of natural numbers:

  1. Choose two numbers (i,j)(i, j) with 1ijk1 \leq i \leq j \leq k and set every value from ii to jj to 00.
  2. Choose two numbers (i,j)(i, j) with 1ijk1 \leq i \leq j \leq k and add 11 to every value from ii to jj.

For example, if we start with the sequence x=[1,3,0,2,3]x = [1, 3, 0, 2, 3], we could apply the following sequence of operations:

  1. Apply operation 1 for (2,3)(2, 3). x=[1,0,0,2,3]x = [1, 0, 0, 2, 3].
  2. Apply operation 2 for (1,3)(1, 3). x=[2,1,1,2,3]x = [2, 1, 1, 2, 3].
  3. Apply operation 2 for (4,5)(4, 5). x=[2,1,1,3,4]x = [2, 1, 1, 3, 4].
  4. Apply operation 1 for (1,3)(1, 3). x=[0,0,0,3,4]x = [0, 0, 0, 3, 4].

Task

Given nn and a sequence a1,a2,ana_1, a_2, \dots a_n of natural numbers. You are given QQ queries of the form l,r,vl, r, v. What is the minimum number of operations needed to make al=al+1=al+2==ar=va_l = a_{l+1} = a_{l+2} = \dots = a_r = v? After each query, the sequence aa will reset to its initial state.

Input data

The first line of the input file egale.in contains two integers, nn and QQ. The second line contains the sequence a1,a2,ana_1, a_2, \dots a_n of natural numbers. The next QQ lines each contain a triplet l,r,vl, r, v.

Output data

For each ii from 11 to QQ, the ii-th line of the output file egale.out will contain a single integer, the answer to the ii-th query.

Constraints and clarifications

  • 1n,Q1000001 \leq n, Q \leq 100\,000
  • 0ai2000 \leq a_i \leq 200 for each ii from 11 to nn
  • 1lrn1 \leq l \leq r \leq n and 0v2000 \leq v \leq 200 for each query
# Points Constraints
1 10 v=0v = 0 for each query and n,Q1000n, Q \leq 1\,000
2 15 v=0v = 0 for each query
3 19 1n,Q10001 \leq n, Q \leq 1\,000
4 12 ai1a_i \leq 1
5 25 ai10a_i \leq 10
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 [1,2,1,2][1, 2, 1, 2] equal to 11. 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 v=0v = 0 for each query.

For the first query, all elements are already 00, so no operations are needed.

For the second query, we can apply operation 1 on the interval (1,2)(1, 2).

Log in or sign up to be able to send submissions!