#C5745. Simplify the Skyline
Simplify the Skyline
Simplify the Skyline
You are given a list of buildings, each represented by a triplet (l, r, h) where l is the left x-coordinate, r is the right x-coordinate, and h is the height of the building. Buildings may overlap. Your task is to simplify the skyline by merging the overlapping buildings and, for each merged interval, maintain the maximum height seen in that interval.
The merging rule is as follows: if two buildings overlap (i.e. the left coordinate of a building is less than or equal to the current building's right coordinate), merge them into a single building whose:
- left coordinate is the left coordinate of the first building,
- right coordinate is the maximum of both right coordinates, and
- height is the maximum of the heights over the overlapping region, i.e. \( \max(h_1, h_2) \).
Output the simplified skyline as a sequence of building segments. If there are no buildings, output nothing.
inputFormat
The input is read from standard input (stdin) and has the following format:
N l1 r1 h1 ... lN rN hN
Where:
N
is a non-negative integer representing the number of buildings.- Each subsequent line contains three space-separated integers:
l
(left coordinate),r
(right coordinate), andh
(height).
If N
is 0, then there are no buildings.
outputFormat
Output the simplified skyline to standard output (stdout) in the following format. For each merged segment, output a line with three space‐separated integers: the left coordinate, right coordinate, and the maintained maximum height.
If there are no segments, output nothing.
## sample3
1 3 4
2 5 3
6 8 5
1 5 4
6 8 5
</p>