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: Wrong Answer)

package main

import (
	"bufio"
	"container/heap"
	"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)
	}

	dp := make([]int, w)
	pq := &pqAsc{}
	for i := 0; i < w; i++ {
		heap.Push(pq, pair{0, i})
	}

	for i := 0; i < h; i++ {
		a, b := scanInt()-1, scanInt()-1
		s := bit.sum(a-1) + 1
		for {
			k := bit.lowerbound(s)
			if k > b {
				break
			}
			bit.add(k, -1)
			dp[k] = -1
		}

		if b+1 != w && bit.sum(b+1) == bit.sum(b) {
			k := bit.lowerbound(bit.sum(b + 1))
			dp[b+1] = b + 1 - k
			bit.add(b+1, 1)
			heap.Push(pq, pair{dp[b+1], b + 1})
		}

		for len(*pq) != 0 && dp[(*pq)[0].b] != (*pq)[0].a {
			heap.Pop(pq)
		}

		if len(*pq) == 0 {
			fmt.Fprintln(wr, -1)
		} else {
			fmt.Fprintln(wr, (*pq)[0].a+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 pair struct{ a, b int }

type pqAsc []pair

func (pq pqAsc) Len() int            { return len(pq) }
func (pq pqAsc) Swap(i, j int)       { pq[i], pq[j] = pq[j], pq[i] }
func (pq pqAsc) Less(i, j int) bool  { return pq[i].a < pq[j].a }
func (pq *pqAsc) Push(x interface{}) { *pq = append(*pq, x.(pair)) }
func (pq *pqAsc) Pop() interface{} {
	x := (*pq)[len(*pq)-1]
	*pq = (*pq)[0 : len(*pq)-1]
	return x
}