#P16702. [MCO 2026] 雨水收集

    ID: 19032 远端评测题 5000ms 1024MiB 尝试: 0 已通过: 0 显示难度省选/NOI− 上传者: 标签>线段树分块2026MCC/MCO(马来西亚)

[MCO 2026] 雨水收集

Problem Description

In the town of MCO, there are NN towers standing side by side. From left to right, the initial height of the ii-th tower (indexed from 00) is HiH_i. After a heavy rain, water may accumulate on top of the towers. Evirir, a resident of MCO, wants to know how much rainwater these towers can collect in total.

For an interval of towers [l,r][l, r] (i.e., towers l,l+1,,rl, l+1, \ldots, r), its rainfall amount is defined as follows:

  • For each tower jj, if and only if there exist towers ii and kk such that lijkrl \le i \le j \le k \le r, and both tower ii and tower kk are at least xx higher than tower jj, i.e.,$$H_i - H_j \ge x \quad \text{and} \quad H_k - H_j \ge x,$$then a column of water of height x0x \ge 0 can be stored on tower jj.
  • Let f(j)f(j) be the maximum possible height of the water column that can be stored on tower jj.
  • The rainfall amount is defined asf(l)+f(l+1)++f(r), f(l) + f(l+1) + \cdots + f(r), i.e., the sum of the maximum water column heights that can be stored on these towers.

Evirir is confident in the new generation of Malaysian OI contestants, so if you were only asked to compute the rainfall amount for one interval, that would be too easy. Instead, you need to process QQ operations, and each operation is one of the following two types:

  • Update: 0 l r x0\ l\ r\ x --- add xx to HiH_i for all lirl \leq i \leq r.
  • Query: 1 l r1\ l\ r --- output the rainfall amount of the tower interval [l,r][l, r].

Notes:

  • When answering a query for interval [l,r][l, r], when computing f(i)f(i) and the rainfall amount, you must not consider towers outside this interval. Towers outside the interval cannot be used to hold water.
  • Tower heights can be negative, but the rules remain the same. See the sample for related explanation.

Input Format

The first line contains two integers NN and QQ separated by spaces.

The second line contains NN integers H0,H1,,HN1H_0, H_1, \ldots, H_{N-1} separated by spaces.

The next QQ lines each describe one operation, containing several integers separated by spaces:

  • Update: 0 l r x0\ l\ r\ x --- add xx to HiH_i for all lirl \le i \le r.
  • Query: 1 l r1\ l\ r --- output the rainfall amount of the tower interval [l,r][l, r].

Output Format

For each query, output the rainfall amount of the towers in interval [l,r][l, r] in order, one answer per line.

9 7
5 3 1 3 -1 1 2 5 3
1 1 6
1 0 8
0 1 4 2
0 6 8 -4
1 1 6
1 3 6
1 6 6
6
21
2
0
0
5 6
-2 3 1 4 2
0 0 2 1
0 0 4 3
0 3 4 8
0 0 0 10
0 1 3 1
1 0 4
10

Hint

Hint

Sample 1\underline{Sample\ 1}

This sample applies to subtasks 1, 5, and 6.

There are N=9N = 9 towers. Below is a visualization of the updates and queries:

:::align{center} :::

In the first query 1 1 6\texttt{1 1 6}, we consider towers 11 to 66. Look at tower j=5j = 5 with height 11. Tower 55 can store a water column of height 11, because:

  • Tower i=3i = 3 has height 33, which is 22 higher than tower 55.
  • Tower k=6k = 6 has height 22, which is 11 higher than tower 55. But tower 55 cannot store a water column of height 22, because there is no tower kk satisfying jk6j \le k \le 6 whose height is at least 22 higher than tower 55 (i.e., height at least 1+2=31 + 2 = 3). Note that you cannot take k=7k = 7, because kk is not within the queried interval [1,6][1, 6]. Therefore, f(5)=1f(5) = 1, represented by the 11 unit of water on tower 55.

In the second query 1 0 8\texttt{1 0 8}, we consider towers 00 to 88. Look at tower j=4j = 4 with height 1-1. Tower 44 can store a water column of height 66, because the heights of tower i=0i = 0 and tower k=7k = 7 are both 55, both 66 higher than tower 44. It can also be proven that 66 is already the maximum possible height, so f(4)=6f(4) = 6.

In the update 0 1 4 2\texttt{0 1 4 2}, the heights of towers 11 to 44 are all increased by 22. In the update 0 6 8 -4\texttt{0 6 8 -4}, the heights of towers 66 to 88 are all decreased by 44.

In the query 1 6 6\texttt{1 6 6}, note that even if a tower has a negative height, it still needs taller surrounding towers to store water.

Note that by taking i=j=ki = j = k, it is always possible to store at least a water column of height 00 on a tower.

Sample 2\underline{Sample\ 2}

This sample applies to subtasks 1, 5, and 6.

Scoring

For all test cases, the input satisfies the following Constraints:

  • 1N51061 \le N\leq 5 \cdot 10^6
  • 1Q51041 \le Q \leq 5 \cdot 10^4
  • For all 0iN10 \le i \le N - 1, Hi107|H_i| \leq 10^7
  • For all updates and queries, 0lrN10 \le l \le r \le N - 1
  • For all updates, x107|x| \leq 10^7
  • There is at least one query operation.
Subtask Points Additional Constraints
11 88 N,Q1000N, Q \leq 1000
22 Q=1Q = 1
33 1616 N106N \leq 10^6 and there are no update operations in the input
44 1818 In updates, l=rl = r and x>0x > 0, and in queries, [l,r]=[0,N1][l, r] = [0, N - 1]
55 2525 N5105N \leq 5 \cdot 10^5
66 ---

Translated by ChatGPT 5