name
k-Maximum Subsequence Sum
descirption
time limit per test 4 seconds
memory limit per test 256 megabytes
Consider integer sequence $a_1, a_2, …, a_n$. You should run queries of two types:
The query format is “0 i val”. In reply to this query you should make the following assignment: $a_i$ = val.
The query format is “1 l r k”. In reply to this query you should print the maximum sum of at most k non-intersecting subsegments of sequence $a_l, a_{l + 1}, …, a_r$. Formally, you should choose at most k pairs of integers $(x_1, y_1), (x_2, y_2), …, (x_t, y_t) (l ≤ x_1 ≤ y_1 < x_2 ≤ y_2 < … < x_t ≤ y_t ≤ r; t ≤ k)$ such that the sum $a_{x_1
} + a_{x_1 + 1} + … + a_{y_1} + a_{x_2} + a_{x_2 + 1} + … + a_{y_2} + … + a_{x_t} + a_{x_t + 1} + … + a_{y_t}$ is as large as possible. Note that you should choose at most k subsegments. Particularly, you can choose 0 subsegments. In this case the described sum considered equal to zero
input
The first line contains integer $n (1 ≤ n ≤ 10^5)$, showing how many numbers the sequence has. The next line contains n integers a1, a2, …, an (|ai| ≤ 500).
The third line contains integer $m (1 ≤ m ≤ 10^5)$ — the number of queries. The next m lines contain the queries in the format, given in the statement.
All changing queries fit into limits: 1 ≤ i ≤ n, |val| ≤ 500.
All queries to count the maximum sum of at most k non-intersecting subsegments fit into limits: 1 ≤ l ≤ r ≤ n, 1 ≤ k ≤ 20. It is guaranteed that the number of the queries to count the maximum sum of at most k non-intersecting subsegments doesn’t exceed 10000.
output
For each query to count the maximum sum of at most k non-intersecting subsegments print the reply — the maximum sum. Print the answers to the queries in the order, in which the queries follow in the input.
sample input
1 | 9 |
sample output
1 | 17 |
sample input
1 | 15 |
sample output
1 | 14 |
hint
In the first query of the first example you can select a single pair (1, 9). So the described sum will be 17.
Look at the second query of the first example. How to choose two subsegments? (1, 3) and (7, 9)? Definitely not, the sum we could get from (1, 3) and (7, 9) is 20, against the optimal configuration (1, 7) and (9, 9) with 25.
The answer to the third query is 0, we prefer select nothing if all of the numbers in the given interval are negative.
toturial
先考虑k=1的情况, 我么你可以直接用线段树来维护,这是一个经典问题,但是当k>1的时候,我们可以这样来做,我们做k次下诉操作,取出最大字段和,然后将这一段数乘以-1,直到最大字段和为负或者执行了k次操作,如此我们就能得到最大k字段和。正确性可以用费用流来证明。
code
1 |
|