City Horizon
InterviewTime limit1sMemory limit128 MB
Given N axis-aligned rectangles that all rest on the ground, compute the area of their union.
- Level
Medium7 of 10
- Topics
- Segment tree, Divide and conquer, Sorting, Geometry
- Solved
- No attempts yet
Problem
Farmer John has taken his cows on a trip to the city! As the sun sets, the cows gaze at the city horizon and admire the beautiful silhouettes formed by the rectangular buildings.
The entire horizon is represented by a number line, and buildings stand along it (). Building 's silhouette has a base that spans the horizon from position to () and has height (). Every building rests on the ground ().
Determine the area, in square units, of the combined silhouette formed by all buildings — that is, the area covered by the union of all the rectangles. Overlapping regions must be counted only once.
Input
- Line 1: A single integer
- Lines 2 to : Line describes building with three space-separated integers , , and .
Output
- Line 1: The total area, in square units, of the silhouette formed by all buildings.
Hint
In the example above, the first building overlaps with the fourth building over an area of square unit. Counting the overlap only once, the total area is .