Problem Link
Here is my approach:
Suppose for the $$$i$$$th operation the value is $$$y_i$$$ for $$$x_i$$$ coordinate then for every $$$x_j$$$ > $$$x_i$$$ their $$$y$$$ coordinate value gets updated to $$$2y_i-y_j$$$. So, the value for $$$y$$$ gets updated in a fashion $$$2y_1-2y_2+2y_3... - y_0$$$ for $$$x_0$$$. I can't think of a way to implement this alternating sign change pattern. Kindly help.








$$$O(q^2) $$$ should easily pass.
No it doesn't; there are a lot of test cases and the input is actually huge
Then you can easily do it with this: https://judge.yosupo.jp/problem/range_affine_range_sum
I got AC in 7234 ms, so I think I can help you with this question.
Main idea: Notice that the difference between two elements will always be either 1 or -1, so we can transform the problem in the following way: given a list A = [1, 1, 1, 1, ..., 1], you have two operations:
Multiply all the values from [x,N] by -1 (the reflection).
Get the cumulative sum from [0,x-1].
You can do that with a lazy segtree. The lazy segtree updates by setting a node as the negative value, if there is an update on top of another we don't update and we merge nodes by summing them. Something like that:
The above code runs in 14296 ms, really close to the time limit. I reimplemented my segtree without some of the abstractions and it ran in 7234 ms, so the ideia should be around this.
Oh, I forgot to comment: allocating this segtree for every test case is too costly, because it has size
MAX_N = 1e5 + 5, so instead we undo the operations. We can do that by saving the operations and undoing in reverse order (technically it could be in any order, as multiplying by -1 is commutative).Wrong, no need to undo, creating segtree of 1e5 size on each testcase is fine (6640 ms my time)
And no need to transform the problem to the difference array, just work with the initial (multiply suffix by -1 and add 2 * f(x) to it).