PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#GD0000080. Bao phủ tối thiểu (Minimal Coverage)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Bao phủ tối thiểu (Minimal Coverage)

Minimal Coverage

Source: UVa

Version: Phuoc Hung OJ Extended

Problem Statement

You are given a positive integer MM and nn closed integer-endpoint segments [Li,Ri][L_i,R_i]. Choose the smallest number whose union covers the entire continuous interval [0,M][0,M]. Segments may extend outside the target. Output 0 if covering is impossible; otherwise give the minimum count and the coordinates of selected segments in ascending left-end order. Multiple optimum covers may exist.

Input

The first line contains M,nM,n. Each of the next nn lines contains segment endpoints Li,RiL_i,R_i.

Output

Print 0 if no cover exists. Otherwise output the minimum number kk, followed by kk selected pairs Li,RiL_i,R_i in nondecreasing left-end order.

Subtasks

  • Subtask 1 (20 points): 1≤M≤50001\le M\le 5000; 1≤n≤151\le n\le 15; −50000≤Li≤Ri≤50000-50000\le L_i\le R_i\le50000.
  • Subtask 2 (30 points): 1≤M≤50001\le M\le 5000; 1≤n≤30001\le n\le 3000; −50000≤Li≤Ri≤50000-50000\le L_i\le R_i\le50000.
  • Subtask 3 (50 points): 1≤M≤50001\le M\le 5000; 1≤n≤1051\le n\le10^5; −50000≤Li≤Ri≤50000-50000\le L_i\le R_i\le50000.

Examples

Input

10 4
-1 4
3 8
8 12
5 9

Output

3
-1 4
3 8
8 12

Explanation

Choose [−1,4][-1,4], [3,8][3,8], and [8,12][8,12]. Their union covers [0,10][0,10]. No pair among the four input segments covers the whole target, so three is optimal.