Data Structures19 sections · 729 units
Open in Course

Rectangle Area: Sweep Line

Sweep with segment tree

Sweep a vertical line from left to right. At each event:

1.1. Rectangle start (left edge): add its yy-interval to active set

2.2. Rectangle end (right edge): remove its yy-interval from active set

Between consecutive events, the covered area is: (distance to next event) ×\times (total covered yy-length). Use a segment tree with lazy propagation to track the covered yy-length as intervals are added/removed. Time: O(nlogn)O(n \log n). Space: O(n)O(n).