Rectangles Covering

后端 未结 12 1184

I have N rectangles with sides parallel to the x- and y-axes. There is another rectangle, model. I need to create an algorithm that can tell whether the

12条回答
  •  [愿得一人]
    2020-12-14 08:23

    Here's how to make a sweepline work in O(n lg n). I will focus on the tricky part of how the BST works.

    Keep a balanced BST that contains the start and end of each rectangle that intersects the current sweepline. Each node of the BST contains two auxiliary fields: minOverlap and deltaOverlap. The field minOverlap generally stores the minimum number of rectangles overlapping any point in the interval covered by the subtree of that node. However, for some nodes the value is slightly off. We maintain an invariant that minOverlap plus the sum of deltaOverlap for every node up to the root has the true minimum number of rectangles overlapping a region in the subtree of the node.

    When we insert a rectangle-starting node, we always insert at a leaf (and possibly rebalance). As we traverse down the insertion path we "push down" any non-zero deltaOverlap values to the children of the access path of the inserted node, updating the minOverlap of the nodes on the access path. Then, we need to increment every node to the 'right' of the inserted node in the tree in O(lg n) time. This is accomplished by incrementing the minOverlap field of all the right ancestors of the inserted node and incrementing the deltaOverlap of all the right children of the right ancestors of the inserted node. An analogous process is carried out for the insertion of the node that ends the rectangle, as well as the deletion of points. A rebalancing operation can be performed by modifying only the fields of the nodes involved in the rotation. All you have to do is check the root at each point in the sweep to see if the minOverlap is 0.

    I've left out details of handling things like duplicate coordinates (one simple solution is just to order the open-rectangle nodes before any close-rectangle nodes of the same coordinate), but hopefully it gives you the idea, and is reasonably convincing.

提交回复
热议问题