Explanation
Let's expand the rule for .
Let's call the added value and the 's alternating sum .
Note that for each query, adding to an even-length segment won't make a difference because they'll cancel themselves. Otherwise, will differ by (it's not canceled by the following element).
This means that we can precompute in time.
What about ? We can compute each segment of elements with two pointers, since 's values never change.
However, checking every value of will surely TLE. Instead, we can notice that since we're calculating absolute value, we only care about the two closest elements on either side of it. Thus, we'll binary search on the alternating sums of .
Implementation
Time Complexity:
C++
#include <algorithm>#include <iostream>#include <vector>using std::vector;int main() {int n;int m;int q;std::cin >> n >> m >> q;
Join the USACO Forum!
Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!