Data Structures19 sections · 729 units
Open in Course

Rectangle Area II Solution

Implement your solution

Implementation steps:

1.1. Coordinate compress yy-values

2.2. Create events: (x,type,y1,y2)(x, \text{type}, y_1, y_2) for each rectangle edge

3.3. Sort events by xx

4.4. Sweep: between events, add (covered length) ×\times (xx difference) to area

5.5. Update segment tree for the current event

The segment tree tracks how much of each yy-segment is covered. Nodes store cover count; query returns total covered length. This is an advanced problem combining sweep line, coordinate compression, and segment trees. Time: O(nlogn)O(n \log n). Space: O(n)O(n).