I was trying to solve 2024 Argentinian Programming Tournament (TAP) problem H (https://codeforces.me/gym/105321) and got stuck on a subproblem, but the contest doesn't have an editorial.
I have N axis-aligned rectangles that don't intersect each other (not even at corners), but some can be nested inside others. I need to efficiently find the "parent" of each rectangle, meaning the smallest rectangle that completely contains it. The trivial approach would be O(N^2): for each rectangle, check all others to see which contain it, but this would be too slow. I was thinking about sweep line algorithms but I can't figure it out.
Is there a standard algorithm for this problem?








Someone posted a similar problem a few weeks ago. That is for convex polygons, but will give you some ideas. Although, I think it's way simpler for axis-aligned rectangles.
https://codeforces.me/blog/entry/144863
Something like this might work. I do not have time to put much thinking rn.
For each rectangle, mark the left side with red (R) and the right side with blue (B). Loop through the rectangles from right to left (sorted by right side). When processing a rectangle, check the first line to its right from any point on its B side.
The sweep can be maintained using a segment tree.