It'd be easier to think about the problem if the marbles were simply points rather than circles with nonzero radii. It turns out this is actually an equivalent problem. We can imagine scaling everything up by a factor of (10^{100}) as our first action. Now there will always be space for the marbles to move past each other, so we don't need to worry about their radii. Once we achieve the desired positions, we can scale everything back down as the last step.
The question that remains is whether the two polygons containing each marble are "topologically equivalent." We will get to what this means later.
For each edge in a hull, we will store which hull it is part of, and whether it's on the upper or lower half of that hull.
We can observe that the relationships between enclosed polygons forms a tree. We'll have to build this structure and be able to find the polygon that contains a given point. We can use a line sweep from left to right, maintaining a sorted set (S) of all of edges spanning the current (x) coordinate, sorted by increasing (y) coordinates. During insertion/deletion, (S) uses a custom comparator that evaluates each line inside at the current (x). Note that as lines never cross, this relative order will be maintained at every (x), and so the set will always be well-ordered despite dynamically changing (x) within the comparator logic.
In the offline version of this problem, we'll also add all query points to the set. When our sweep line reaches the start or end of a line, we'll add it to or remove it from (S). When we get to a query point (or the leftmost line in a rubber band), we use a floor/lower bound search on (S) (again with a custom comparator by (y) value) to find the line immediately below us:
- If the line below us is in the lower convex hull of its rubber band, this means it is our parent, and this polygon's parent is ours as well.
- If the line below us is part of an upper hull, then it must be our sibling. We can set our parent to its parent. Note that since we are sweeping from bottom to top, we must have already encountered this line, and so its parent is guaranteed to have already been determined.
Having computed the tree structure of all nodes and which node corresponds to each query point, all that remains is checking whether two nodes (u) and (v) are topologically equivalent. Hashing would seem appropriate here. Let us define the subtree hash of a node as a constant (fixed globally) combined with its children's subtree hashes using an associative hash function.
Some wrong ideas include only requiring the subtree hashes of (u) and (v) to be equal, or only requiring the subtree hashes of children of the lowest common ancestor of (u) and (v) to be equal. Instead, it is important for us to us to also check that the nodes are topologically-equivalently-situated with respect to their ancestors' structures as well.
Namely, we can define the root-path hash of each node to be the root-path hash of its parent joined non-commutatively with the node itself's subtree hash. Two nodes are topologically equivalent if and only if their root-path hashes are equal. First we'll compute all subtree hashes for all nodes from the bottom up using DFS. Then, the root-path hash can be computed for each node using another DFS, where we pass the root-path hash of each parent downwards.
The sweep line algorithm will run in (\mathcal{O}((V + Q) \log V)) where (V) is the total number of polygon vertices, while building the graph and hashing all nodes will take (\mathcal{O}(N)). After this is done, each query can be answered in (\mathcal{O}(1)) time.