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"
	"fmt"
	"os"
	"strconv"
)

func configure(scanner *bufio.Scanner) {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
}
func getNextString(scanner *bufio.Scanner) string {
	scanned := scanner.Scan()
	if !scanned {
		panic("scan failed")
	}
	return scanner.Text()
}
func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}
func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}
func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}
func main() {
	fp := os.Stdin
	wfp := os.Stdout
	extra := 0
	if os.Getenv("I") == "IronMan" {
		fp, _ = os.Open(os.Getenv("END_GAME"))
		extra = 100
	}
	scanner := bufio.NewScanner(fp)
	configure(scanner)
	writer := bufio.NewWriter(wfp)
	defer func() {
		r := recover()
		if r != nil {
			fmt.Fprintln(writer, r)
		}
		writer.Flush()
	}()
	solve(scanner, writer)
	for i := 0; i < extra; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
}
func solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	h := getNextInt(scanner)
	w := getNextInt(scanner)

	rbt := newTreeWithIntComparator()
	rbt2 := newTreeWithIntComparator()
	for i := 0; i < w; i++ {
		rbt.Put(i, i)
	}
	rbt2.Put(0, w)

	for i := 0; i < h; i++ {
		a := getNextInt(scanner) - 1
		b := getNextInt(scanner)
		x := -1
		for node, ok := rbt.Ceiling(a); ok && node.Key.(int) <= b; node, ok = rbt.Ceiling(a) {
			k := node.Key.(int)
			v := node.Value.(int)
			rbt.Remove(k)
			if x < v {
				x = v
			}
			old, _ := rbt2.Get(k - v)
			if old.(int) == 1 {
				rbt2.Remove(k - v)
			} else {
				rbt2.Put(k-v, old.(int)-1)
			}
		}
		if b < w && x != -1 {
			old, found := rbt.Get(b)
			if found && old.(int) > x {
				x = old.(int)
			}
			rbt.Put(b, x)
			old, found = rbt2.Get(b - x)
			if found {
				rbt2.Put(b-x, old.(int)+1)
			} else {
				rbt2.Put(b-x, 1)
			}
		}
		if rbt2.Empty() {
			fmt.Fprintln(writer, -1)
			continue
		}
		fmt.Fprintln(writer, rbt2.Left().Key.(int)+i+1)
	}
}

type tree struct {
	Root       *node
	Comparator func(a, b interface{}) int
}
type node struct {
	Left, Right, Parent *node
	Key                 interface{}
	Value               interface{}
	size                int
}

func newTreeWithIntComparator() *tree {
	return newTree(func(a, b interface{}) int { return a.(int) - b.(int) })
}
func newTree(comparator func(interface{}, interface{}) int) *tree {
	return &tree{Root: nil, Comparator: comparator}
}
func newNode(parent *node, key, value interface{}) *node {
	return &node{Parent: parent, Key: key, Value: value, size: 1}
}
func (n *node) IsRoot() bool {
	return n.Parent == nil
}
func (n *node) IsLeft() bool {
	return !n.IsRoot() && n.Parent.Left == n
}
func (n *node) IsRight() bool {
	return !n.IsRoot() && n.Parent.Right == n
}
func (n *node) Update() {
	n.size = 1 + n.Left.Size() + n.Right.Size()
}
func (n *node) Size() int {
	if n == nil {
		return 0
	}
	return n.size
}
func (n *node) Merge(m *node) *node {
	if n == nil {
		return m
	}
	if m == nil {
		return n
	}
	if n.Size() > m.Size() {
		n.Left = n.Left.Merge(n.Right)
		n.Right = m
		if n.Left != nil {
			n.Left.Parent = n
		}
		if n.Right != nil {
			n.Right.Parent = n
		}
		n.Update()
		return n
	}
	m.Right = m.Left.Merge(m.Right)
	m.Left = n
	if m.Left != nil {
		m.Left.Parent = m
	}
	if m.Right != nil {
		m.Right.Parent = m
	}
	m.Update()
	return m
}
func (n *node) MaximumNode() *node {
	if n.Right != nil {
		return n.Right.MaximumNode()
	}
	return n
}
func (n *node) MinimumNode() *node {
	if n.Left != nil {
		return n.Left.MinimumNode()
	}
	return n
}
func (t *tree) Put(key, value interface{}) {
	if t.Root == nil {
		t.Root = newNode(nil, key, value)
		return
	}
	now := t.Root
	for {
		compared := t.Comparator(key, now.Key)
		switch {
		case compared == 0:
			now.Value = value
			return
		case compared < 0:
			if now.Left == nil {
				now.Left = newNode(now, key, value)
				t.UpdateSize(now)
				return
			}
			if now.Left.Size() > now.Right.Size() {
				t.RotateRight(now.Left)
				now = now.Parent
			} else {
				now = now.Left
			}
		case compared > 0:
			if now.Right == nil {
				now.Right = newNode(now, key, value)
				t.UpdateSize(now)
				return
			}
			if now.Left.Size() < now.Right.Size() {
				t.RotateLeft(now.Right)
				now = now.Parent
			} else {
				now = now.Right
			}
		}
	}
}
func (t *tree) Get(key interface{}) (interface{}, bool) {
	now := t.Lookup(key)
	if now == nil {
		return nil, false
	}
	return now.Value, true
}
func (t *tree) Lookup(key interface{}) *node {
	now := t.Root
	for now != nil {
		compared := t.Comparator(key, now.Key)
		switch {
		case compared == 0:
			return now
		case compared < 0:
			now = now.Left
		case compared > 0:
			now = now.Right
		}
	}
	return nil
}
func (t *tree) Remove(key interface{}) {
	now := t.Lookup(key)
	if now == nil {
		return
	}
	parent := now.Parent

	switch {
	case now.IsRoot():
		t.Root = now.Left.Merge(now.Right)
		if t.Root != nil {
			t.Root.Parent = nil
		}
	case now.IsLeft():
		t.AppendLeft(parent, now.Left.Merge(now.Right))
	case now.IsRight():
		t.AppendRight(parent, now.Left.Merge(now.Right))
	}
	t.UpdateSize(parent)
}
func (t *tree) UpdateSize(now *node) {
	for now != nil {
		now.Update()
		now = now.Parent
	}
}
func (t *tree) AppendLeft(Parent, child *node) {
	Parent.Left = child
	Parent.Update()
	if child == nil {
		return
	}
	child.Parent = Parent
}
func (t *tree) AppendRight(Parent, child *node) {
	Parent.Right = child
	Parent.Update()
	if child == nil {
		return
	}
	child.Parent = Parent
}
func (t *tree) RotateLeft(now *node) {
	Parent := now.Parent
	grand := Parent.Parent
	t.AppendRight(Parent, now.Left)
	t.AppendLeft(now, Parent)
	switch {
	case grand == nil:
		t.Root = now
		now.Parent = nil
	case grand.Left == Parent:
		t.AppendLeft(grand, now)
	case grand.Right == Parent:
		t.AppendRight(grand, now)
	}
}
func (t *tree) RotateRight(now *node) {
	Parent := now.Parent
	grand := Parent.Parent
	t.AppendLeft(Parent, now.Right)
	t.AppendRight(now, Parent)
	switch {
	case grand == nil:
		t.Root = now
		now.Parent = nil
	case grand.Left == Parent:
		t.AppendLeft(grand, now)
	case grand.Right == Parent:
		t.AppendRight(grand, now)
	}
}
func (t *tree) Right() *node {
	now := t.Root
	if now == nil {
		return now
	}
	return now.MaximumNode()
}
func (t *tree) Left() *node {
	now := t.Root
	if now == nil {
		return now
	}
	return now.MinimumNode()
}
func (t *tree) Floor(key interface{}) (*node, bool) {
	now := t.Root
	var n *node
	for now != nil {
		compared := t.Comparator(key, now.Key)
		switch {
		case compared == 0:
			return now, true
		case compared < 0:
			now = now.Left
		case compared > 0:
			n = now
			now = now.Right
		}
	}
	return n, n != nil
}
func (t *tree) Ceiling(key interface{}) (*node, bool) {
	now := t.Root
	var n *node
	for now != nil {
		compared := t.Comparator(key, now.Key)
		switch {
		case compared == 0:
			return now, true
		case compared < 0:
			n = now
			now = now.Left
		case compared > 0:
			now = now.Right
		}
	}
	return n, n != nil
}
func (t *tree) Empty() bool {
	return t.Root == nil
}