This page is still under construction.

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

Seat Assignment

Interview

Time limit0.8sMemory limit32 MB

Summary
Assign seats left to right, each time giving the seat to the still-unseated request with the smallest right endpoint that covers it.
Level

Medium5 of 10

Topics
Greedy, Sorting, Heap
Solved
No attempts yet

Problem

A big match is played next week. The stadium has NN seats, numbered 11 through NN. A fan who requests a ticket also states the range of seats the fan is willing to sit in. The range is given by two integers FF and LL, and the fan accepts any seat SS with F≤S≤LF \le S \le L.

The ticket office has received MM requests. Two fans cannot get the same seat. Compute the largest number of requests that can be satisfied at the same time, and print a seat assignment for that many requests.

Input

The first line contains the number of seats NN (1≤N≤1000001 \le N \le 100000) and the number of requests MM (1≤M≤10000001 \le M \le 1000000), separated by a space.

Each of the next MM lines contains one request as two integers FF and LL (1≤F≤L≤N1 \le F \le L \le N). Requests are numbered 11 through MM in the order they appear in the input.

Output

Print the largest number of selected requests KK on the first line.

On each of the next KK lines print one seat assignment, in increasing order of seat number. Each line holds the seat number SS and the number RR of the request that gets seat SS, separated by a space.

Several assignments reach the maximum, so only the assignment fixed by the following rule is accepted. Scan the seats from 11 to NN. For the seat SS under consideration, collect the requests that have no seat yet and satisfy F≤S≤LF \le S \le L. If there is at least one such request, give seat SS to the one with the smallest LL. If two or more of them share the same LL, give the seat to the one with the smallest request number. If there is no such request, leave seat SS empty.

This rule always reaches the largest possible number of satisfied requests.

Examples4

  1. Example 1

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

    Input
    1 1
    1 1
    
    Expected output
    1
    1 1
    
  3. Example 3

    Input
    3 4
    2 2
    2 2
    2 2
    2 2
    
    Expected output
    1
    2 1
    
  4. Example 4

    Input
    5 2
    3 3
    5 5
    
    Expected output
    2
    3 1
    5 2