A - 魚 3 (Fish 3) 解説
by
zyc212303
User Editorial
Hereafter, we will refer to “feeding \(A\) food” as “performing \(A\) operations” and feeding \(B\) food as the same.
Consider how brute force works. Assume that all \(B\) operations are performed after operation \(A\). Clearly, an \(A\) operation is equivalent to making some \(C_i\leftarrow C_i-D\), and the final sequence \(C\) that can be generated by \(B\) operations must be monotonically nondecreasing after all of the \(A\) operations (since it can be given a suffix \(+1\) at a time, the preceding elements will never be greater than the following ones).
Consider another kind of brute force, which is to take the query offline, sort it by \(R\) in descending order, and sweep through it, each time sweeping to a new \(R\), and if \(C_{R-1}>C_R\), then update it forward until the condition is satisfied.
This brute force above looks good for optimization. Observe that if we have updated a pair of neighboring elements, then their difference remains unchanged no matter how we update them next, because each update is \(-D\) to a \(C_i\), and it’s enough to consider the remaining coefficients of \(\bmod D\). So use a stack to maintain the position that has not yet been updated (because if the right doesn’t have to update, the left doesn’t have to update), so each time from the top of the stack to start down to check whether there is a conflict, and if so, update the value of the middle section, and then delete the top of the stack…… This operation is done at most \(N\) times. Adding a number to an interval and querying the interval sum can be maintained using lazy segment trees; the time complexity is \(O(N\log N)\).
投稿日時:
最終更新:
