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>
using namespace std;
using namespace std::chrono;
int n,m,l[200002],r[200002],dp[200002],pref[200002];

void tr(int row , int col){
	int num = 0;
	while(true){
		if(col > m || row == n+1){
			break;
		}
		if(col < l[row] || col > r[row]){
			row += 1;
			num += 1;
			if(dp[row] == -1){
				dp[row] = num;
			}else{
				dp[row] = min(dp[row],num);
			}
		}else{
			num += 1;
			col += 1;
			if(pref[col] >= row-1){
				num = row-1;
			}
		}
		// cout << row << " " << col << " " << num << endl;
	}
}

int main(){
	milliseconds ms = duration_cast<milliseconds>(system_clock::now().time_since_epoch());
	srand(ms.count());
	ios_base::sync_with_stdio(false);
	cout.tie(NULL);
	cin.tie(NULL);
	memset(dp,-1,sizeof dp);
	cin >> n >> m;
	vector<pair<int,int>> events[200002];
	for(int i = 1 ; i <= n ; i += 1){
		cin >> l[i] >> r[i];
		events[l[i]].push_back({0,i});
		events[r[i]+1].push_back({1,i});
	}
	set<int> st;
	vector<pair<int,int>> g;
	for(int i = 1 ; i <= m ; i += 1){
		for(pair<int,int> j : events[i]){
			if(j.first == 0){
				st.insert(j.second);
			}else{
				st.erase(j.second);
			}
		}
		if(st.size() == 0){
			pref[i] = n+1;
		}else{
			int a = *st.begin();
			pref[i] = a-1;
		}
		g.push_back({pref[i],i});
	}
	sort(g.rbegin(),g.rend());
	for(int i = 0 ; i < min(800,(int)g.size()) ; i += 1){
		if(g[i].first > 0){
			tr(1,g[i].second);
		}
	}
	sort(g.begin(),g.end());
	for(int i = 0 ; i < min(800,(int)g.size()) ; i += 1){
		if(g[i].first > 0){
			tr(1,g[i].second);
		}
	}
	random_shuffle(g.begin(),g.end());
	for(int i = 0 ; i < min(800,(int)g.size()) ; i += 1){
		if(g[i].first > 0){
			tr(1,g[i].second);
		}
	}
	if(l[1] == 1 && r[1] == m){
		for(int i = 1 ; i <= n ; i += 1){
			cout << -1 << '\n';
		}
		return 0;
	}
	if(l[1] > 1){
		tr(1,1);
		tr(1,l[1]-1);
	}
	if(r[1] < m){
		tr(1,m);
		tr(1,r[1]+1);
	}
	if(n >= 2 && l[2] > 1){
		tr(1,l[2]-1);
	}
	if(n >= 2 && r[2] < m){
		tr(1,r[2]+1);
	}
	// tr(1,2);
	for(int i = 2 ; i <= n+1 ; i += 1){
		cout << dp[i] << '\n';
	}
}