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 Go (1.14.1) (judge verdict: Accepted)

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var sc, wr = bufio.NewScanner(os.Stdin), bufio.NewWriter(os.Stdout)

func scanString() string   { sc.Scan(); return sc.Text() }
func scanRunes() []rune    { return []rune(scanString()) }
func scanInt() int         { a, _ := strconv.Atoi(scanString()); return a }
func scanInt64() int64     { a, _ := strconv.ParseInt(scanString(), 10, 64); return a }
func scanFloat64() float64 { a, _ := strconv.ParseFloat(scanString(), 64); return a }

func scanInts(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = scanInt()
	}
	return res
}

func debug(a ...interface{}) { fmt.Fprintln(os.Stderr, a...) }

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}
func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}
func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

//•*¨*•.¸¸♪main•*¨*•.¸¸♪(　-ω-)ノ　(　・ω・)
func main() {
	defer wr.Flush()
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 10000), 1001001)

	h, w := scanInt(), scanInt()
	bit := binaryIndexedTreeInit(w)
	for i := 0; i < w; i++ {
		bit.add(i, 1)
	}

	inf := 1 << 60
	seg := segmentTreeInit(w, min, inf)
	seg.build(make([]int, w))

	for i := 0; i < h; i++ {
		a, b := scanInt()-1, scanInt()-1

		if b+1 < w && seg.data(b+1) == inf {
			s := bit.sum(b + 1)
			if s != 0 {
				k := bit.lowerbound(s)
				bit.add(b+1, 1)
				seg.update(b+1, b+1-k+seg.data(k))
			}
		}

		s := bit.sum(a-1) + 1
		for {
			k := bit.lowerbound(s)
			if k > b {
				break
			}
			bit.add(k, -1)
			seg.update(k, inf)
		}

		ans := seg.query(0, w)
		if ans == inf {
			fmt.Fprintln(wr, -1)
		} else {
			fmt.Fprintln(wr, ans+i+1)
		}
	}

}

type binaryIndexedTree []int

func binaryIndexedTreeInit(n int) binaryIndexedTree {
	return make(binaryIndexedTree, n+1)
}

func (t binaryIndexedTree) sum(i int) (s int) {
	for i++; i > 0; i -= i & -i {
		s += t[i]
	}
	return
}
func (t binaryIndexedTree) add(i, x int) {
	for i++; i < len(t) && i > 0; i += i & -i {
		t[i] += x
	}
}

func (t binaryIndexedTree) lowerbound(x int) int {
	idx, k := 0, 1
	for k < len(t) {
		k <<= 1
	}
	for k >>= 1; k > 0; k >>= 1 {
		if idx+k < len(t) && t[idx+k] < x {
			x -= t[idx+k]
			idx += k
		}
	}
	return idx
}

type segmentTree struct {
	arr []int
	e   int
	n   int
	fn  func(a, b int) int
}

func segmentTreeInit(n int, fn func(a, b int) int, e int) segmentTree {
	a := 1
	for a < n {
		a <<= 1
	}
	res := segmentTree{make([]int, 2*a), e, a, fn}
	for i := 0; i < 2*a; i++ {
		res.arr[i] = e
	}
	return res
}

func (seg segmentTree) build(arr []int) {
	for i := 0; i < len(arr); i++ {
		seg.arr[i+seg.n] = arr[i]
	}
	for i := seg.n - 1; i > 0; i-- {
		seg.arr[i] = seg.fn(seg.arr[2*i], seg.arr[2*i+1])
	}
}

func (seg segmentTree) update(i, a int) {
	i += seg.n
	seg.arr[i] = a
	for i >>= 1; i > 0; i >>= 1 {
		seg.arr[i] = seg.fn(seg.arr[2*i], seg.arr[2*i+1])
	}
}

func (seg segmentTree) query(a, b int) int {
	var query func(a, b, k, l, r int) int
	query = func(a, b, k, l, r int) int {
		if r <= a || b <= l {
			return seg.e
		}
		if a <= l && r <= b {
			return seg.arr[k]
		}
		return seg.fn(query(a, b, 2*k, l, (l+r)/2), query(a, b, 2*k+1, (l+r)/2, r))
	}
	return query(a, b, 1, 0, seg.n)
}

func (seg segmentTree) data(i int) int {
	return seg.arr[i+seg.n]
}