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;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class TEST{
	static void Main(){
		Sol mySol =new Sol();
		mySol.Solve();
	}
}

class Sol{
	public void Solve(){
		
		var ST = new SegTree(W + 1);
		for(int i=0;i<W;i++) ST.Update(i, 0);
		
		int[] ans = new int[H];
		for(int i=0;i<H;i++){
			while(true){
				var idx = ST.QueryMinIdx(A[i], B[i] + 1);
				var val = ST.At(idx);
				if(val == ST.Inf) break;
				int hor = B[i] + 1 - idx;
				if(ST.At(B[i] + 1) > val + hor) ST.Update(B[i] + 1, val + hor);
				ST.ClearAt(idx);
			}
			var minhor = ST.QueryMin(0, W);
			ans[i] = minhor == ST.Inf ? -1 : (minhor + i + 1);
		}
		
		Console.WriteLine(String.Join("\n", ans));
		
	}
	
	int H,W;
	int[] A,B;
	public Sol(){
		var d = ria();
		H = d[0]; W = d[1];
		A = new int[H];
		B = new int[H];
		for(int i=0;i<H;i++){
			d = ria();
			A[i] = d[0] - 1; B[i] = d[1] - 1;
		}
	}

	static String rs(){return Console.ReadLine();}
	static int ri(){return int.Parse(Console.ReadLine());}
	static long rl(){return long.Parse(Console.ReadLine());}
	static double rd(){return double.Parse(Console.ReadLine());}
	static String[] rsa(char sep=' '){return Console.ReadLine().Split(sep);}
	static int[] ria(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>int.Parse(e));}
	static long[] rla(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>long.Parse(e));}
	static double[] rda(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>double.Parse(e));}
}



class SegTree{
	//segment Tree 
	// 0-origin
	public int[] Data;	
	public int[] Idx;	
	public int Inf;	
	public int N;		//size 
	public int n;		//size (power of 2);
	
	// constructor
	public SegTree(int n_, int inf_){
		N=n_;Inf = inf_;
		n = 1;
		while(n < n_) n *= 2;
		Data = new int[2*n - 1];
		Idx = new int[2*n - 1];
		for(int i=0;i<2*n-1;i++) Data[i] = Inf;
	}
	public SegTree(int n_){
		N = n_; Inf = (int) 1e9;
		n = 1;
		while(n < n_) n *= 2;
		Data = new int[2*n - 1];
		Idx = new int[2*n - 1];
		for(int i=0;i<2*n-1;i++) Data[i] = Inf;
	}
	
	//	   0
	//	 1   2
	//	3 4 5 6
	public void Update(int k, int a){
		k+=n-1;	
		Data[k]=a;
		Idx[k] = k - (n - 1);
		while(k>0){
			k=(k-1)/2;
			Data[k] = Math.Min(Data[k*2+1], Data[k*2+2]);
			Idx[k] = Data[k*2+1] < Data[k*2+2] ? Idx[k*2+1] : Idx[k*2+2];
		}
	}
	
	
	
	//RMQ;
	//	[a,b) 
	//	call: Query(a,b,0,0,n) 
	public int QueryMin(int a, int b){
		return Query(a, b, 0, 0, n);
	}
	public int QueryMinIdx(int a, int b){
		return QueryIdx(a, b, 0, 0, n);
	}
	public int At(int idx){
		return Data[idx + n - 1];
	}
	public void ClearAt(int idx){
		Update(idx, Inf);
	}
	
	public int Query(int a,int b,int k,int l,int r){
		// [a,b) not overwraps [l,r) => return Inf
		if(r<=a || b<=l) return Inf;
		// [a,b) include [l,r) => return Data[k];
		if(a<=l && r<=b)return Data[k];
		// or return min(left,right)
		var vl = Query(a,b,k*2+1,l,(l+r)/2);
		var vr = Query(a,b,k*2+2,(l+r)/2,r);
		return Math.Min(vl,vr);
	}
	public int QueryIdx(int a,int b,int k,int l,int r){
		// [a,b) not overwraps [l,r) => return Inf
		if(r<=a || b<=l) return -1;
		// [a,b) include [l,r) => return Data[k];
		if(a<=l && r<=b)return Idx[k];
		// or return min(left,right)
		var vlidx = QueryIdx(a,b,k*2+1,l,(l+r)/2);
		var vridx = QueryIdx(a,b,k*2+2,(l+r)/2,r);
		var vl = Data[vlidx + n - 1];
		var vr = Data[vridx + n - 1];
		return vl < vr ? vlidx : vridx;
	}
	
	
	public int Min{
		get{ return Data[0]; }
	}
	public int MinIdx{
		get{ return Idx[0]; }
	}
	
	public void UniqInit(int val){
		for(int i=0+n-1;i<N+n-1;i++) Data[i] = val;
		int l = n-1; int r = N+n-1-1;
		while(l > 0){
			l = (l-1)/2; r = (r-1)/2;
			for(int i=l;i<=r;i++) Data[i] = val;
		}
	}
	public void UniqInit(int[] vs){
		for(int i=0+n-1;i<N+n-1;i++) {
			Data[i] = vs[i-(n-1)];
			Idx[i] = i - (n-1);
		}
		for(int i=n-2;i>=0;i--){
			Data[i] = Math.Min(Data[2*i+1],Data[2*i+2]);
			Idx[i] = Data[2*i+1] < Data[2*i+2] ? Idx[2*i+1] : Idx[2*i+2];
		}
	}
	
	public void Dump(){
		Console.WriteLine();
		int h = 0;
		int cnt = 0;
		for(int i=0;i<Data.Length;i++){
			Console.Write("{0} ",Data[i]);
			cnt++;
			if(cnt == 1<<h){
				cnt = 0;
				h++; 
				Console.WriteLine();
			}
		}
	}
	
}