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<iostream>
using namespace std;
int h,w; 
int l[200010],r[200010];
struct tree{
	int l,r;
	long long tag,v;
}tr[2][4*200010];
void build(int i,int l,int r)
{
	tr[0][i].l=l,tr[0][i].r=r;
	if (l==r) return;
	int mid=(l+r)>>1;
	build(i<<1,l,mid);
	build(i<<1|1,mid+1,r);
}
void update(int i,int id)
{
	tr[id][i].v=min(tr[id][i<<1].v,tr[id][i<<1|1].v);
}
void build2(int i,int l,int r)
{
	tr[1][i].l=l,tr[1][i].r=r;
	
	if (l==r) {tr[1][i].v=-l;return;}
	int mid=(l+r)>>1;
	build2(i<<1,l,mid);
	build2(i<<1|1,mid+1,r);
	update(i,1);
}
void add(int i,int x,int id)
{
	//if (id==0&&tr[id][i].l==2&&tr[id][i].l==tr[id][i].r)
	//	cout<<tr[id][i].v<<" "<<x<<" tree"<<endl;
	tr[id][i].v+=x;
	tr[id][i].tag+=x;
}
void pushdown(int i,int id)
{
	if (tr[id][i].tag)
	{
		add(i<<1,tr[id][i].tag,id);
		add(i<<1|1,tr[id][i].tag,id);
		tr[id][i].tag=0;
	}
}
void modify(int i,int l,int r,int x,int id)
{
	int L=tr[id][i].l,R=tr[id][i].r;
	if (l>R) return;
	if (l<=L&&R<=r)
	{
		add(i,x,id);
		return;
	}
	pushdown(i,id);
	int mid=(L+R)>>1;
	if (l<=mid) modify(i<<1,l,r,x,id);
	if (r>mid) modify(i<<1|1,l,r,x,id);
	update(i,id); 
}
long long query(int i,int l,int r,int id)
{
	int L=tr[id][i].l,R=tr[id][i].r;
	if (l>R) return 0;
	//cout<<l<<" "<<r<<" "<<L<<" "<<R<<" "<<tr[id][i].v<<endl; 
	if (l<=L&&R<=r) return tr[id][i].v; 
	pushdown(i,id);
	int mid=(L+R)>>1;
	long long maxn=1e18;
	if (l<=mid) maxn=min(maxn,query(i<<1,l,r,id));
	if (r>mid) maxn=min(maxn,query(i<<1|1,l,r,id));
	return maxn;
}
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);build2(1,1,w);
	for (int i=1;i<=h;i++)
	{
		long long other=min(0ll,r[i]+1+query(1,1,r[i],1)-query(1,r[i]+1,r[i]+1,0));
	//	cout<<other<<endl;
	//	cout<<"step1 "<<query(1,r[i]+1,r[i+1],0)<<endl;
		modify(1,r[i]+1,r[i]+1,other,0);
		modify(1,r[i]+1,r[i]+1,other,1);
		modify(1,l[i],r[i],1e9,0);
	//	cout<<"step2"<<" "<<query(1,2,2,0)<<endl; 
		modify(1,l[i],r[i],1e9,1);
		modify(1,1,w,1,0);
	//	cout<<"step3"<<" "<<query(1,2,2,0)<<endl<<endl; 
		modify(1,1,w,1,1);
		long long ans=query(1,1,w,0);
		if (ans>=1e9) printf("-1\n");
		else printf("%d\n",ans);
	}
}