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: Accepted)

#include <bits/stdc++.h>
#define x first
#define y second
#define all(v) v.begin(), v.end()
#define compress(v) sort(all(v)), v.erase(unique(all(v)), v.end())
using namespace std;

typedef long long ll;
typedef pair<ll, ll> p;

int n, m;
int a[202020], b[202020];

ll tree[1 << 19];
const int sz = 1 << 18;
void update(int x, ll v){
    x |= sz; tree[x] = v;
    while(x >>= 1) tree[x] = min(tree[x << 1], tree[x << 1 | 1]);
}
ll query(int l, int r){
    l |= sz; r |= sz; ll ret = 1e18;
    while(l <= r){
        if(l & 1) ret = min(ret, tree[l++]);
        if(~r & 1) ret = min(ret, tree[r--]);
        l >>= 1; r >>= 1;
    }
    return ret;
}

ll tree2[1 << 19];
void update2(int x, ll v){
    v -= x;
    x |= sz; tree2[x] = v;
    while(x >>= 1) tree2[x] = min(tree2[x << 1], tree2[x << 1 | 1]);
}
ll query2(int l, int r){
    l |= sz; r |= sz; ll ret = 1e18;
    while(l <= r){
        if(l & 1) ret = min(ret, tree2[l++]);
        if(~r & 1) ret = min(ret, tree2[r--]);
        l >>= 1; r >>= 1;
    }
    return ret;
}

int main(){
    ios_base::sync_with_stdio(false); cin.tie(nullptr);
    memset(tree, 0x3f, sizeof tree);
    memset(tree2, 0x3f, sizeof tree2);
    cin >> n >> m;
    set<p> st;
    for(int i=1; i<=m; i++){
        st.emplace(i, 0); update(i, 0); update2(i, 0);
    }
    
    for(int i=1; i<=n; i++){
        cin >> a[i] >> b[i];
        if(query(1, m) >= 1e9){ cout << "-1\n"; continue; }
        
        ll mn = query2(1, b[i]+1) + b[i]+1;
        
        auto it = st.lower_bound(p(a[i], -1));
        
        while(it != st.end()){
            if(it->x > b[i]+1) break;
            update(it->x, 1e18);
            update2(it->x, 1e18);
            it = st.erase(it);
        }
        if(b[i]+1 <= m){
            st.emplace(b[i]+1, mn);
            update(b[i]+1, mn); update2(b[i]+1, mn);
        }
        
        ll res = query(1, m);
        if(res >= 1e9) cout << "-1\n";
        else cout << res+i << "\n";
    }
}