For this problem, it definitely has to be done offline first; online there is definitely no way to do it. In fact, we can split the matches into three kinds: on the ascending chain, crossing the LCA, or on the descending chain. These three can be counted separately. The ascending and descending chains can be handled by building Aho–Corasick automata on the query strings forward and reversed, and then doing a DFS on the original tree; the chains through the LCA are a bit hard to handle, but we notice that the number of characters that can take part in a match is related to \(|\text{query string}|\), so we splice together the two parts of the left and right chains and then just run KMP on it separately.
Continue reading →Chinese original · English translation by Claude Opus 5.5
This problem looks like it's \(\Theta(n^3)\). At a glance I thought of setting up \(dp[i][a][b][c]\) for the transitions, but it turns out that not only does it not fit in memory, it also needs extra time.
Consider designing \(dp[a][b][c]\), where \(a, b, c\) are the latest times \(R, G, B\) appear; clearly \(i = \max(a, b, c)\). With this, we can go through the requirements and then do the transitions.
Continue reading →Chinese original · English translation by Claude Opus 5.5
This way of handling it is really neat. According to yyb's blog, this technique is called “divide and conquer on the maximum”.
We can consider designing the state \(dp[i][j]\) as the number of ways for length \(i\) with maximum value not exceeding \(j\). Then we can enumerate a \(k\) as the position of \(j\), and force this \(k\) to be the rightmost \(j\) in this segment. Written out concretely, the transition is:
FFS, I went completely off track. When I first started thinking about it, I thought it was about building a rooted tree in which every root-to-leaf path is increasing.
Later I realized that this graph might actually be a DAG, and with a tree it might not work out because of the limit on K. So we can just do a path cover and find the minimum cost.
Continue reading →Chinese original · English translation by Claude Opus 5.5
One read of the statement and I knew it was going to be yet another classic string heavy-industry problem; in the end it took me 6.3KB.
First, building the SAM and then setting up binary lifting + segment trees to maintain the endpos sets is obviously necessary. Then we start thinking about what this pair means.
I spent a while thinking about how to count it directly, and found that the condition for this thing to hold is an OR, so counting directly can only be done with subset inclusion–exclusion. So we consider inverting the condition, turning it into a strict AND form, and then just subtracting from the total number of pairs, \({n - 1 \choose 2}\).
Continue reading →Chinese original · English translation by Claude Opus 5.5
It's hard to see much at the macro level, so we can consider the contribution of each edge separately.
First, this graph is a cactus, so we need to consider the contributions of cycle edges and tree edges separately. The contribution of a tree edge is easy to handle: it is simply \((2^{siz_u} - 1) \times (2^{siz_v} - 1)\). Cycle edges are a bit more troublesome: we need to enumerate the minimum arc length that contributes, and then, taking some vertex as the starting point, compute the answer from the pairwise products of the sizes of the outgoing edges. Prefix-sum optimization saves one dimension of the transition complexity.
Continue reading →Chinese original · English translation by Claude Opus 5.5
At first I came up with a bogus one, and only after I finished writing it did I realize where it went wrong, which was pretty idiotic.
First, greedily, we know that the \(i\)-th smallest number must end up at position \(K + i - 1\). When computing the answer, we find it is nothing more than splitting the contribution according to how the absolute value breaks apart and handling the parts separately: one part is \((a_i - rk_i) - (k - 1)\), and the other is \((k - 1) - (a_i - rk_i)\). So we need a data structure to split these two parts.
Continue reading →Chinese original · English translation by Claude Opus 5.5