PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FT0000214. Tổng sau khi chặn giá trị (Clamp)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Tổng sau khi chặn giá trị (Clamp)

Clamp

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

You are given an integer sequence A=(A1,A2,…,AN)A=(A_1,A_2,\ldots,A_N), with positions numbered from 11 to NN. Process QQ queries in order. Each query has one of the following forms:

  • 1 x y: Replace the value at position xx with yy. All other positions remain unchanged.
  • 2 l r: For every current element AiA_i, compute max⁡(l,min⁡(r,Ai))\max(l,\min(r,A_i)), and output the sum of these results over i=1,2,…,Ni=1,2,\ldots,N.

In this expression, min⁡(r,Ai)\min(r,A_i) selects the smaller of rr and AiA_i; max⁡(l,⋅)\max(l,\cdot) then selects the larger of ll and that result.

The input does not guarantee l≤rl\le r. In particular, if l>rl>r, each summand equals ll. Only a type-1 query changes AA; a type-2 query does not modify the sequence.

Input

  • The first line contains N,QN,Q: the array length and the number of queries.
  • The second line contains exactly NN integers A1,A2,…,ANA_1,A_2,\ldots,A_N.
  • Each of the following QQ lines is either 1 x y or 2 l r, as described above. Position xx is 11-based.

Output

For every type-2 query, print one line containing ∑i=1Nmax⁡(l,min⁡(r,Ai))\sum_{i=1}^{N}\max(l,\min(r,A_i)), evaluated using the current sequence. Preserve the order of type-2 queries.

Subtasks

  • Subtask 1 (20 points): 1≤N≤401\le N\le 40; 1≤Q≤401\le Q\le 40; 0≤Ai≤5⋅1050\le A_i\le 5\cdot10^5 for every 1≤i≤N1\le i\le N; type 1 has 1≤x≤N1\le x\le N and 0≤y≤5⋅1050\le y\le 5\cdot10^5; type 2 has 0≤l,r≤5⋅1050\le l,r\le 5\cdot10^5 (without requiring l≤rl\le r).
  • Subtask 2 (30 points): 1≤N≤30001\le N\le 3000; 1≤Q≤30001\le Q\le 3000; 0≤Ai≤5⋅1050\le A_i\le 5\cdot10^5 for every 1≤i≤N1\le i\le N; type 1 has 1≤x≤N1\le x\le N and 0≤y≤5⋅1050\le y\le 5\cdot10^5; type 2 has 0≤l,r≤5⋅1050\le l,r\le 5\cdot10^5 (without requiring l≤rl\le r).
  • Subtask 3 (50 points): 1≤N≤5000001\le N\le 500000; 1≤Q≤2000001\le Q\le 200000; 0≤Ai≤5⋅1050\le A_i\le 5\cdot10^5 for every 1≤i≤N1\le i\le N; type 1 has 1≤x≤N1\le x\le N and 0≤y≤5⋅1050\le y\le 5\cdot10^5; type 2 has 0≤l,r≤5⋅1050\le l,r\le 5\cdot10^5 (without requiring l≤rl\le r).

Examples

Input

3 4
4 3 2
1 1 7
2 3 5
1 2 0
2 4 2

Output

11
12

Explanation

Initially, A=(4,3,2)A=(4,3,2). Query 1 1 7 changes the first element to 77, giving (7,3,2)(7,3,2). For query 2 3 5, the contributions are max⁡(3,min⁡(5,7))=5\max(3,\min(5,7))=5, max⁡(3,min⁡(5,3))=3\max(3,\min(5,3))=3, and max⁡(3,min⁡(5,2))=3\max(3,\min(5,2))=3. Their sum is 5+3+3=115+3+3=11, giving the first output line.

Next, 1 2 0 changes the sequence to (7,0,2)(7,0,2). The final query is 2 4 2, where l=4>r=2l=4>r=2. For any element, min⁡(2,Ai)≤2<4\min(2,A_i)\le2<4, so each clamped contribution is 44. Since there are N=3N=3 elements, the sum is 4+4+4=124+4+4=12, giving the second output line.