Problem:
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.

Solution in Java (OpenJDK 11.0.6) (judge verdict: Time Limit Exceeded)

import java.util.*;
import java.io.PrintWriter;

public class Main {
    public static void main(String[] args) throws Exception {
        // Your code here!
        Scanner sc = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);
        int h = sc.nextInt();
        int w = sc.nextInt();
        
        // 問題:
        // 1. (H+1)*Wのboardがある
        // 2. 最初(0,0)(0,1)...(0,W)に人がいる
        // 3. 右or下に進める
        // 4. i行目には区間[L,R]の壁があり下には行けない
        
        // ans[i]: i行目に到達する人のうち、最小の歩数
        
        // map(now, from): 最初from列にいて、今now列にいる人らの集合
        SortedMap<Integer, Integer> map = new TreeMap<Integer, Integer>();
        for (int i = 0; i < w; i++) {
            map.put(i, i);
        }
        
        // gaps: mapの(now-from)の集合
        TreeMap<Integer, Integer> gaps = new TreeMap<Integer, Integer>();
        for (Integer key : map.keySet()) {
            int val = map.get(key);
            int gap = key-val;
            
            int cnt = gaps.getOrDefault(gap, 0);
            gaps.put(gap, ++cnt);
        }
        
        for (int i = 1; i <= h; i++) {
            int L = sc.nextInt()-1;
            int R = sc.nextInt()-1;
            
            // submap: 壁で下に行けない人の集合(R+1列目のまっすぐ行ける人も含む)
            // (submapは区間[l,r)なので、[L,R+1]は[l,r+2))
            SortedMap<Integer, Integer> submap = map.subMap(L, R+2);
            
            // (次の行,R+1)に最小で行ける人は、submapの一番右の人なので、
            // それ以外は削除する
            if (!submap.isEmpty()) {
                int val = 0;
                int now_r = submap.lastKey();
                int from_r = submap.get(now_r);
                
                // gapsの削除
                for (Integer key : submap.keySet()) {
                    val = map.get(key);
                    int gap = key-val;
                    
                    int cnt = gaps.get(gap);
                    if (cnt == 1) {
                        gaps.remove(gap);
                    } else {
                        gaps.put(gap, --cnt);
                    }
                }
                
                // mapの削除
                submap.clear();
                
                // map(now, from)のnowの更新
                now_r = R+1;
                if (now_r < w) {
                    map.put(now_r, from_r);
                }
                
                // gapsの追加
                if (now_r < w) {
                    val = gaps.getOrDefault(now_r - from_r, 0);
                    gaps.put(now_r - from_r, ++val);
                }
                // System.out.println(map.toString());
                // System.out.println(gaps.toString());
                // System.out.println(now-right);
            }
            
            // ans: 縦移動+横移動
            int ans = -1;
            if (!gaps.isEmpty()) {
                ans = i + gaps.firstKey();
            }
            out.println(ans);
        }
        out.flush(); 
    }
}