Background
During Facebook Hacker Cup Round 2 today, Problem D caught my attention. It reminded me of a particular data structure called a kinetic tournament, which is not very well known in this community. However, it offers an extremely clean and slick solution to the problem, in my opinion.
I first learned about this data structure from dragonslayerintraining, who described a variant of it in this Codeforces blog comment. Since the data structure is so interesting, I feel like it deserves a longer explanation, some template code, and more examples. That's why I am writing this blog post.
Kinetic Tournaments
Briefly, the functionality of the data structure is a mix between a line container, i.e., "convex hull trick", and a segment tree.
Suppose that you have an array containing pairs of nonnegative integers, $$$A[i]$$$ and $$$B[i]$$$. You also have a global parameter $$$T$$$, corresponding to the "temperature" of the data structure. Your goal is to support the following queries on this data:
- update(i, a, b): set $$$A[i] = \text a$$$ and $$$B[i] = \text b$$$
- query(s, e): return $$$\displaystyle \min_{s \leq i \leq e} A[i] * T + B[i]$$$
- heaten(new_temp): set $$$T = \text{new_temp}$$$
- [precondition: new_temp >= current value of T]
(For simplicity, we set A[i] = 0 and B[i] = LLONG_MAX for uninitialized entries, which should not change the query results.)
This allows you to essentially do arbitrary lower convex hull queries on a collection of lines, as well as any contiguous subcollection of those lines. This is more powerful than standard convex hull tricks and related data structures (Li-Chao Segment Tree) for three reasons:
- You can arbitrarily remove/edit lines, not just add them.
- Dynamic access to any subinterval of lines, which lets you avoid costly merge small-to-large operations in some cases.
- Easy to reason about and implement from scratch, unlike dynamic CHT.
The tradeoff is that you can only query sequential values (temperature is only allowed to increase) for amortization reasons, but this happens to be a fairly common case in many problems.
Here's the kicker. If you implement the data structure correctly, you get the following time complexities:
- query: $$$O(\log n)$$$
- update: $$$O(\log n)$$$
- heaten: $$$O(\log^2 n)$$$ [amortized]
(The runtime complexity might actually be $$$O(m \alpha(m) \log^2 n)$$$ instead of $$$O(m \log^2 n)$$$ if your updates include both adding and removing lines, and you're also very careful about constructing an example. See the discussion below from Elegia about Davenport-Schinzel sequences.)
Implementation
How does it work? With kinetic tournaments, you essentially build a min segtree as usual, but you add a global priority queue that stores whenever a "contract" breaks. We can put this in the analogy of increasing temperature. For every node, you store the temperatures at which that node "melts", meaning that the minimum value changes from one child to the other. Then, you can just keep popping events from this priority queue as your data structure heats up.
However, this unfortunately can be slow if you're not careful, which isn't good enough, especially if you have many concurrent lines (might give you quadratic runtime, depending on implementation of priority queue). Luckily, there's a neat implementation trick that circumvents the priority queue and prevents this possible quadratic runtime. In every node of the segment tree, you store the minimum temperature at which any part of its subtree could melt. This fits in naturally with the segment tree definition.
To optimize the data structure, you do not recompute at every certificate failure. Instead, whenever the user calls heaten(), you batch all of the recompute operations and recursively rebuild only the parts of the segment tree that changed, once.
The runtime analysis follows from comparing the data structure to computing a convex hull by divide-and-conquer (thanks dragonslayerintraining!). The number of times each node "melts" is bounded at most by the number of leaves in its subtree. This is because you can never have two lines, $$$A$$$ and $$$B$$$, such that $$$A(x_1) < B(x_1)$$$, $$$A(x_2) > B(x_2)$$$, and $$$A(x_3) < B(x_3)$$$, where $$$x_1 < x_2 < x_3$$$. In other words, as you increase temperature, the number of recalculations is at most $$$O(n \log n)$$$. Combining this with the fact that we have to walk down the segment tree, which has logarithmic depth, every time we do a recalculation, this yields a final amortized time complexity of $$$O(\log^2 n)$$$ for each "heaten" operation.
Code
You can find my implementation of a modified kinetic tournament here:
https://ekzlib.netlify.app/view/kinetic_tournament.cpp
It defines a single struct with template type (~100 LoC), which has a plug-and-play interface that you can use in your own solutions, and no global variable pollution. Example usage:
int N = 2;
kinetic_tournament<long long> kt(N);
kt.update(0, 2, 10); // 2t + 10
kt.update(1, 4, 1); // 4t + 1
kt.heaten(1); // t = 1
kt.query(0, 0); // => 2*1 + 10 = 12
kt.query(1, 1); // => 4*1 + 1 = 5
kt.query(0, 1); // => min(2*1 + 10, 4*1 + 1) = 5
kt.heaten(5); // t = 5
kt.query(0, 0); // => 2*5 + 10 = 20
kt.query(1, 1); // => 4*5 + 1 = 21
kt.query(0, 1); // => min(2*5 + 10, 4*5 + 1) = 20
I've verified this implementation on Problem D from Facebook Hacker Cup Round 2, titled "Log Drivin' Hirin'". In this problem, you can simply sort the queries and nodes in order of increasing "carelessness", and the kinetic tournament finishes the task. Full solution is available below. It runs in less than 5 seconds on the entire test input.
Conclusion
I hope to see more of this interesting data structure pop up in competitive programming! Hopefully this explanatory blog post is useful for you, and please let me know if you find any errors.