All problems

Problem 688

The Skyline Problem

Hard · 30 pts · Heap, Sweep Line, Divide and Conquer

Problem

Each building is a rectangle on flat ground given by (L, R, H): it covers x from L to R and has height H. The skyline is the outline of all buildings seen from far away. Describe it by its key points: each is (x, y) where the outline height changes to y at position x, sorted by x; the last key point has y = 0. Consecutive key points never share the same height. Sweep the x coordinates of all edges, keeping the active heights in a max-heap with lazy deletion.

Input

The first line contains N. Each of the next N lines contains L, R and H for one building.

Output

Print the key points, one per line, as x and y separated by a space.

Constraints

1 <= N <= 10000; 0 <= L < R <= 2^31 - 1; 1 <= H <= 2^31 - 1

Example input

5
2 9 10
3 7 15
5 12 12
15 20 10
19 24 8

Example output

2 10
3 15
7 12
12 0
15 10
20 8
24 0

Explanation

For buildings (2,9,10), (3,7,15), (5,12,12), (15,20,10) and (19,24,8), the outline rises to 10 at x = 2, to 15 at 3, drops to 12 at 7, to 0 at 12, rises to 10 at 15, drops to 8 at 20 and to 0 at 24.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Implement Trie (Prefix Tree) Next: Rectangle Area II