#P13963. [ICPC 2023 Nanjing R] 接雨水

    ID: 13911 远端评测题 5000ms 1024MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>线段树2023颜色段均摊(珂朵莉树 ODT)分块ICPC吉司机线段树 segment tree beats单调栈南京

[ICPC 2023 Nanjing R] 接雨水

Description

There is a histogram represented by an integer sequence a1,a2,,ana_1, a_2, \cdots, a_n of length nn. For the ii-th bar from left to right, its height is aia_i and its width is 11.

We'll perform qq modifications to the histogram. The ii-th modification can be represented by a pair of integers (xi,vi)(x_i, v_i) indicating that we'll increase the height of the xix_i-th bar by viv_i.

After each modification, answer the following query: Calculate how much water this histogram can trap if a heavy rain pours onto it and fills all the pits as much as possible.

More formally, given an integer sequence a1,a2,,ana_1, a_2, \cdots, a_n of length nn, the ii-th modification will increase axia_{x_i} by viv_i. After each modification, answer the following query: Let fi=max(a1,a2,,ai)f_i = \max(a_1, a_2, \cdots, a_i) and gi=max(ai,ai+1,,an)g_i = \max(a_i, a_{i + 1}, \cdots, a_n), calculate

$$\sum\limits_{i = 1}^n \left( \min(f_i, g_i) - a_i \right)$$

Input Format

There are multiple test cases. The first line of the input contains an integer TT indicating the number of test cases. For each test case:

The first line contains an integer nn (1n1051 \le n \le 10^5) indicating the number of bars in the histogram.

The second line contains nn integers a1,a2,,ana_1, a_2, \cdots, a_n (1ai1061 \le a_i \le 10^6) where aia_i indicates the initial height of the ii-th bar.

The third line contains an integer qq (1q1051 \le q \le 10^5) indicating the number of modifications.

For the following qq lines, the ii-th line contains two integers xix_i and viv_i (1xin1 \le x_i \le n, 1vi1061 \le v_i \le 10^6) indicating that the ii-th modification increases the height of the xix_i-th bar by viv_i.

It is guaranteed that neither the sum of nn nor the sum of qq of all test cases will exceed 10610^6.

Output Format

For each modification output one line containing one integer indicating how much rain water this histogram can trap.

2
6
1 2 3 4 5 6
2
1 2
3 3
5
100 10 1 10 100
1
3 100
1
4
180