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<bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
const int N=2e5+5;
int n,m,lg[N],tot,a[N],b[N],ans[N],t[N<<2];
int rt[N],t1[N*100],t2[N*100],ls[N*100],rs[N*100];
void update(int l,int r,int k,int x,int y,int v)
{
    if(r<x||l>y) return;
    if(l>=x&&r<=y)
    {
        t[k]=min(t[k],v+l-x);return;
    }
    int m=l+r>>1;
    update(l,m,k<<1,x,y,v);
    update(m+1,r,k<<1|1,x,y,v+m+1-x);
}
void push(int l,int r,int k)
{
    if(l==r){ans[l]=t[k]==inf?-1:t[k];return;}
    int m=l+r>>1;
    t[k<<1]=min(t[k<<1],t[k]);
    t[k<<1|1]=min(t[k<<1|1],t[k]+m+1-l);
    push(l,m,k<<1);push(m+1,r,k<<1|1);
}
void Insert(int l,int r,int x1,int x2,int v1,int v2,int &now,int pre)
{
    now=++tot;
    ls[now]=ls[pre];rs[now]=rs[pre];
    t1[now]=t1[pre]+v1;
    t2[now]=t2[pre]+v2;
    if(l==r) return;
    int m=l+r>>1;
    if(x1<=m||x2<=m)
        Insert(l,m,x1,x2,v1&&x1<=m,v2&&x2<=m,ls[now],ls[pre]);
    if(x1>m||x2>m)
        Insert(m+1,r,x1,x2,v1&&x1>m,v2&&x2>m,rs[now],rs[pre]);
}
int query1(int l,int r,int x,int now,int pre)
{
    if(r<=x) return 0;
    if(l>x) return t1[now]-t1[pre];
    int m=l+r>>1;
    return query1(l,m,x,ls[now],ls[pre])+query1(m+1,r,x,rs[now],rs[pre]);
}
int query2(int l,int r,int x,int now,int pre)
{
    if(l>=x) return 0;
    if(r<x) return t2[now]-t2[pre];
    int m=l+r>>1;
    return query2(l,m,x,ls[now],ls[pre])+query2(m+1,r,x,rs[now],rs[pre]);
}
bool vis[N];
void Move(int x,int y,int d)
{
    if(y>=a[x]&&y<=b[x])
    {
        if(vis[x]) return;
        vis[x]=true;
        d+=b[x]-y+1;
        if(b[x]+1>m) return;
        y=b[x]+1;
        Move(x,y,d);
    }
    else
    {
        int xx=x;
        for(int i=1<<lg[n-x];i;i>>=1)
        {
            if(xx+i<=n&&query1(1,n,y,rt[xx+i],rt[x])+query2(1,n,y,rt[xx+i],rt[x])==xx+i-x)
                xx+=i;
        }
        xx++;
        update(1,n,1,x,xx-1,d+1);
        d+=xx-x;
        if(xx<=n)
            Move(xx,y,d);
    }
}
bool cmp(pair<int,int>&a,pair<int,int>&b)
{
    if(a.first==b.first) return a.second>b.second;
    return a.first<b.first;
}
int main()
{
    memset(t,inf,sizeof(t));
    for(int i=1;i<N;i++) lg[i]=lg[i-1]+(1<<lg[i-1]==i);
    for(int i=1;i<N;i++) lg[i]--;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++) scanf("%d%d",&a[i],&b[i]);
    for(int i=1;i<=n;i++)
        Insert(1,n,a[i],b[i],1,1,rt[i],rt[i-1]);
    for(int i=1;i<=n;i++)
        Move(1,i,0);
    push(1,n,1);
    for(int i=1;i<=n;i++) printf("%d\n",ans[i]);
}