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 <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>

using namespace std;

int H, W;
int A[200005], B[200005];

int C[200005];
set<int > S;

int ans[200005];

multiset<int> M;

int p = 0;
int E[200005];

void ADD(int k) {
	E[k]++;
}
void DEL(int k) {
	E[k]--;
	
	while (E[p] == 0) { p++; }
}

int main(void) {

	std::ios::sync_with_stdio(false);
	std::cin.tie(0);

	cin >> H >> W;
	for (int i = 1; i <= H; i++) {
		cin >> A[i] >> B[i];
	}

	for (int i = 1; i <= W; i++) { S.insert(i); /*M.insert(0);*/ ADD(0); }
	
	/*if (A[1] == 1 && B[1] == W) { for (int i = 1; i <= H; i++) { cout << -1 << endl; } return 0; }

	if (A[1]==1) {}
	else if (A[1] == 2) { S.insert(1); M.insert(0); }
	else { S.insert(1); M.insert(0);  S.insert(A[1] - 1); M.insert(0); }

	if (B[1] < W) {
		S.insert(B[1] + 1); M.insert(0);
	}
	ans[1] = 1;*/
	
	for (int i = 1; i <= H; i++) {
		
		//cout << "i= " << i << endl;

		//-----------------
//		auto it = upper_bound(S.begin(), S.end(), B[i] + 1);
/*		if (it != S.begin()) { --it; }
		if (it!=S.end() && *it <= B[i] + 1 && ( B[i]+1<=W)) {
			C[B[i] + 1] = C[*it] + B[i] + 1 - (*it); int flag = 0;
			if (S.find(B[i] + 1) != S.end()) { flag = 1; }
			S.insert(B[i] + 1);if (flag == 0) { ADD(C[B[i] + 1]); }
		}
*/
		//cout << "S: "; for (auto y : S) { cout << " " << y; }cout << endl;
		//cout << "M: "; for (auto y : M) { cout << " " << y; }cout << endl;

		//--------------------------------------------------------
/*		it = upper_bound(S.begin(), S.end(), A[i] - 1);
		if (it != S.begin()) { --it; }
		if (it!=S.end() && *it <= A[i] - 1  &&  A[i]-1>=1) {
			C[A[i] - 1] = C[*it] + A[i] - 1 - (*it);
			//auto tmp_it = lower_bound(S.begin(), S.end(), A[i] - 1);if (tmp_it != S.end() && *tmp_it == A[i] - 1) {auto itr = lower_bound(M.begin(), M.end(), C[A[i] - 1]); M.erase(itr);}
			int flag = 0;
			if (S.find(A[i] - 1) != S.end()) { //auto itr = M.find(C[A[i]-1]);  M.erase(itr); 
		                                       flag = 1; }
			S.insert(A[i] - 1); //M.insert(C[A[i] - 1]);
		if (flag == 0) { ADD(C[A[i] - 1]); }
		}
*/
		//cout << "S: "; for (auto y : S) { cout << " " << y; }cout << endl;
		//cout << "M: "; for (auto y : M) { cout << " " << y; }cout << endl;
		
		//--------------------------------

/*		it = lower_bound(S.begin(), S.end(), A[i]);
		while (it!=S.end() && *it <= B[i]) {
			//auto itr = M.find(C[*it]); //cout << *itr << " 消去" << endl;M.erase(itr); 
			DEL(C[*it]);
			it = S.erase(it);
		}
*/
		//cout << "S: "; for (auto y : S) { cout << " " << y; }cout << endl;
		//cout << "M: "; for (auto y : M) { cout << " " << y; }cout << endl;

		//cout << "i= " << i << endl;
		//cout << "E:"; for (int i = 0; i <= 10; i++) { cout << " " << E[i]; } cout << " p= " << p << endl;

	/*	if (S.size() == 0) { ans[i] = -1; }
		else { ans[i] = i + p; }*/

	}

	
	for (int i = 1; i <= H; i++) {
		printf("%d\n", ans[i]);
		//cout << ans[i] << endl;
	}

	return 0;
}