#P17826. [COCI 2026/2027 #1] 水 / Voda
[COCI 2026/2027 #1] 水 / Voda
Problem Description
In his yard, Marko has built a histogram out of wooden columns: columns of width stand next to each other, and the column at position has height . 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 to , 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 and over the right edge of column . 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 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 events in order, and each of them is of one of the following five types:
1 i x- the height of column becomes ,2 l r x- the heights of all columns from to become ,3 l r x- the heights of all columns from to increase by (the number may also be negative),4 l r- Marko observes the columns from to and wants to know how many of them are not completely under water,5 l r- Marko observes the columns from to 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 and (), the number of columns and the number of events.
The second line contains integers (), the initial heights of the columns.
Each of the following lines contains the description of one event in one of the forms given above. It holds that and . In events of types and , , and in events of type , .
It is guaranteed that the heights of all columns are integers between and at all times.
Output Format
For each event of type or , 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 . In the first question, Marko observes the whole histogram. Water is held above columns , , and (, , and units, respectively), while the tops of columns , , and are dry. These four columns are not completely under water, so the answer is .
In the second question, Marko observes the columns . 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 .
Clarification of the second example
The histogram is . When Marko observes the whole histogram, the water between columns and stands up to height , and to the right of column it all flows away. So units of water are held above the columns, in total. Columns , and are completely under water and the remaining three are not, so the answer to the second question is .
Scoring
| Subtask | Points | Constraints |
|---|---|---|
| 1 | 21 | . |
| 2 | 16 | There are only events of type . |
| 3 | 11 | There are only events of types and . |
| 4 | 13 | There are only events of types and . |
| 5 | 14 | There are only events of types , and . |
| 6 | 16 | There are only events of types , , and . |
| 7 | 19 | No additional constraints. |