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 <stdio.h>

typedef struct {
	int key, id;
} data;

typedef struct {
	data obj[400001];
	int size;
} min_heap;

void push(data x, min_heap* h)
{
	int i = ++(h->size);
	data tmp;
	h->obj[i] = x;
	while (i > 1) {
		if (h->obj[i].key < h->obj[i>>1].key) {
			tmp = h->obj[i>>1];
			h->obj[i>>1] = h->obj[i];
			h->obj[i] = tmp;
			i >>= 1;
		} else break;
	}
}

data pop(min_heap* h)
{
	int i = 1, j = 2;
	data output = h->obj[1], tmp;
	h->obj[1] = h->obj[(h->size)--];
	while (j <= h->size) {
		if (j < h->size && h->obj[j^1].key < h->obj[j].key) j ^= 1;
		if (h->obj[j].key < h->obj[i].key) {
			tmp = h->obj[j];
			h->obj[j] = h->obj[i];
			h->obj[i] = tmp;
			i = j;
			j = i << 1;
		} else break;
	}
	return output;
}

int main()
{
	int i, H, W, A[200001], B[200001];
	scanf("%d %d", &H, &W);
	for (i = 1; i <= H; i++) scanf("%d %d", &(A[i]), &(B[i]));
	
	int j, k, l[200002], r[200002], left[200002], right[200002], L, R, M;
	data d;
	min_heap h;
	h.size = 0;
	for (i = 0; i <= W + 1; i++) {
		l[i] = i;
		r[i] = i + 1;
		left[i] = i - 1;
		right[i] = i + 1;
	}
	for (i = 1, d.key = 0; i <= W; i++) {
		d.id = i;
		push(d, &h);
	}
	for (i = 1; i <= H; i++) {
		L = 0;
		R = W;
		while (L < R) {
			M = (L + R + 1) / 2;
			if (l[M] < A[i]) L = M;
			else R = M - 1;
		}
		for (; l[L] == r[L] || l[L] >= A[i]; L = left[L]);
		r[L] = B[i] + 1;
		for (j = right[L]; l[j] <= B[i]; j = right[j]) {
			if (r[j] <= B[i] + 1) {
				l[j] = r[j];
				right[L] = right[j];
				left[right[j]] = L;
			} else break;
		}
		if (l[j] <= B[i]) {
			l[j] = B[i] + 1;
			right[L] = j;
			left[j] = L;
			d.key = l[j] - j;
			d.id = j;
			push(d, &h);
		}
		
		while (h.size > 0 && l[h.obj[1].id] - h.obj[1].id > h.obj[1].key) pop(&h);
		if (h.size > 0) printf("%d\n", h.obj[1].key + i);
		else printf("-1\n");
	}
	
	fflush(stdout);
	return 0;
}