#P6242. 【模板】线段树 3(区间最值操作、区间历史最值)

【模板】线段树 3(区间最值操作、区间历史最值)

Description

Given a sequence AA of length nn, we also define an auxiliary array BB. Initially, BB is exactly the same as AA. Then mm operations are performed. There are five types of operations, given in the following format:

  • 1 l r k: For all i[l,r]i\in[l,r], add kk to AiA_i (kk can be negative).
  • 2 l r v: For all i[l,r]i\in[l,r], set AiA_i to min(Ai,v)\min(A_i,v).
  • 3 l r: Compute i=lrAi\sum_{i=l}^{r} A_i.
  • 4 l r: For all i[l,r]i\in[l,r], compute the maximum value of AiA_i.
  • 5 l r: For all i[l,r]i\in[l,r], compute the maximum value of BiB_i.

After each operation, we perform an update: Bimax(Bi,Ai)B_i\gets\max(B_i,A_i).

Input Format

The first line contains two positive integers n,mn,m, representing the length of sequence AA and the number of operations.

The second line contains nn integers A1,A2,,AnA_1,A_2,\cdots,A_n, representing the sequence AA.

The next mm lines each start with an integer opop, representing the operation type. It is followed by two or three integers as the operation parameters. The format is described in the Description section.

Output Format

For operations with op{3,4,5}op\in\{3,4,5\}, output one line containing one integer, which is the answer to the query.

5 6
1 2 3 4 5
3 2 5
1 1 3 3
4 2 4
2 3 4 1
5 1 5
3 1 4

14
6
6
11

Hint

Sample Explanation #1

Operation Count Input Operation Sequence Output
0 1,2,3,4,51,2,3,4,5
1 3 2 5 Find the sum of all numbers in [2,5][2,5] 14
2 1 1 3 3 Add 33 to all numbers in [1,3][1,3] 4,5,6,4,54,5,6,4,5
3 4 2 4 Find the maximum of all numbers in [2,4][2,4] 6
4 2 3 4 1 Replace all numbers in [3,4][3,4] with the minimum of itself and 11 4,5,1,1,54,5,1,1,5
5 5 1 5 Find the maximum among the historical maximum values over [1,5][1,5] 6
6 3 1 4 Find the sum of all numbers in [1,4][1,4] 11

Constraints

  • For test points 1,21,2, n,m5000n,m\leq 5000.
  • For test points 3,43,4, op{1,2,3,4}op\in\{1,2,3,4\}.
  • For test points 5,65,6, op{1,3,4,5}op\in\{1,3,4,5\}.
  • For all testdata, it is guaranteed that 1n,m5×1051\leq n,m\leq 5\times 10^5, 5×108Ai5×108-5\times10^8\leq A_i\leq 5\times10^8, op[1,5]op\in[1,5], 1lrn1 \leq l\leq r \leq n, 2000k2000-2000\leq k\leq 2000, and 5×108v5×108-5\times10^8\leq v\leq 5\times10^8.

Note

The input size of this problem is large. Please use a reasonable and efficient input method.

Translated by ChatGPT 5