· Olympiad in Informatics

Lagrange Interpolation

If you have a bunch of scattered points on hand, then you can fit a function with this formula:

\[f(x) = \sum_{i = 1}^n y_i \prod_{j \neq i}^n \frac{x - x_j}{x_i - x_j}\]

Continue reading →Chinese original · English translation by Claude Opus 5.5

· Olympiad in Informatics

NOI Review Topic - Data Structures

Classic problem: HDU4085

Continue reading →Chinese original · English translation by Claude Opus 5.5

· Olympiad in Informatics

NOI Review Topic - DP

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

· Olympiad in Informatics

P5161: WD and Sequences — Solution

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

· Olympiad in Informatics

[Codeforces 724F] Uniformly Branched Trees — Solution

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

· Olympiad in Informatics

[Codeforces 566C] Logistical Questions — Solution

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

· Olympiad in Informatics

[Codeforces 526F] Pudding Monsters — Solution

Transformed a bit, this problem is: given a permutation, count how many intervals there are whose numbers can form a consecutive sequence.

Continue reading →Chinese original · English translation by Claude Opus 5.5

· Olympiad in Informatics

[Codeforces 341D] Iahub and Xors — Solution

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:

\[d_{i, j} = a_{i, j} \oplus a_{i - 1, j} \oplus a_{i, j - 1} \oplus a_{i - 1, j - 1}\]

Continue reading →Chinese original · English translation by Claude Opus 5.5

· Olympiad in Informatics

AtCoder Grand Contest 033 - Problem-Solving Report

A one-glance problem: just run a BFS to find the longest shortest path.

Continue reading →Chinese original · English translation by Claude Opus 5.5

· Olympiad in Informatics

Virtual Tree

A flexible data structure for performing computations on a set of key vertices in a tree.

Continue reading →Chinese original · English translation by Claude Opus 5.5