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: Accepted)

#include <bits/stdc++.h>
using namespace std;

const int inf = numeric_limits<int>::max() / 2;
struct recursive_segment_tree{
	using L = int; // Lazy type
	using Q = int; // Query type
	L apply_lazy(const L &lazy, const L &x, array<int, 2> r, array<int, 2> rr){ // r is a subset of rr
		return !~x ? lazy : x + r[0] - rr[0];
	} // update lazy node representing r with rr
	Q merge(const Q &lval, const Q &rval, int l, int m, int r){
		return min(lval, rval);
	} // merge two nodes representing the intervals [l, m) and [m, r)
	Q apply(const Q &val, const L &x, array<int, 2> r, array<int, 2> rr){ // r is a subset of rr
		return !~x ? val : x + r[0] - rr[0];
	} // apply to node representing r lazy node representing rr
	pair<L, Q> id{-1, inf};
	Q init(int p){
		return id.second;
	}

	int n;
	vector<L> lazy;
	vector<Q> val;
	void push(int u, int l, int r){ // push the internal node u
		if(lazy[u] != id.first && u + 1 < n << 1){
			int m = l + (r - l >> 1), v = u + 1, w = u + (m - l << 1);
			val[v] = apply(val[v], lazy[u], {l, m}, {l, r});
			lazy[v] = apply_lazy(lazy[v], lazy[u], {l, m}, {l, r});
			val[w] = apply(val[w], lazy[u], {m, r}, {l, r});
			lazy[w] = apply_lazy(lazy[w], lazy[u], {m, r}, {l, r});
			lazy[u] = id.first;
		}
	}
	void refresh(int u, int l, int r){
		if(u + 1 < n << 1){
			int m = l + (r - l >> 1), v = u + 1, w = u + (m - l << 1);
			val[u] = merge(val[v], val[w], l, m, r);
		}
		if(lazy[u] != id.first) val[u] = apply(val[u], lazy[u], {l, r}, {l, r});
	}
	template<typename IT>
	recursive_segment_tree(IT begin, IT end): n(end - begin), lazy(n << 1, id.first), val(n << 1, id.second){
		build(begin, end, 0, 0, n);
	}
	recursive_segment_tree(int n): n(n), lazy(n << 1, id.first), val(n << 1, id.second){
		build(0, 0, n);
	}
	template<typename IT>
	void build(IT begin, IT end, int u, int l, int r){
		if(l + 1 == r) val[u] = *begin;
		else{
			int m = l + (r - l >> 1), v = u + 1, w = u + (m - l << 1);
			IT inter = begin + (end - begin >> 1);
			build(begin, inter, v, l, m), build(inter, end, w, m, r);
			val[u] = merge(val[v], val[w], l, m, r);
		}
	}
	void build(int u, int l, int r){
		if(l + 1 == r) val[u] = init(l);
		else{
			int m = l + (r - l >> 1), v = u + 1, w = u + (m - l << 1);
			build(v, l, m), build(w, m, r);
			val[u] = merge(val[v], val[w], l, m, r);
		}
	}
	template<bool First = true>
	void update(int ql, int qr, L x, int u = 0, int l = 0, int r = numeric_limits<int>::max()){
		if(First) r = n;
		if(qr <= l || r <= ql) return;
		if(ql <= l && r <= qr) val[u] = apply(val[u], x, {l, r}, {ql, qr}), lazy[u] = apply_lazy(lazy[u], x, {l, r}, {ql, qr});
		else{
			push(u, l, r);
			int m = l + (r - l >> 1), v = u + 1, w = u + (m - l << 1);
			update<false>(ql, qr, x, v, l, m), update<false>(ql, qr, x, w, m, r);
			refresh(u, l, r);
		}
	} // Apply x to values at [ql, qr)
	template<bool First = true>
	Q query(int ql, int qr, int u = 0, int l = 0, int r = numeric_limits<int>::max()){
		if(First) r = n;
		if(qr <= l || r <= ql) return id.second;
		if(ql <= l && r <= qr) return val[u];
		push(u, l, r);
		int m = l + (r - l >> 1), v = u + 1, w = u + (m - l << 1);
		return merge(query<false>(ql, qr, v, l, m), query<false>(ql, qr, w, m, r), max(ql, l), clamp(m, ql, qr), min(qr, r));
	} // Get the query result for [ql, qr)
};

int main(){
	cin.tie(0)->sync_with_stdio(0);
	cin.exceptions(ios::badbit | ios::failbit);
	int h, w;
	cin >> h >> w;
	vector<int> a(w);
	recursive_segment_tree tr(a.begin(), a.end());
	for(auto i = 1; i <= h; ++ i){
		int l, r;
		cin >> l >> r, -- l;
		if(!l){
			tr.update(l, r, inf);
		}
		else{
			tr.update(l, r, tr.query(l - 1, l) + 1);
		}
		int res = tr.query(0, w) + i;
		res < inf ? cout << res << "\n" : cout << "-1\n";
	}
	return 0;
}

/*

*/

////////////////////////////////////////////////////////////////////////////////////////
//                                                                                    //
//                                   Coded by Aeren                                   //
//                                                                                    //
////////////////////////////////////////////////////////////////////////////////////////