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: Wrong Answer)

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
using namespace std;
typedef long long LL;
const int SIZEN = 200010;
const int INF = 0x3f3f3f3f;
int mn[SIZEN << 2] = {0};
int lazy[SIZEN << 2] = {0};
int add[SIZEN << 2] = {0};
int H,W;
int l[SIZEN],r[SIZEN];
#define mid ((l+r)>>1)
void build(int rt,int l,int r){
    mn[rt] = 0;lazy[rt] = INF;add[rt] = 0;
    if(l == r)return;
    build(rt<<1,l,mid);
    build(rt<<1|1,mid+1,r);
}
void pushdown(int rt,int l,int r){
    if(lazy[rt] != INF){
        int t = lazy[rt];lazy[rt] = INF;
        mn[rt<<1] = t + l;
        mn[rt<<1|1] = t+mid+1;
        add[rt<<1] = 0;
        add[rt<<1|1] = 0;
        lazy[rt<<1] = min(lazy[rt<<1],t);
        lazy[rt<<1|1] = min(lazy[rt<<1|1],t);
    }
    if(add[rt]){
        int t = add[rt];add[rt] = 0;
        mn[rt<<1] += t;
        mn[rt<<1|1] += t;
        if(lazy[rt<<1] != INF)lazy[rt<<1] += t; else add[rt<<1] += t;
        if(lazy[rt<<1|1] != INF)lazy[rt<<1|1] += t; else add[rt<<1|1] += t;
    }
}
int get(int rt,int l,int r,int x,int y){
    if(x <= l && y >= r)return mn[rt];
    pushdown(rt,l,r);
    if(y <= mid)return get(rt<<1,l,mid,x,y);
    if(x >  mid)return get(rt<<1|1,mid+1,r,x,y);
    return min(get(rt<<1,l,mid,x,y),get(rt<<1|1,mid+1,r,x,y));
}
void change(int rt,int l,int r,int x,int y){
    if(x <= l && y >= r){
        mn[rt]++;
        if(lazy[rt]!=INF){
            lazy[rt]++;
        }else{
            add[rt]++;
        }
        return;
    }
    pushdown(rt,l,r);
    if(x <= mid)change(rt<<1,l,mid,x,y);
    if(y >  mid)change(rt<<1|1,mid+1,r,x,y);
    mn[rt] = min(mn[rt<<1],mn[rt<<1|1]);
}
void change(int rt,int l,int r,int x,int y,int z){
    if(x <= l && y >= r){
        mn[rt] = z + l;
        lazy[rt] = min(lazy[rt],z);
        add[rt] = 0;
        return;
    }
    pushdown(rt,l,r);
    if(x <= mid)change(rt<<1,l,mid,x,y,z);
    if(y >  mid)change(rt<<1|1,mid+1,r,x,y,z);
    mn[rt] = min(mn[rt<<1],mn[rt<<1|1]);
}
int main(){
    scanf("%d%d",&H,&W);
    for(int i = 1;i <= H;i++)scanf("%d%d",&l[i],&r[i]);
    build(1,1,W);
    int now = 1;
    for(int i = 1;i <= H;i++){
        if(now < l[i]){
            change(1,1,W,now,l[i]-1);
            if(r[i]+1 <= W)change(1,1,W,r[i]+1,W);
            int x = get(1,1,W,l[i]-1,l[i]-1);
            change(1,1,W,l[i],r[i],x-l[i]+1);
        }
        else if(l[i] <= now && now <= r[i]){
            now = r[i]+1;
            if(now > W){
                for(int j = i;j <= H;j++)puts("-1");
                return 0;
            }
            change(1,1,W,now,W);
        }else{
            change(1,1,W,now,W);
        }
        printf("%d\n",get(1,1,W,now,W));
        // printf("now = %d : ",now);
        // for(int i = 1;i <= W;i++)printf("%d ",get(1,1,W,i,i));printf("\n");
        // for(int i = 1;i <= 7;i++)printf("(%d %d %d) ,",lazy[i],add[i],mn[i]);printf("\n");
    }
    return 0;
}