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

#CC0000074. Xếp những viên gạch dài (Long Bricks)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Xếp những viên gạch dài (Long Bricks)

Long Bricks

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

There are WW adjacent cells, initially all at height 00. Place NN bricks in order. Brick ii covers every cell from LiL_i through RiR_i, inclusive, and rests on the highest surface within its range. Output the top height of each brick after placing it.

Input

The first line contains W,NW,N, followed by NN lines containing Li,RiL_i,R_i.

Output

Print NN lines; line ii is the top height of the ii-th brick.

Subtasks

  • Subtask 1 (20 points): 1≤N≤201\le N\le 20; 2≤W≤1002\le W\le 100; 1≤Li≤Ri≤W1\le L_i\le R_i\le W.
  • Subtask 2 (30 points): 1≤N≤90001\le N\le 9000; 2≤W≤90002\le W\le 9000; 1≤Li≤Ri≤W1\le L_i\le R_i\le W.
  • Subtask 3 (50 points): 1≤N≤2500001\le N\le 250000; 2≤W≤5000002\le W\le 500000; 1≤Li≤Ri≤W1\le L_i\le R_i\le W.

Examples

Input

3 5
1 2
2 2
2 3
3 3
1 2

Output

1
2
3
4
4

Explanation

The first brick reaches height 11, the second 22, the third 33, and the fourth 44. For the fifth brick, the maximum height over cells 1,21,2 is 33, so its top becomes 44.