ACM ICPC World Finals 2017

Problem L: Visual Python++

Shortest judge solution: 1776 bytes. Shortest team solution (during contest): 1727 bytes.

Python solutions by the judges: only Pypy

This problem can be solved by a sweepline algorithm. Let us sweep from left to right, keeping a set of encountered but unmatched top left corners, ordered by r-coordinate. When we encounter a new corner:

Each event can be processed in O(log n) time so we have a time complexity of O(n log n).

However, we are not yet done. Even if this process finishes, the constructed rectangles may intersect. To check for this, we run essentially the same sweep again, but this time, since we know exactly how the rectangles look, we also check when adding and removing top left corners from our active set that adjacent pairs of rectangles in this set do not intersect.

Note that the solution, if it exists, is actually unique, but like with the Replicate problem, figuring this out is part of solving the problem.