This page is still under construction.

Parts of this page are still being built. What you see may change.

Reorganizing Bus Routes

Interview

Time limit2sMemory limit1024 MB

Summary
Merge overlapping intervals into their union, keeping the cheaper fare, then output the surviving intervals sorted by start.
Level

Medium6 of 10

Topics
Sorting, Intervals, Greedy, Array
Solved
No attempts yet

Problem

Seogang operates NN bus routes along a straight road. Because routes were added whenever the need arose, many of them overlap or duplicate each other. To help citizens tired of the complicated bus network, the government decided to reorganize the routes.

Each bus route is described by three integers SS, EE, and CC, meaning that it covers the interval [S,E][S,E] with fare CC. If the intervals of two bus routes share at least one point, the two intervals are replaced by a single new route covering their union. The fare of the new route is the lower of the two fares. The reorganization continues until no two routes have overlapping intervals.

Figure D.1: Bus routes before and after reorganization

Given the information about the bus routes, write a program that outputs the routes after the reorganization finishes.

Input

The first line contains the number of bus routes NN. (1≤N≤200 0001 \le N \le 200\,000)

The next NN lines each contain three integers SS, EE, and CC describing one bus route. (0≤S<E≤1090 \le S < E \le 10^9, 1≤C≤1091 \le C \le 10^9)

Output

Print KK, the number of bus routes after the reorganization, on the first line.

On the next KK lines, print SS, EE, and CC for each route after the reorganization, in increasing order of SS.

Examples2

  1. Example 1

    Input
    6
    1 4 4
    6 9 5
    7 9 6
    2 5 7
    0 2 10
    9 10 10
    
    Expected output
    2
    0 5 4
    6 10 5
    
  2. Example 2

    Input
    5
    1 2 4
    3 7 3
    8 9 5
    10 14 10
    17 18 3
    
    Expected output
    5
    1 2 4
    3 7 3
    8 9 5
    10 14 10
    17 18 3