F - I hate Shortest Path Problem Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

H+1 マス横 W マスのマス目があります。

あなたは一番上のいずれかのマスから始めて右か下に一マス移動することを繰り返します。ただし、1 以上 H 以下のそれぞれの整数 i について、グリッドの上から i マス目の左から A_i マス目、A_i + 1 マス目、\ldotsB_i マス目のマスからは下に移動することができません。

1 以上 H 以下のそれぞれの整数 k について、上から k+1 マス目のいずれかのマス目まで移動するための最小の移動回数を求めてください。(出発するマスは各場合について個別に選ぶことができます。) 一番上のどのマスから始めても上から k+1 マス目のいずれのマス目にも移動できない場合、代わりに -1 を出力してください。

制約

  • 1 \leq H,W \leq 2\times 10^5
  • 1 \leq A_i \leq B_i \leq W

入力

入力は以下の形式で標準入力から与えられる。

H W
A_1 B_1
A_2 B_2
:
A_H B_H

出力

H 行出力せよ。i 行目には、k=i のときの答えを出力せよ。


入力例 1

4 4
2 4
1 1
2 3
2 4

出力例 1

1
3
6
-1

上から i マス目、左から j マス目のマスをマス (i,j) とします。

k=1 のとき、マス (1,1)(2,1) のように 1 回で移動できます。

k=2 のとき、マス (1,1)(2,1)(2,2)(3,2) のように 3 回で移動できます。

k=3 のとき、マス (1,1)(2,1)(2,2)(3,2)(3,3)(3,4)(4,4) のように 6 回で移動できます。

k=4 のとき、上から 5 マス目のマスに移動する方法はありません。

Score : 600 points

Problem Statement

There is a grid of squares with H+1 horizontal rows and W vertical columns.

You will start at one of the squares in the top row and repeat moving one square right or down. However, for each integer i from 1 through H, you cannot move down from the A_i-th, (A_i + 1)-th, \ldots, B_i-th squares from the left in the i-th row from the top.

For each integer k from 1 through H, find the minimum number of moves needed to reach one of the squares in the (k+1)-th row from the top. (The starting square can be chosen individually for each case.) If, starting from any square in the top row, none of the squares in the (k+1)-th row can be reached, print -1 instead.

Constraints

  • 1 \leq H,W \leq 2\times 10^5
  • 1 \leq A_i \leq B_i \leq W

Input

Input is given from Standard Input in the following format:

H W
A_1 B_1
A_2 B_2
:
A_H B_H

Output

Print H lines. The i-th line should contain the answer for the case k=i.


Sample Input 1

4 4
2 4
1 1
2 3
2 4

Sample Output 1

1
3
6
-1

Let (i,j) denote the square at the i-th row from the top and j-th column from the left.

For k=1, we need one move such as (1,1)(2,1).

For k=2, we need three moves such as (1,1)(2,1)(2,2)(3,2).

For k=3, we need six moves such as (1,1)(2,1)(2,2)(3,2)(3,3)(3,4)(4,4).

For k=4, it is impossible to reach any square in the fifth row from the top.