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)

#![allow(unused_imports, unused_macros, dead_code)]

macro_rules! min {
    (.. $x:expr) => {{
        let mut it = $x.iter();
        it.next().map(|z| it.fold(z, |x, y| min!(x, y)))
    }};
    ($x:expr) => ($x);
    ($x:expr, $($ys:expr),*) => {{
        let t = min!($($ys),*);
        if $x < t { $x } else { t }
    }}
}
macro_rules! max {
    (.. $x:expr) => {{
        let mut it = $x.iter();
        it.next().map(|z| it.fold(z, |x, y| max!(x, y)))
    }};
    ($x:expr) => ($x);
    ($x:expr, $($ys:expr),*) => {{
        let t = max!($($ys),*);
        if $x > t { $x } else { t }
    }}
}
macro_rules! trace {
    ($x:expr) => {
        #[cfg(debug_assertions)]
        eprintln!(">>> {} = {:?}", stringify!($x), $x)
    };
    ($($xs:expr),*) => { trace!(($($xs),*)) }
}
macro_rules! flush {
    () => {
        std::io::stdout().flush().unwrap();
    };
}
macro_rules! put {
    (.. $x:expr) => {{
        let mut it = $x.iter();
        if let Some(x) = it.next() { print!("{}", x); }
        for x in it { print!(" {}", x); }
        println!("");
    }};
    ($x:expr) => { println!("{}", $x) };
    ($x:expr, $($xs:expr),*) => { print!("{} ", $x); put!($($xs),*) }
}

const M: i64 = 1_000_000_007;

// @seq.segment_tree.rs
// @algebra.monoid.rs
trait Monoid: std::ops::Mul<Output = Self> + Clone + Copy {
    fn unit() -> Self;
}
impl Monoid for i64 {
    fn unit() -> Self {
        0
    }
}
impl Monoid for f64 {
    fn unit() -> Self {
        0.0
    }
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
enum MaxInt<X> {
    Minimal,
    Val(X),
}
impl<X> MaxInt<X> {
    fn unwrap(self) -> X {
        if let Self::Val(x) = self {
            x
        } else {
            panic!();
        }
    }
}
impl<X: Ord> std::ops::Mul for MaxInt<X> {
    type Output = Self;
    fn mul(self, other: Self) -> Self {
        if self > other {
            self
        } else {
            other
        }
    }
}
impl<X: Ord + Copy> Monoid for MaxInt<X> {
    fn unit() -> Self {
        MaxInt::Minimal
    }
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
enum MinInt<X> {
    Val(X),
    Maximal,
}
impl<X> MinInt<X> {
    fn unwrap(self) -> X {
        if let Self::Val(x) = self {
            x
        } else {
            panic!();
        }
    }
}
impl<X: Ord> std::ops::Mul for MinInt<X> {
    type Output = Self;
    fn mul(self, other: Self) -> Self {
        if self < other {
            self
        } else {
            other
        }
    }
}
impl<X: Ord + Copy> Monoid for MinInt<X> {
    fn unit() -> Self {
        MinInt::Maximal
    }
}

trait Act<X> {
    fn act(&self, x: X) -> X;
}

#[derive(Debug, Clone, Copy)]
enum AssignInt<X> {
    None,
    Val(X),
}

impl<X: Copy> std::ops::Mul for AssignInt<X> {
    type Output = Self;
    fn mul(self, other: Self) -> Self {
        match (self, other) {
            (x, AssignInt::None) => x,
            _ => other,
        }
    }
}

impl<X: Copy> Monoid for AssignInt<X> {
    fn unit() -> Self {
        return AssignInt::None;
    }
}

impl<X: Copy> Act<MinInt<X>> for AssignInt<X> {
    fn act(&self, x: MinInt<X>) -> MinInt<X> {
        match (x, self) {
            (_, AssignInt::Val(m)) => MinInt::Val(*m),
            _ => x,
        }
    }
}

impl<X: Copy> Act<MaxInt<X>> for AssignInt<X> {
    fn act(&self, x: MaxInt<X>) -> MaxInt<X> {
        match (x, self) {
            (_, AssignInt::Val(m)) => MaxInt::Val(*m),
            _ => x,
        }
    }
}

#[derive(Clone)]
struct LazySegmentTree<X, M> {
    size: usize,
    size_upper: usize, // power of 2
    depth: usize,
    data: Vec<Vec<X>>,
    act: Vec<Vec<M>>,
}
impl<X: Monoid, M: Monoid + Act<X>> LazySegmentTree<X, M> {
    fn new(size: usize) -> Self {
        let mut data = vec![];
        let mut act = vec![];
        let mut i = 1;
        loop {
            data.push(vec![X::unit(); i]);
            act.push(vec![M::unit(); i]);
            if i >= size {
                break;
            }
            i *= 2;
        }
        LazySegmentTree {
            size,
            size_upper: i,
            depth: data.len(),
            data,
            act,
        }
    }
    fn from(xs: Vec<X>) -> Self {
        let mut tree = Self::new(xs.len());
        for i in 0..xs.len() {
            tree.data[tree.depth - 1][i] = xs[i];
        }
        for depth in (0..tree.depth - 1).rev() {
            for i in 0..tree.data[depth].len() {
                tree.data[depth][i] = tree.data[depth + 1][2 * i] * tree.data[depth + 1][2 * i + 1];
            }
        }
        tree
    }
    fn propagation(&mut self, depth: usize, idx: usize) {
        if depth + 1 < self.depth {
            self.act[depth + 1][idx * 2] = self.act[depth + 1][idx * 2] * self.act[depth][idx];
            self.act[depth + 1][idx * 2 + 1] =
                self.act[depth + 1][idx * 2 + 1] * self.act[depth][idx];
        }
        self.data[depth][idx] = self.act[depth][idx].act(self.data[depth][idx]);
        self.act[depth][idx] = M::unit();
    }
    fn update_sub(
        &mut self,
        range: std::ops::Range<usize>,
        m: M,
        depth: usize,
        idx: usize,
        focus: std::ops::Range<usize>,
    ) {
        if focus.end <= range.start || range.end <= focus.start {
            return;
        }
        self.propagation(depth, idx);
        if range.start <= focus.start && focus.end <= range.end {
            self.act[depth][idx] = self.act[depth][idx] * m;
            self.propagation(depth, idx);
        } else if depth + 1 < self.depth {
            let mid = (focus.start + focus.end) / 2;
            self.update_sub(range.clone(), m, depth + 1, idx * 2, focus.start..mid);
            self.update_sub(range.clone(), m, depth + 1, idx * 2 + 1, mid..focus.end);
            self.data[depth][idx] =
                self.data[depth + 1][idx * 2] * self.data[depth + 1][idx * 2 + 1];
        }
    }
    fn update(&mut self, range: std::ops::Range<usize>, m: M) {
        self.update_sub(range, m, 0, 0, 0..self.size_upper);
    }
    fn product_sub(
        &mut self,
        range: std::ops::Range<usize>,
        depth: usize,
        idx: usize,
        focus: std::ops::Range<usize>,
    ) -> X {
        self.propagation(depth, idx);
        if focus.end <= range.start || range.end <= focus.start {
            X::unit()
        } else if range.start <= focus.start && focus.end <= range.end {
            self.data[depth][idx]
        } else {
            let mid = (focus.start + focus.end) / 2;
            let a = self.product_sub(range.clone(), depth + 1, idx * 2, focus.start..mid);
            let b = self.product_sub(range.clone(), depth + 1, idx * 2 + 1, mid..focus.end);
            a * b
        }
    }
    fn product(&mut self, range: std::ops::Range<usize>) -> X {
        self.product_sub(range, 0, 0, 0..self.size_upper)
    }
    fn index(&mut self, i: usize) -> X {
        self.product(i..i + 1)
    }
    fn to_vec(&mut self) -> Vec<X> {
        (0..self.size).map(|i| self.index(i)).collect()
    }
}
impl<X: std::fmt::Debug, M: std::fmt::Debug> LazySegmentTree<X, M> {
    fn debug(&self) {
        for i in 0..self.depth {
            for j in 0..self.data[i].len() {
                eprint!("{:?} / {:?}; ", &self.data[i][j], &self.act[i][j]);
            }
            eprintln!();
        }
    }
}

fn main() {
    let mut sc = Scanner::new();
    let h: usize = sc.cin();
    let w: usize = sc.cin();

    let inf = h + w + 100;

    // 到達可能な最右点
    let mut start = LazySegmentTree::<MaxInt<usize>, AssignInt<usize>>::from(
        (0..w).map(|i| MaxInt::Val(i)).collect(),
    );
    // 右移動距離
    let mut right = LazySegmentTree::<MinInt<usize>, AssignInt<usize>>::from(
        (0..w).map(|_| MinInt::Val(0)).collect(),
    );

    // trace!(start.to_vec());
    // trace!(right.to_vec());
    for k in 0..h {
        let l = sc.cin::<usize>() - 1;
        let r = sc.cin::<usize>();
        // trace!(l, r);
        if l == 0 {
            start.update(l..r, AssignInt::Val(inf));
        } else {
            let left = start.index(l - 1).unwrap();
            start.update(l..r, AssignInt::Val(left));
        }
        right.update(l..r, AssignInt::Val(inf));
        if r < w {
            let x = start.index(r).unwrap();
            if x == inf {
                right.update(r..r + 1, AssignInt::Val(inf));
            } else {
                right.update(r..r + 1, AssignInt::Val(r - x));
            }
        }
        // trace!(start.to_vec());
        // trace!(right.to_vec());

        if let MinInt::Val(x) = right.product(0..w) {
            if x == inf {
                put!(-1);
            } else {
                put!(x + k + 1);
            }
        } else {
            put!(-1);
        }
    }
}

use std::collections::VecDeque;
use std::io::{self, Write};
use std::str::FromStr;

struct Scanner {
    stdin: io::Stdin,
    buffer: VecDeque<String>,
}
impl Scanner {
    fn new() -> Self {
        Scanner {
            stdin: io::stdin(),
            buffer: VecDeque::new(),
        }
    }
    fn cin<T: FromStr>(&mut self) -> T {
        while self.buffer.is_empty() {
            let mut line = String::new();
            let _ = self.stdin.read_line(&mut line);
            for w in line.split_whitespace() {
                self.buffer.push_back(String::from(w));
            }
        }
        self.buffer.pop_front().unwrap().parse::<T>().ok().unwrap()
    }
    fn chars(&mut self) -> Vec<char> {
        self.cin::<String>().chars().collect()
    }
    fn vec<T: FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.cin()).collect()
    }
}