Imagine only one point. There are three possible rectangles: below the point, to the left of it and to the right of it.
Say now there are two points. Let's look at what's closer to the bottom. Again, there are three cases at this point. There is below the rectangle. There is a rectangle on the side opposite to another point. And there is a side with a different point, where there are more rectangles.
IOW, if you look at the points from bottom to top, then each point has a rectangle at the bottom, and this point divides the space into two vertical stripes, each of which has other points that also have rectangles under them and which further divide the space.
Thus, it would be wise to sort the points both by Y and by X (possibly by making them nodes of two sorted trees, sorted by Y (so you can move upwards by Y), and another, sorted by X (so you can move / look left / right)), which is N * log (N), and then recursively visit all points from bottom to top, appropriately dividing the space that looks like another N * log (N).
source share