Slope optimization actually isn't very hard. You can think of it as taking some points in the plane as decision points; you now have a line with slope \(k\), and you need to make it pass through a suitable point so that the line's intercept is maximized/minimized. Clearly this can be done by making these points form an upper/lower convex hull, then binary searching for the segment with the closest slope and transitioning from it.
Continue reading →Chinese original · English translation by Claude Opus 5.5
First take differences, and then solve this non-overlapping substring matching problem.
We can consider maintaining the endpos sets with segment trees, and then use small-to-large merging to compute the contribution produced between a suffix node and the current point set. There are two cases:
Substrings that have the current longest length for the endpos and do not overlap the current position \(x\). For these we can just brute-force a count query on the segment tree.
Lengths less than \(maxdep\). Taking the case where \(endpos\) is on the left as an example: the contribution to the answer is \(x - endpos - 1\).
Continue reading →Chinese original · English translation by Claude Opus 5.5
Ah, an unrooted-tree DP again. First split on parity: if it's odd, you can DP directly; if not, you need to do inclusion–exclusion in one go.
Define the state \(dp[scale][maxPart][degree]\), meaning the number of ways for a tree of size \(scale\) whose largest subtree has size \(maxPart\) and whose root has degree \(degree\). The transitions are actually easy to come up with; just read the code.
Continue reading →Chinese original · English translation by Claude Opus 5.5
This problem is really godlike. First, note that this function is unimodal, and the cost function formed by multiple points in the end must have the form \(y = ax^{\frac{3}{2}} + b\), which is clearly also unimodal. So, when the tree is a chain, we can try binary search. If it evolves into a tree, we can consider centroid decomposition, observing that among the subtrees currently connected to the root, at most one subtree is better.
Continue reading →Chinese original · English translation by Claude Opus 5.5
First, seeing this problem, you could consider a tree of trees, and then you're done.
Let's consider using a 2D Fenwick tree to take differences. Of course, if we take differences the usual way, all we can think of is using prefix sums to maintain point values, not range XOR sums. Let the result after differencing be: