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 C++ (GCC 9.2.1) (judge verdict: Runtime Error)

#include<bits/stdc++.h>
using namespace std;
#define ll long long
long inf;
#define INT_MAX inf
struct LazySegmentTree {
private:
    int n;
    vector<ll> node, lazy;

public:
    LazySegmentTree(vector<ll> v) {
        int sz = (int)v.size();
        n = 1; while(n < sz) n *= 2;
        node.resize(2*n-1);
        lazy.resize(2*n-1, -1);

        for(int i=0; i<sz; i++) node[i+n-1] = v[i];
        for(int i=n-2; i>=0; i--) node[i] = min(node[i*2+1], node[i*2+2]);
    }

    // k 番目のノードについて遅延評価を行う
    void eval(int k, int l, int r) {

        // 遅延配列が空でない場合、自ノード及び子ノードへの
        // 値の伝播が起こる
        if(lazy[k] != -1) {
            //if(r - l > 1) {eval(2*k+1,l,(l+r)/2);eval(2*k+2,(l+r)/2,r);}// ?? 先に下を解決しておく
            node[k] = lazy[k];

            // 最下段かどうかのチェックをしよう
            // 子ノードは親ノードの 1/2 の範囲であるため、
            // 伝播させるときは半分にする
            if(r - l > 1) {
                lazy[2*k+1] = lazy[k];
                lazy[2*k+2] = lazy[k]+(r-l)/2; 
            }

            // 伝播が終わったので、自ノードの遅延配列を空にする
            lazy[k] = -1;
        }
    }
    void add(int a, int b, ll x, int k=0, int l=0, int r=-1) {
        if(r < 0) r = n;

        // k 番目のノードに対して遅延評価を行う
        eval(k, l, r);

        // 範囲外なら何もしない
        if(b <= l || r <= a) return;

        // 完全に被覆しているならば、遅延配列に値を入れた後に評価
        if(a <= l && r <= b) {
            lazy[k] = x;
            eval(k, l, r);
        }

        // そうでないならば、子ノードの値を再帰的に計算して、
        // 計算済みの値をもらってくる
        else {
            add(a, b, x, 2*k+1, l, (l+r)/2);
            //add(a, b, x+(r-l)/2, 2*k+2, (l+r)/2, r);    
            add(a, b, x+(l+r)/2-a, 2*k+2, (l+r)/2, r);    
            node[k] = min(node[2*k+1], node[2*k+2]);
        }
        ///        a  b
        ///     l        r
        ///     l    rl    r
    }
    ll getsum(int a, int b, int k=0, int l=0, int r=-1) {
        if(r < 0) r = n;
        if(b <= l || r <= a) return INT_MAX;

        // 関数が呼び出されたら評価！
        eval(k, l, r);
        if(a <= l && r <= b) return node[k];
        ll vl = getsum(a, b, 2*k+1, l, (l+r)/2);
        ll vr = getsum(a, b, 2*k+2, (l+r)/2, r);
        return min(vl, vr);
    }

};



int main(){
  int H,W;cin>>H>>W;inf=H+W;
  vector<long>A(H),B(H);for(int i=0;i<H;i++)cin>>A[i]>>B[i];
  LazySegmentTree lst(vector<ll>(W,0));
  for(long i=0;i<H;i++){
    if(A[i]>1)lst.add(A[i]-1,B[i],1+lst.getsum(A[i]-2,A[i]-1));
    else lst.add(0,B[i],INT_MAX);
    long x=lst.getsum(0,W)+i+1;
    cout<<(x<inf?x:-1)<<endl;
    assert(x>=0);
    //cout<<lst.getsum(0,W)+i+1<<endl;
    //cout<<" "<<(A[i]>1?1+lst.getsum(A[i]-2,A[i]-1):-1)<<endl;
    //cout<<" "<<(A[i]>1?A[i]-1:-1)<<endl;
    //for(long j=0;j<W;j++)cout<<lst.getsum(j,j+1)<<" ";cout<<endl;
  }

}




/*

移動回数を持たせる
遅延は, 
[この区間はQから初めて1ずつ上がります]
という意味にすればいい




*/