33OJ
登录 注册

#P17826. [COCI 2026/2027 #1] 水 / Voda

    ID: 20480 远端评测题 4000ms 512MiB 尝试: 0 已通过: 0 显示难度省选/NOI− 上传者: 标签>线段树COCI(克罗地亚)2026

[COCI 2026/2027 #1] 水 / Voda

Problem Description

In his yard, Marko has built a histogram out of wooden columns: nn columns of width 11 stand next to each other, and the column at position ii has height hih_i. Marko likes to change his work, so sometimes he raises, lowers or replaces some of the columns.

Sometimes Marko looks only at a part of the histogram, the columns at positions from ll to rr, and imagines a lot of rain falling on them. The columns outside the observed part do not exist at that moment, so water can flow away over the left edge of column ll and over the right edge of column rr. The rain falls until the histogram is full: water stays wherever it is trapped by higher columns on both sides, and all other water flows away. One 1×11\times 1 cell filled with water is one unit of water.

We say that a column is completely under water if there is water above its top. A column is not completely under water if its top is dry, that is, there is no water above it. Water may still touch such a column from the side, up to the height of its top.

Marko will make qq events in order, and each of them is of one of the following five types:

  • 1 i x - the height of column ii becomes xx,
  • 2 l r x - the heights of all columns from ll to rr become xx,
  • 3 l r x - the heights of all columns from ll to rr increase by xx (the number xx may also be negative),
  • 4 l r - Marko observes the columns from ll to rr and wants to know how many of them are not completely under water,
  • 5 l r - Marko observes the columns from ll to rr and wants to know how many units of water in total are held.

Help Marko and answer all of his questions. Observing does not change the histogram, and the water disappears after each question.

Input Format

The first line contains positive integers nn and qq (1≤n,q≤200 0001\le n,q\le 200\,000), the number of columns and the number of events.

The second line contains nn integers h1,h2,…,hnh_1,h_2,\ldots,h_n (0≤hi≤1090\le h_i\le 10^9), the initial heights of the columns.

Each of the following qq lines contains the description of one event in one of the forms given above. It holds that 1≤i≤n1\le i\le n and 1≤l≤r≤n1\le l\le r\le n. In events of types 11 and 22, 0≤x≤1090\le x\le 10^9, and in events of type 33, −109≤x≤109-10^9\le x\le 10^9.

It is guaranteed that the heights of all columns are integers between 00 and 10910^9 at all times.

Output Format

For each event of type 44 or 55, in the order they are given, output the answer to Marko's question in a separate line.

8 6
3 0 2 4 1 4 0 2
4 1 8
4 2 5
4 3 3
4 5 8
4 1 4
4 2 7
4
4
1
3
2
5
6 5
5 1 3 2 4 1
5 1 6
4 1 6
5 2 5
5 4 6
4 3 3
6
3
1
0
1
5 8
2 0 1 0 3
5 1 5
4 1 5
3 2 4 2
5 1 5
2 1 3 1
5 1 5
1 4 5
4 1 5
5
2
1
0
5

Hint

Clarification of the first example

The histogram is [3,0,2,4,1,4,0,2][3,0,2,4,1,4,0,2]. In the first question, Marko observes the whole histogram. Water is held above columns 22, 33, 55 and 77 (33, 11, 33 and 22 units, respectively), while the tops of columns 11, 44, 66 and 88 are dry. These four columns are not completely under water, so the answer is 44.

In the second question, Marko observes the columns [0,2,4,1][0,2,4,1]. The heights first increase and then decrease, so all the water flows away over the left or the right edge. No column is completely under water, and the answer is 44.

Clarification of the second example

The histogram is [5,1,3,2,4,1][5,1,3,2,4,1]. When Marko observes the whole histogram, the water between columns 11 and 55 stands up to height 44, and to the right of column 55 it all flows away. So 0,3,1,2,0,00,3,1,2,0,0 units of water are held above the columns, 66 in total. Columns 22, 33 and 44 are completely under water and the remaining three are not, so the answer to the second question is 33.

Scoring

Subtask Points Constraints
1 21 n,q≤5000n,q\le 5000.
2 16 There are only events of type 44.
3 11 There are only events of types 44 and 55.
4 13 There are only events of types 11 and 44.
5 14 There are only events of types 11, 44 and 55.
6 16 There are only events of types 11, 22, 33 and 44.
7 19 No additional constraints.