Problem Author: | Anton Trygub |
Problem Prepared by: | Andrii Stolitnii, Kostiantyn Denysov |
Editorial Written by: | Kostiantyn Lutsenko |
10pt
Block 1
Ans = R - L + 1 - (L%2==0) - (R%2==0) + ((L==R)&&(L%2==0))
Block 2
One can iterate over all 2^n subsequences and check if they are good by calculating all prefix and suffix sums. One can calculate the answer for each of \frac{n(n+1)}{2} possible queries by iterating over all its subsequences. Complexity is \mathcal{O}(2^n\cdot n).
Block 3
Let \sum [L, R] denote the sum on the subsegment . Let's say \sum[L, R] \stackrel{def}{=} 0 for L > R (empty segment).
Let \sum[1, n] = S.
The definition of a good array is rewritten as \sum[1, a] \geqslant 0, \sum[a, n] \geqslant 0. But \sum[a, n] = \sum[1, n] - \sum[1, a-1]. Hence, the definition of a good array can be written as
\sum[1, a] \in [0, S]
Hence, we can calculate dp[position][current sum][maximum prefix sum encountered], where the dynamic value is the maximum number of elements (or -INF if the state is unreachable). Complexity is \mathcal{O}(q n^3).
Also, note that the optimal answer is obtained from the given segment by removing some number of minus ones. Our goal is to determine how many minus ones we need to remove.
Block *
Statement. An array is good if and only if all sums of its subsegments do not exceed the sum of the entire array.
Proof:
: Contrarily, suppose all segments have a sum less than , but the array is not good. Since the array is not good, there exists either its prefix or suffix with a negative sum. Without loss of generality, let it be a prefix, i.e., there exists such that \sum[1, R] < 0. But then \sum[R+1, n] = S - \sum[1, R] > S, implying the segment has a sum greater than the sum of the entire array.
: Contrarily, suppose the array is good, but there exists \sum[L, R] > \sum[1, n]. Therefore \sum[1, n] - \sum[L, R] = \sum[1, L-1] + \sum[R+1, n] < 0, so at least one of the numbers \sum[1, L-1] or \sum[R+1, n] is negative since their sum is negative. Hence, the array is not good.
Returning to the task — we need to determine the minimum number of «-1» that need to be removed so that sums on all subsegments do not exceed the sum on the entire array.
Let \sum[1, n] = S_0. Let be the subsegment with the maximum sum (if there are several, any of them). Let \sum[L, R] = M_0. We want to make S = M, but initially, we have M_0 \geqslant S_0.
With one removal of minus one, increases by 1, and does not decrease. Hence, the difference M - S decreases by no more than 1. Thus, we need to remove at least M_0-S_0 elements.
We will prove that removing M_0-S_0 elements is always sufficient. Let \sum[1, L-1] = -P and \sum[R+1, n] = -Q. Note that M_0-S_0 = \sum[L, R] - \sum[1, n] = -(\sum[1, L-1] + \sum[R+1, n]) = P + Q. Note also that P, Q \leqslant 0 (i.e., \sum[1, L-1] \leqslant 0 and \sum[R+1, n] \leqslant 0), otherwise could be expanded with an increase in the sum.
Let \sum'[L, R] be the sum on the subsegment after our removal. We want all sums \sum'[1, a] and \sum'[a, n] to be non-negative by removing P+Q minus ones.
To do this, we will remove some minus ones on the prefix [1, L-1], and some minus ones on the suffix [R+1, n]. Which ones exactly will be determined later. Then \sum'[1, L-1] = \sum[1, L-1] + P = -P+P = 0, similarly \sum'[R+1, n] = 0.
Notice that \sum[a+1, L-1] \leqslant 0, otherwise \sum[a+1, R] = \sum[L, R] + \sum[a+1, L-1] > \sum[L, R], and we designated as the subsegment with the largest sum.
Notice that \sum[L, a] \geqslant 0, otherwise \sum[a+1, R] = \sum[L, R] - \sum[L, a] > \sum[L, R], and we designated as the subsegment with the largest sum.
Let us determine what additional conditions are necessary to make the result array good:
a < L:
There is a condition \sum'[1, a] \geqslant 0.
L \leqslant a \leqslant R:
\sum'[1, a] = \sum'[1, L] + \sum'[L, a] = 0 + \sum'[L, a] \geqslant 0,
so this is automatically fulfilled.
a > R:
\sum'[1, a] = \sum'[1, L-1] + \sum'[L, R] + \sum'[R+1, a] =
= 0 + M_0 + \sum'[R+1, a] = M_0 + \sum'[R+1, n] - \sum'[a+1, n] =
=M_0 + 0 - \sum'[a+1, n],
so there is a condition \sum'[a+1, n] \leqslant M_0.
Thus, it is necessary and sufficient for all a < L to satisfy \sum[1, a] \in [0, M_0], and also a similar condition on the suffixes, i.e., \sum[a, n] \in [0, M_0] for all a > R. This can be achieved, for example, by greedily removing minus ones: we go through increasing from to L-1 and, if the prefix sum becomes negative, we remove the current minus one. Then all prefix sums will become non-negative. Suppose that by removing minus one at position it turns out that \sum'[1, b] became greater than M_0 + 1 for some b < L. Note that at this moment only the prefix sums to the right of changed, i.e., b \geqslant a. But \sum'[1, a] at this moment became equal to . So \sum'[a+1, b] = \sum'[1, b] - \sum'[1, a] = M_0 + 1. But on this segment, we changed nothing, i.e.
\sum'[a+1, b] = \sum[a, b] = M_0 + 1 > M_0 = \sum[L, R].
But we chose M_0 as the maximum sum on a subsegment. Therefore, this cannot be, hence all prefix sums fall within the required limits. Similarly, we do with suffixes.
It is also worth noting that we indeed removed exactly minus ones, i.e., the sum over the entire prefix became zero. Indeed, if when removing minus one at position it turns out that \sum'[1, L-1] > 0, then \sum[a+1, L-1] = \sum'[a+1, L-1] = \sum'[1, L-1] - \sum'[1, a] = \sum'[1, L-1] - 0 > 0, and we proved that sums on segments of the form [a, L-1] are always non-negative.
Thus, we reduced the problem to finding M_0, S_0 and outputting the quantity R - L + 1 - (M_0 - S_0).
Block 4
The analysis of this and the following blocks describes how to find M = \max\limits_{l\leqslant L \leqslant R \leqslant r}\sum[L, R]. As shown above, this solves the problem.
Iterate over . We need to choose such that \sum[L, R] is the maximum, i.e., \sum[1, L-1] is the minimum. We can maintain this simultaneously with iterating over .
Complexity:
Blocks 5-6
Let node[L, R] be a quadruple of values:
— the sum on this segment;
— the maximum prefix sum on this segment;
— the maximum suffix sum on this segment;
— the maximum sum of some subsegment of this segment.
Then we can calculate node[L, R] through node[L, a] and node[a+1, R] for some : let
(S_1, A_1, B_1, M_1) + (S_2, A_2, B_2, M_2) = (S, A, M, B),
then:
S = S_1 + S_2;
A = \max(A_1, S_1 + A_2);
B = \max(B_2, S_2 + B_1);
M = \max(M_1, M_2, B_1 + A_2).
If you really want to submit these blocks but not the next, you can use sqrt decomposition for \mathcal{O}(q\sqrt{n}). For this, divide the array into blocks of length (where C \sim \sqrt{n}) and precompute node[kC, (k+1)C-1]. Then for each search query to find the answer, you can perform merges described above, and for update queries, you need to recompute the sum for the changed block in .
Block 7
The merge described in the previous block can be computed in the segment tree, where each node stores the numbers S, A, B, M for the corresponding segments. The get query will also use this merge and return a quadruple of numbers, from which we will take and and output the quantity R - L + 1 - (M - S).