Rectangle Area II
From LeetCode. Union area with sweep line. Solve the geometry problem "Rectangle Area II".
Examples
Input: [1,2,3]
Output: 0
Input: [2,3,4]
Output: 0
Hints
Sweep a vertical line left to right across all rectangle edges. Between consecutive x-coordinates, the vertical cross-section of the union is a set of disjoint y-intervals whose total length is constant.
Process events at each x: add the y-interval [y1, y2] to the active set on a left edge, remove it on a right edge. After processing all events at a given x, merge active intervals to compute the total covered y-length.
The area between x[i] and x[i+1] is (x[i+1] - x[i]) * total_y_covered. Sort unique x-coordinates and do interval merging at each distinct x. For n ≤ 500, this is sufficient without a segment tree.
Rectangle Area II
**From LeetCode.** Union area with sweep line. Solve the geometry problem "Rectangle Area II".