The \(\Theta(n^2)\) brute force is still pretty easy to write: first enumerate the horizontal line, then handle above and below separately; in both cases we enumerate from left to right, but we keep two pointers \(lptr, rptr\), and along the way we can maintain that the number of colors within \([lptr, rptr]\) always stays strictly less than \(k\), then update the answer with the number of points.
The intended solution, though, is not so easy to write. We consider enumerating one excluded color among the \(k\), then split into cases:
Continue reading →Chinese original · English translation by Claude Opus 5.5
Range division and square root are about handling the following two operations:
\(\forall i \in [l, r], \lfloor \frac{a_i}{d} \rfloor \to a_i\)
\(\forall i \in [l, r], \lfloor \sqrt{a_i} \rfloor \to a_i\)
These operations are very hard to implement with ordinary lazy tags, but we can transform them a bit, because within a certain range of values they can be processed in bulk. In other words, if the spread of values in the interval is small, then division or square root has the same effect on the whole interval, so it can be turned into a subtraction.
Continue reading →Chinese original · English translation by Claude Opus 5.5
First build the tree. With a bit of thought you should be able to work out: use binary lifting to find the marked point, then do a range update from the root. As it turns out, the intended solution is written this way too, it's just that there are a lot of details, so I'll mention them here: when answering query \(x\), first bring his father up to the root, then find the nearest point; if one is found, tag everything with greater depth to flip it all.
Continue reading →Chinese original · English translation by Claude Opus 5.5
This problem is just a textbook problem of finding the dominator tree of a DAG. The process goes like this: first do a topological sort; then for the \(i\)-th vertex \(u\), the first \(i - 1\) vertices are already done, so its parent in the dominator tree is the LCA of the start points of its incoming edges. A little bit of handling and that's it. Then run a DFS on the dominator tree to get the size of each subtree, and you're done.
Continue reading →Chinese original · English translation by Claude Opus 5.5
I thought about it for ages, and when I finally looked, it turned out to be maintained offline. I straight up coughed up blood.
First, you can probably figure out that XORing the XOR sum of the distinct values with the range XOR sum gives the answer. Because this filters out the ones that occur an odd number of times and gives the final answer. Consider how to get the XOR sum of the distinct values in a range: one way is to maintain it with a persistent segment tree, the other is to maintain it offline. We can maintain the prefix XOR of distinct values in a Fenwick tree: we stuff each number into the nearest spot possible, and at that point we need a map to decide whether it has to be moved.
Continue reading →Chinese original · English translation by Claude Opus 5.5
Time has gone by so fast. It feels like last year's me, shut down in a hotel as I finished writing the 2018 summary, was only a short while ago. This year, I changed, more or less, in every respect.
Continue reading →Chinese original · English translation by Claude Opus 5.5
When I first saw the divide-and-conquer tag, I made up my mind to solve it with divide and conquer, but before long I found the information extremely hard to make use of and gave up on it. Let's consider solving this problem with a method like the Aho–Corasick automaton, which is similar to KMP.
First, let \(dp[i]\) be the answer for those starting at \(i\) and extending to the right; then when we go left we can merge these, and we set \(nxt[i]\) to jump with. Since doing the process of getting the \(nxt\) array by brute force would make the time complexity GG, we can merge the information with a Map in a way similar to the Aho–Corasick automaton: swap in the Map at position \(nxt[i]+1\) and set the current position. Finally, just do the DP.
Continue reading →Chinese original · English translation by Claude Opus 5.5