Problem Description
Given multiple axis‐aligned rectangles (each specified by the bottom-left and top-right coordinates), compute the total area covered by these rectangles (i.e. the union of all rectangle areas). Since rectangles may overlap, any overlapping area should be counted only once. Return the computed area modulo 10^9+7.
Key Insights
- Use a line sweep algorithm along the x-axis to break the problem into vertical slices.
- For each vertical slice, compute the union of the active y-intervals.
- Apply coordinate compression on the y-values since they can be very large.
- Utilize a segment tree (or an equivalent data structure) to efficiently track and update the length of y-intervals that are currently active.
- Process two event types per rectangle: one when entering (adding a y-interval) and one when leaving (removing a y-interval).
Space and Time Complexity
Time Complexity: O(n log n) – sorting events and processing them using the segment tree. Space Complexity: O(n) – for storing events, the list of unique y-coordinates, and the segment tree structure.
Solution
The solution involves sweeping a vertical line across the x-axis and handling events where a rectangle starts or ends. For each rectangle, generate two events: one for adding the corresponding y-interval when the sweep line hits the left edge, and one for removing the interval when hitting the right edge. The y-coordinates are compressed to allow efficient segment tree operations. The segment tree keeps track of the coverage count and the total covered length on the y-axis. For each segment between consecutive x-values, multiply the width of the segment (delta in x) by the current union length of y-intervals to accumulate the area. Finally, return the accumulated area modulo 10^9+7.