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# (.NET Core 3.1.201) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Collections;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using static System.Math;
using System.Numerics;
using nint=System.Int32;
static class Program{
	const int mod=(int)1e9+7;
	const double eps=1e-11;
	static void Main(){
		Sc sc=new Sc();
		var s=sc.Ia;
		var avl=new Avl();
		St st=new St(s[1]+1,false,false,0);
		var b=new bool[s[1]+1];
		var h=new int[s[1]+1];
		for(int i = 1;i<=s[1];i++) {
			avl.Ud(i);
			b[i]=true;
			h[i]=i;
		}
		st.Ud(0,mod);
		StringBuilder sb=new StringBuilder();
		for(int i = 1;i<=s[0];i++) {
			var e=sc.Ia;
			var d=avl.Lb(e[0]);
			int p=0,g=e[1]+1;
			while(d!=null&&d.n<=e[1]){
				b[d.n]=false;
				st.Ud(d.n,mod);
				p=Max(h[d.n],p);
				h[d.n]=0;
				avl.Dl(d.n);
				d=avl.Lb(e[0]);
			}
			if(p!=0&&g!=s[1]+1&&!b[g]){
				avl.Ud(g);
				st.Ud(g,g-p);
				b[g]=true;
				h[g]=p;
			}
			var z=st.dat[1].d+i;
			sb.Append((z>1000000?-1:z)+"\n");
		}
		Console.Write(sb);
	}
}
public class St{
	public class Dt{
		public int n;
		public long d;
		public Dt(long d,int n){this.d=d;this.n=n;}
		public override string ToString()=>"n:"+n.ToString()+" d:"+d.ToString();
	}
	public Dt[] dat;
	private int n,cnt;
	private bool ku;
	private Dt du;
	private Func<Dt,Dt,Dt> compare;
	public St(int n,bool mm,bool ku,long df){
		this.n=n;
		this.ku=ku;
		if(mm){du=new Dt(long.MinValue,-1);compare=CoMax;}
		else{du=new Dt(long.MaxValue,-1);compare=CoMin;}
		cnt=n<<1;
		dat=new Dt[cnt];
		for(int i = cnt-1;i>=cnt-n;i--) {dat[i]=new Dt(df,n-cnt+i);}
		for(int i = cnt-n-1;i>0;i--) {dat[i]=dat[i<<1];}
	}
	private Dt CoMax(Dt x,Dt y){return x.d>y.d?x:y;}
	private Dt CoMin(Dt x,Dt y){return x.d<y.d?x:y;}
	public void Ud(int q,long b){
		q+=n;
		if(ku){dat[q].d+=b;}
		else{dat[q].d=b;}
		while(q>1){dat[q>>=1]=compare(dat[q<<1],dat[(q<<1)+1]);}
	}
	public Dt Get(int l,int r){
		Dt x=du;
		l+=n;r+=n;
		while(l<=r){
			if((l&1)==1){x=compare(x,dat[l++]);}
			if((r&1)==0){x=compare(x,dat[r--]);}
			l>>=1;r>>=1;
		}
		return x;
	}
}
public class Avl{
	public class Nd{
		public int h=1,c=1;
		public readonly nint n;
		public Nd l,r;
		public Nd(nint n,Nd du){this.n=n;l=du;r=du;}
	}
	public Nd root,du;
	public int cnt=0;
	public Avl(){
		du=new Nd(nint.MinValue,du);du.h=0;du.c=0;
		root=du;
	}
	public void Ud(nint n){
		Nd mn=new Nd(n,du);
		if(root==du){root=mn;}
		else{
			Fu(root,mn);
			if(Abs(root.l.h-root.r.h)>1){root=Rotate(root);}
		}
		cnt++;
	}
	private void Fu(Nd t,Nd mn){
		t.c++;
		if(t.n>mn.n){
			if(t.l!=du){
				Fu(t.l,mn);
				if(Abs(t.l.l.h-t.l.r.h)>1){t.l=Rotate(t.l);}
			}
			else{t.l=mn;}
		}
		else{
			if(t.r!=du){
				Fu(t.r,mn);
				if(Abs(t.r.l.h-t.r.r.h)>1){t.r=Rotate(t.r);}
			}
			else{t.r=mn;}
		}
		t.h=Max(t.l.h,t.r.h)+1;
	}
	private Nd Rotate(Nd t){
		Nd nd=du;
		if(t.l.h>t.r.h){
			if(t.l.l.h>t.l.r.h){
				nd=t.l;t.l=t.l.r;nd.r=t;
				t.h=Max(t.l.h,t.r.h)+1;
				t.c=t.l.c+t.r.c+1;
			}
			else{
				nd=t.l.r;
				t.l.r=nd.l;nd.l=t.l;
				t.l.h=Max(t.l.l.h,t.l.r.h)+1;
				t.l.c=t.l.l.c+t.l.r.c+1;
				t.l=nd.r;nd.r=t;
				t.h=Max(t.l.h,t.r.h)+1;
				t.c=t.l.c+t.r.c+1;
			}
		}
		else{
			if(t.r.l.h>t.r.r.h){
				nd=t.r.l;
				t.r.l=nd.r;nd.r=t.r;
				t.r.h=Max(t.r.l.h,t.r.r.h)+1;
				t.r.c=t.r.l.c+t.r.r.c+1;
				t.r=nd.l;nd.l=t;
				t.h=Max(t.l.h,t.r.h)+1;
				t.c=t.l.c+t.r.c+1;
			}
			else{
				nd=t.r;t.r=t.r.l;nd.l=t;
				t.h=Max(t.l.h,t.r.h)+1;
				t.c=t.l.c+t.r.c+1;
			}
		}
		nd.h=Max(nd.l.h,nd.r.h)+1;
		nd.c=nd.l.c+nd.r.c+1;
		return nd;
	}
	public bool Dl(nint n){
		if(cnt==0){return false;}
		Nd t=root;
		if(t.n==n){
			root=Fd2(root);
			if(cnt==0){return true;}
			else if(Abs(root.l.h-root.r.h)>1){root=Rotate(root);}
			else{
				root.h=Max(root.l.h,root.r.h)+1;
				root.c=root.l.c+root.r.c+1;
			}
			return true;
		}
		bool bo=Fd1(root,n);
		if(Abs(root.l.h-root.r.h)>1){root=Rotate(root);}
		else{
			root.h=Max(root.l.h,root.r.h)+1;
			root.c=root.l.c+root.r.c+1;
		}
		return bo;
	}
	private bool Fd1(Nd t,nint n){
		if(t.n>n){
			if(t.l==du){return false;}
			else if(t.l.n!=n){
				bool bo=Fd1(t.l,n);
				if(Abs(t.l.l.h-t.l.r.h)>1){t.l=Rotate(t.l);}
				else{
					t.l.h=Max(t.l.l.h,t.l.r.h)+1;
					t.l.c=t.l.l.c+t.l.r.c+1;
				}
				return bo;
			}
			else{
				t.l=Fd2(t.l);
				t.h=Max(t.l.h,t.r.h)+1;
				t.c=t.l.c+t.r.c+1;
				return true;
			}
		}
		else{
			if(t.r==du){return false;}
			else if(t.r.n!=n){
				bool bo=Fd1(t.r,n);
				if(Abs(t.r.l.h-t.r.r.h)>1){t.r=Rotate(t.r);}
				else{
					t.r.h=Max(t.r.l.h,t.r.r.h)+1;
					t.r.c=t.r.l.c+t.r.r.c+1;
				}
				return bo;
			}
			else{
				t.r=Fd2(t.r);
				t.h=Max(t.l.h,t.r.h)+1;
				t.c=t.l.c+t.r.c+1;
				return true;
			}
		}
	}
	private Nd Fd2(Nd dn){
		cnt--;
		if(dn.l==du){return dn.r;}
		if(dn.r==du){return dn.l;}
		Nd u=dn.l;
		if(u.r==du){
			u.r=dn.r;
			u.h=Max(u.l.h,u.r.h)+1;
			u.c=u.l.c+u.r.c+1;
			return u;
		}
		u=Fd3(u,dn);
		if(Abs(dn.l.l.h-dn.l.r.h)>1){u.l=Rotate(dn.l);}
		else{
			dn.l.h=Max(dn.l.l.h,dn.l.r.h)+1;
			dn.l.c=dn.l.l.c+dn.l.r.c+1;
		}
		u.h=Max(u.l.h,u.r.h)+1;
		u.c=u.l.c+u.r.c+1;
		return u;
	}
	private Nd Fd3(Nd u,Nd dn){
		if(u.r.r!=du){
			Nd v=Fd3(u.r,dn);
			if(Abs(u.r.l.h-u.r.r.h)>1){u.r=Rotate(u.r);}
			else{
				u.r.h=Max(u.r.l.h,u.r.r.h)+1;
				u.r.c=u.r.l.c+u.r.r.c+1;
			}
			return v;
		}
		else{
			Nd v=u.r;u.r=u.r.l;v.l=dn.l;v.r=dn.r;
			return v;
		}
	}
	public bool Hs(nint n){
		Nd t=root;
		if(cnt==0){return false;}
		if(t.n==n){return true;}
		bool bo=false;
		while(true){
			if(t.n>n){
				if(t.l==du){break;}
				else if(t.l.n!=n){t=t.l;}
				else{bo=true;break;}
			}
			else{
				if(t.r==du){break;}
				else if(t.r.n!=n){t=t.r;}
				else{bo=true;break;}
			}
		}
		return bo;
	}
	public Nd Ra(int n){return cnt>=n&&cnt!=0?Fr(root,n):null;}
	private Nd Fr(Nd t,int n){
		if(t.l.c>n-1){return Fr(t.l,n);}
		if(t.l.c<n-1){return Fr(t.r,n-t.l.c-1);}
		return t;
	}
	public Nd Lb(nint n){return cnt!=0?Flb(root,n):null;}
	private Nd Flb(Nd t,nint n){
		if(t.n<n){
			if(t.r==du){return null;}
			return Flb(t.r,n);
		}
		if(t.n>n){
			if(t.l==du){return t;}
			Nd u=Flb(t.l,n);
			return u==null?t:u;
		}
		return t;
	}
	public Nd Ub(nint n){return cnt!=0?Fub(root,n):null;}
	private Nd Fub(Nd t,nint n){
		if(t.n>n){
			if(t.l==du){return null;}
			return Fub(t.l,n);
		}
		if(t.n<n){
			if(t.r==du){return t;}
			Nd u=Fub(t.r,n);
			return u==null?t:u;
		}
		return t;
	}
	public int Rg(nint a,nint b){return cnt!=0?Frg(root,a,b,false,false):0;}
	private int Frg(Nd t,nint a,nint b,bool l,bool r){
		if(t.h==0){return 0;}
		if(l&&r){return t.c;}
		if(t.n<a){return Frg(t.r,a,b,t.n>=a,r);}
		if(t.n>b){return Frg(t.l,a,b,l,t.n<=b);}
		return Frg(t.l,a,b,l,t.n<=b)+Frg(t.r,a,b,t.n>=a,r)+1;
	}
	public void En(Action<Nd> f){if(cnt>0){Fen(root,f);}}
	private void Fen(Nd t,Action<Nd> f){
		f(t);
		if(t.l!=du){Fen(t.l,f);}
		if(t.r!=du){Fen(t.r,f);}
	}
	public void En(nint l,nint r,Action<Nd> f){if(cnt>0){Fen(root,l,r,false,false,f);}}
	private void Fen(Nd t,nint a,nint b,bool l,bool r,Action<Nd> f){
		if(l&&r){
			f(t);
			if(t.l!=du){Fen(t.l,f);}
			if(t.r!=du){Fen(t.r,f);}
		}
		else{
			if(t.n<a){if(t.r!=du){Fen(t.r,a,b,l,r,f);}}
			else if(t.n>b){if(t.l!=du){Fen(t.l,a,b,l,r,f);}}
			else{
				f(t);
				if(t.l!=du){Fen(t.l,a,b,l,t.n<=b,f);}
				if(t.r!=du){Fen(t.r,a,b,t.n>=a,r,f);}
			}
		}
	}
}
public class Sc{
	public int I{get{return int.Parse(Console.ReadLine());}}
	public long L{get{return long.Parse(Console.ReadLine());}}
	public double D{get{return double.Parse(Console.ReadLine());}}
	public string S{get{return Console.ReadLine();}}
	public int[] Ia{get{return Array.ConvertAll(Console.ReadLine().Split(),int.Parse);}}
	public long[] La{get{return Array.ConvertAll(Console.ReadLine().Split(),long.Parse);}}
	public double[] Da{get{return Array.ConvertAll(Console.ReadLine().Split(),double.Parse);}}
	public string[] Sa{get{return Console.ReadLine().Split();}}
	public object[] Oa{get{return Console.ReadLine().Split();}}
	public int[] Ia2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),int.Parse);}}
	public int[] Ia3(string a,string b){return Array.ConvertAll((a+Console.ReadLine()+b).Split(),int.Parse);}
	public int[] Ia3(int a){return Array.ConvertAll((Console.ReadLine()+" "+a.ToString()).Split(),int.Parse);}
	public long[] La2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),long.Parse);}}
	public long[] La3(string a,string b){return Array.ConvertAll((a+Console.ReadLine()+b).Split(),long.Parse);}
	public long[] La3(int a){return Array.ConvertAll((Console.ReadLine()+" "+a.ToString()).Split(),long.Parse);}
	public double[] Da2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),double.Parse);}}
	public double[] Da3(string a,string b){return Array.ConvertAll((a+Console.ReadLine()+b).Split(),double.Parse);}
	public T[] Arr<T>(int n,Func<T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f();}return a;}
	public T[] Arr<T>(int n,Func<int,T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i);}return a;}
	public T[] Arr<T>(int n,Func<string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(Console.ReadLine().Split());}return a;}
	public T[] Arr<T>(int n,Func<int,string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i,Console.ReadLine().Split());}return a;}
}