Hello, Codeforces!
"The Struggle" is a problem I authored which appeared in the HDU Multi-university Training, the Ptz Summer Camp and the Open Cup. Despite appearing in contests where there are a total of ~1300 three people teams, I know of few (possibly no more than 5) people who have learned and independently implemented the solution.
The problem is pretty much fun and the solution is quite easy to implement (actual implementation < 2kb). From this blog you will easily learn how the algorithm works, how to implement effortlessly and you will become able to solve this OpenCup problem that few people is able to solve, up till today!
The problem statement is very simple: Given an ellipsoid $$$E$$$ that is contained in $$$(0,4 \times 10^6) \times (0,4 \times 10^6)$$$, calculate the value
over all integer points $$$(x,y)$$$.
The algorithm complexity of this question is $$$O(n \log n)$$$, where $$$n = \max_{(x,y) \in E} \max(x,y)$$$. Consider using the FWT algorithm for calculation. We consider xor convolution which can only process one at a time a$[x\times2^n,x\times2^n+2^n-1] \times [y\times2^n,y\times2^n+2 ^n-1]$ square. We first process all the largest squares that are all inside the ellipse, and then process the next largest squares, and so on...
But the complexity of this algorithm is $$$O(n \log^2 n)$$$, which is not fast enough. Consider optimizing this algorithm. The method is to perform FWT from the bottom up, and calculate the squares that need to be calculated at each layer. After calculating the inner product of FWT array, we should not calculate the inverse FWT, but should "accumulate" it on the result array. (See author's solution for better understanding)
One issue in the complexity analysis of this question is to prove that the sum of the side lengths of all squares is $$$O(n \log n)$$$. This fact can be proved on the condition that the border function is a monotone function, and the boundary of the ellipse can be split into four monotone functions. The idea of the proof is to see that the y-intervals corresponding to each x-interval must be a constant plus some "extra" intervals, and for x-coordinate intervals of the same size, the total length of the "extra y-intervals" cannot exceed $$$n$$$. Since there is only $$$\log n$$$ sizes for x-intervals, the proof is done.