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<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<cmath>
#include<assert.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define INF 0x3f3f3f3f
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
typedef double db;
const ll mod=1000000007;
const double PI = acos(-1.0);
const double epsilon = PI / 180.0;//角度转弧度 
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
const int N = 2e5+10, M = 1e6+10;
int h, w;
int a[N], b[N];
struct node {
	int l, r;
	int mn, add1, x, add2;
}tr[N * 4];
void build(int u, int l, int r) {
	if(l == r) {
		tr[u] = {l, r, 0};
	} else {
		int mid = l + r >> 1;
		tr[u] = {l, r};
		build(u << 1, l, mid);
		build(u << 1 | 1, mid + 1, r);
	}
}
void pushup(int u) {
	tr[u].mn = min(tr[u << 1].mn, tr[u << 1 | 1].mn);
}
void pushdown1(int u) {
	if(tr[u].add1) {
		tr[u << 1].mn += tr[u].add1;
		tr[u << 1 | 1].mn += tr[u].add1;
		tr[u << 1].add1 += tr[u].add1;
		tr[u << 1 | 1].add1 += tr[u].add1;
		tr[u].add1 = 0;
	}
}
void pushdown2(int u) {
	if(tr[u].x) {
		tr[u << 1].mn = abs(tr[u].x - tr[u << 1].l) + tr[u].add2;
		tr[u << 1 | 1].mn = abs(tr[u << 1 | 1].l - tr[u].x) + tr[u].add2;
		tr[u << 1].x = tr[u].x;
		tr[u << 1].add2 = tr[u].add2;
		tr[u << 1 | 1].x = tr[u].x;
		tr[u << 1 | 1].add2 = tr[u].add2;
		tr[u].x = 0;
		tr[u].add2 = 0;
	}
}
void modify1(int u, int l, int r) {
	if(tr[u].l >= l&&tr[u].r <= r) {
		tr[u].mn += 1;
		tr[u].add1 += 1;
	} else {
		int mid = tr[u].l + tr[u].r >> 1;
		pushdown1(u);
		if(l <= mid) modify1(u << 1, l, r);
		if(r > mid) modify1(u << 1 | 1, l, r);
		pushup(u);
	}
}
void modify2(int u, int l, int r, int x, int d) {
	if(tr[u].l >= l&&tr[u].r <= r) {
		tr[u].mn = tr[u].l - x + d;
		tr[u].x = x;
		tr[u].add2 = d;
	} else {
		int mid = tr[u].l + tr[u].r >> 1;
		pushdown2(u);
		if(l <= mid) modify2(u << 1, l, r, x, d);
		if(r > mid) modify2(u << 1 | 1, l, r, x, d);
		pushup(u);
	}
}
int query(int u, int l, int r) {
	if(tr[u].l >= l&&tr[u].r <= r) {
		return tr[u].mn;
	} else {
		int mid = tr[u].l + tr[u].r >> 1;
		pushdown2(u);
		pushdown1(u);
		int ans = INF;
		if(l <= mid) ans = min(ans, query(u << 1, l, r));
		if(r > mid) ans = min(ans, query(u << 1 | 1, l, r));
		return ans;
	}
}
int main() {
	cin>>h>>w;
	for(int i = 1;i <= h;i++) {
		scanf("%d %d", &a[i], &b[i]);
	}
	build(1, 1, w);
	int st = 1, f1 = 0;
	for(int i = 1;i <= h;i++) {
		if(f1) {
			cout<<"-1\n";
			continue;
		}
		int res = INF, tmp = st;
		if(st <= a[i] - 1) {
			res = min(res, query(1, st, a[i] - 1));
		} else {
			tmp = max(st, b[i] + 1);
		}
		if(st <= b[i]&&b[i] + 1 <= w) {
			res = min(res, query(1, b[i] + 1, w));
		} else if(st > b[i]&&b[i] + 1 <= w&&st <= w) {
			res = min(res, query(1, st, w));
		}
		if(res == INF) {
			cout<<"-1\n";
			f1 = 1;
		} else {
			cout<<res + 1<<"\n";
		}
		if(st <= a[i] - 1) {
			modify1(1, st, a[i] - 1);
			modify2(1, a[i], b[i], a[i] - 1, query(1, a[i] - 1, a[i] - 1));
			if(b[i] + 1 <= w)
			modify1(1, b[i] + 1, w);
		} else {
			if(b[i] + 1 <= w) modify1(1, b[i] + 1, w);
		}
		st = tmp;
	}
	return 0;
}