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 Rust (1.42.0) (judge verdict: Wrong Answer)

//  ____            _                  _                       _       _
// |  _ \ _   _ ___| |_    ___ _ __   | |_ ___ _ __ ___  _ __ | | __ _| |_ ___
// | |_) | | | / __| __|  / __| '_ \  | __/ _ \ '_ ` _ \| '_ \| |/ _` | __/ _ \
// |  _ <| |_| \__ \ |_  | (__| |_) | | ||  __/ | | | | | |_) | | (_| | ||  __/
// |_| \_\\__,_|___/\__|  \___| .__/___\__\___|_| |_| |_| .__/|_|\__,_|\__\___|
//                            |_| |_____|               |_|

//https://github.com/manta1130/Competitive_Programming_Template_Rust

mod segtree {

///汎用のセグメント木
pub struct SegTreeBasic<T, F> {
    //n: usize,
    v: Vec<T>,
    init: T,
    parent_generator: F,
}

impl<T, F> SegTreeBasic<T, F>
where
    T: PartialOrd + Clone + Copy,
    F: Fn(T, T) -> T,
{
    ///セグメント木を構築する。
    ///
    /// # Examples
    /// ```ignore
    /// let mut segtree = SegTreeBasic::new(100, usize::max_value(), |a, b| std::cmp::min(a, b));
    /// //要素数100のRMQを構築する。
    /// ```
    pub fn new(n: usize, init: T, parent_generator: F) -> SegTreeBasic<T, F> {
        let mut size = 1;
        while size < n {
            size *= 2;
        }
        let v = vec![init; size * 2 - 1];
        SegTreeBasic {
            //n: size,
            v: v,
            init: init,
            parent_generator: parent_generator,
        }
    }

    ///[l,r)の要素をparent_generatorに基づいて計算する。
    pub fn get(&mut self, l: usize, r: usize) -> T {
        let vlen = self.v.len();
        self._get(l, r, 0, 0, (vlen + 1) / 2)
    }

    fn _get(&mut self, l: usize, r: usize, now: usize, a: usize, b: usize) -> T {
        if a >= r || b <= l {
            self.init
        } else if l <= a && b <= r {
            self.v[now]
        } else {
            let bufa = self._get(l, r, now * 2 + 1, a, (a + b) / 2);
            let bufb = self._get(l, r, now * 2 + 2, (a + b) / 2, b);
            let cond = &self.parent_generator;
            cond(bufa, bufb)
        }
    }

    ///任意の要素を書き換える。
    pub fn update(&mut self, index: usize, value: T) {
        let offset = (self.v.len() + 1) / 2 - 1;
        self.v[offset + index] = value;
        let mut parent = index + offset;
        loop {
            parent = (parent - 1) / 2;
            let cond = &self.parent_generator;
            self.v[parent] = cond(self.v[parent * 2 + 1], self.v[parent * 2 + 2]);
            if parent == 0 {
                break;
            }
        }
    }
}

///汎用の遅延セグメント木
pub struct SegTreeLazy<T, F, G, H> {
    //n: usize,
    pub v: Vec<T>,
    pub lazy: Vec<T>,
    pub lazy_flag: Vec<bool>,
    init: T,
    lazy_init: T,
    lazy_convert: F,
    lazy_propagation: G,
    parent_generator: H,
}

impl<T, F, G, H> SegTreeLazy<T, F, G, H>
where
    T: PartialOrd + Clone + Copy,
    F: Fn(T, T) -> T,
    G: Fn(T, T) -> T,
    H: Fn(T, T) -> T,
{
    ///遅延セグメント木を構築する。
    ///
    /// # Examples
    /// ```ignore
    ///　let mut segtree = SegTreeLazy::new(100, 0, 0, |a, b| a + b, |a, b| a + b, |a, b| a + b / 2);
    /// //要素数100の区間加算・区間取得のセグ木を構築する。
    /// ```
    pub fn new(
        n: usize,
        init: T,
        lazy_init: T,
        parent_generator: H,
        lazy_convert: F,
        lazy_propagation: G,
    ) -> SegTreeLazy<T, F, G, H> {
        let mut size = 1;
        while size < n {
            size *= 2;
        }
        let v = vec![init; size * 2 - 1];
        let lazy = vec![lazy_init; size * 2 - 1];
        let lazy_flag = vec![false; size * 2 - 1];
        SegTreeLazy {
            v: v,
            lazy: lazy,
            init: init,
            lazy_init: lazy_init,
            lazy_flag: lazy_flag,
            lazy_convert: lazy_convert,
            lazy_propagation: lazy_propagation,
            parent_generator: parent_generator,
        }
    }

    ///[l,r)の要素をparent_generatorに基づいて計算する。
    pub fn get(&mut self, l: usize, r: usize) -> T {
        let vlen = self.v.len();
        self._get(l, r, 0, 0, (vlen + 1) / 2)
    }

    fn _eval(&mut self, now: usize) {
        if now >= self.lazy_flag.len() {
            return;
        }

        if now * 2 + 2 < self.lazy.len() && self.lazy[now] != self.lazy_init {
            let propagation = &self.lazy_propagation;
            self.lazy[now * 2 + 1] = propagation(self.lazy[now * 2 + 1], self.lazy[now]);
            self.lazy[now * 2 + 2] = propagation(self.lazy[now * 2 + 2], self.lazy[now]);
        }
        if self.lazy[now] != self.lazy_init {
            let convert = &self.lazy_convert;
            self.v[now] = convert(self.v[now], self.lazy[now]);
        }
        self.lazy[now] = self.lazy_init;
        if self.lazy_flag[now] {
            self._eval(now * 2 + 1);
            self._eval(now * 2 + 2);
            let generator = &self.parent_generator;
            if now * 2 + 2 < self.v.len() {
                self.v[now] = generator(self.v[now * 2 + 1], self.v[now * 2 + 2]);
            }
        }
        self.lazy_flag[now] = false;
    }

    fn _get(&mut self, l: usize, r: usize, now: usize, a: usize, b: usize) -> T {
        self._eval(now);
        if a >= r || b <= l {
            self.init
        } else if l <= a && b <= r {
            self.v[now]
        } else {
            let bufa = self._get(l, r, now * 2 + 1, a, (a + b) / 2);
            let bufb = self._get(l, r, now * 2 + 2, (a + b) / 2, b);
            let cond = &self.parent_generator;
            cond(bufa, bufb)
        }
    }

    ///任意の要素を書き換える。
    pub fn update(&mut self, l: usize, r: usize, v: T) {
        let vlen = self.v.len();
        self._update(l, r, 0, 0, (vlen + 1) / 2, v);
    }

    fn _update(&mut self, l: usize, r: usize, now: usize, a: usize, b: usize, v: T) {
        if now < self.v.len() {
            self._eval(now);
        }
        if l <= a && b <= r {
            let propagation = &self.lazy_propagation;
            self.lazy[now] = propagation(self.lazy[now], v);
            self.lazy_flag[now] = true;
        } else if !(a >= r || b <= l) {
            let v_next;
            {
                let propagation = &self.lazy_propagation;
                v_next = propagation(self.lazy_init, v);
            }
            self._update(l, r, now * 2 + 1, a, (a + b) / 2, v_next);
            self._update(l, r, now * 2 + 2, (a + b) / 2, b, v_next);
            self.lazy_flag[now] = true;
        }
    }
}
}

  #[allow(unused_imports)]
use proconio::{
    fastout, input,
    marker::{Bytes, Chars, Isize1, Usize1},
};

use segtree::*;

const INF: isize = 999999999999;

#[fastout]
fn main() {
    input! {
        h:usize,w:usize,
    }
    let mut p = SegTreeLazy::new(
        w + 1,
        (INF, 0),
        (-1, -1),
        |a, b| {
            if a.0 - a.1 < b.0 - b.1 {
                (a.0, a.1)
            } else {
                (b.0, b.1)
            }
        },
        |a, b| {
            if b.1 != INF {
                b
            } else {
                if a.0 < b.0 {
                    (b.0, a.1)
                } else {
                    (a.0, a.1)
                }
            }
        },
        |a, b| {
            if b.1 != INF {
                b
            } else if a.0 < b.0 {
                b
            } else {
                a
            }
        },
    );
    let mut q = SegTreeLazy::new(
        w + 1,
        INF,
        INF,
        |a, b| std::cmp::min(a, b),
        |a, b| std::cmp::min(a, b),
        |a, b| std::cmp::min(a, b),
    );
    for i in 0..w as isize {
        p.update(i as usize, i as usize + 1, (i, i));
        q.update(i as usize, i as usize + 1, i);
    }
    p.update(w as usize, w as usize + 1, (INF, w as isize));
    q.update(w as usize, w as usize + 1, w as isize);

    for k in 0..h {
        input! {
            a:Usize1,b:Usize1,
        }
        let from = q.get(a, a + 1) as usize;
        let to = q.get(b + 1, b + 2) as usize;

        if b + 1 != w {
            q.update(from, to + 1, from as isize);
            p.update(from, to, (b as isize + 1, INF));
        } else {
            q.update(from, w, from as isize);
            p.update(from, w, (INF, INF));
        }

        /*
        println!("p:{} to {} value:{}", start, b + 1, b + 1);
        println!("q:{} to {} value:{}", start, b + 1, start);
        for i in 0..w {
            print!("{} ", q.get(i, i + 1));
        }
        println!("");
        for i in 0..w {
            print!("{} ", p.get(i, i + 1).0);
        }
        println!("");
        */

        let ans = p.get(0, w);
        if ans.0 > INF / 2 {
            println!("-1");
        } else {
            println!("{}", ans.0 - ans.1 + k as isize + 1);
        }
    }
}