EGOI 2026 Editorial - Fox Families
Task Author: Tomohisa Hachiya, Yuki Tanaka
In this problem, you are give a graph with nodes representing intervals. Two nodes are connected connected if and only if the corresponding intervals are contained in each other. The task is to track the number of connected components while the graph is extended with new intervals step by step.
Subtask 1
In this subtask, we can construct and maintain the graph explicitly. Whenever a new interval is added, we go through all already existing intervals and check the containment condition. For every containment found, we add a new edge to the graph. After updating the graph this way, we compute the number of connected components using depth first search (DFS) after each update.
Since the number of containments is
Subtask 2
In this subtask, we can maintain the connected components using a disjoint set union (DSU) data structure, initialized with
Subtask 3
The solution for this subtask is similar to Subtask 2: we will also maintain the connected components using a DSU. However, we can not afford checking all other intervals whenever adding a new one.
Instead, the key observation here is that for each interval, there is
only a small number of other intervals that could contain it or be
contained in it. To find all connected intervals for some
Thus, we store all already-added intervals by their left endpoint and run the solution of Subtask 2, with the faster containment check that only checks the close intervals after each update.
Runtime:
Subtask 4
From now on, we will no longer use a DSU data structure, but instead exploit the special structure of the graph.
In this subtask, containment of intervals
Therefore, it suffices to store the largest right endpoint of each component: This is enough to check whether a newly added interval will join this component.
The solution then works as follows: maintain a sorted stack of
largest right endpoints (one for each component). Whenever a new
interval arrives, check whether it is contained in the component on the
top of the stack (by checking
Since every interval will be pushed to the stack once and popped at most once, the total runtime of this approach is
Full Solution
For the full solution, we generalize the idea from the previous subtask to two dimensions.
An interval
Thus, for every component, it suffices to keep track of four numbers: the minimum right endpoint (minR), maximum right endpoint (maxR), minimum left endpoint (minL) and the maximum right endpoint (maxL). Then, a new interval
We maintain a C++ set with all components, sorted by their minimal left endpoint. When adding a new interval
For the first step, we search in the set for the first component with a minimal left endpoint that is at least
Symmetrically, for the second step, we repeatedly search for the maximal right endpoint that is less than
At the end, we insert the newly merged component into the set. The number of components is then the size of the set.
In Python, the solution is more involved since there is no primitive set. One possibility is to manually implement a binary search tree.