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: Runtime Error)

// EbTech's Rust template. Thanks EbTech!
#![allow(unused_imports)]
use std::cmp::{max, min, Reverse};
use std::collections::{BinaryHeap, HashMap, HashSet};
use std::io::{self, prelude::*};
use std::str;

struct Scanner<R> {
    reader: R,
    buf_str: Vec<u8>,
    buf_iter: str::SplitWhitespace<'static>,
}
impl<R: BufRead> Scanner<R> {
    fn new(reader: R) -> Self {
        Self {
            reader,
            buf_str: vec![],
            buf_iter: "".split_whitespace(),
        }
    }
    fn next<T: str::FromStr>(&mut self) -> T {
        loop {
            if let Some(token) = self.buf_iter.next() {
                return token.parse().ok().expect("Failed parse");
            }
            self.buf_str.clear();
            self.reader
                .read_until(b'\n', &mut self.buf_str)
                .expect("Failed read");
            self.buf_iter = unsafe {
                let slice = str::from_utf8_unchecked(&self.buf_str);
                std::mem::transmute(slice.split_whitespace())
            }
        }
    }
}

fn solve<R: BufRead, W: Write>(scan: &mut Scanner<R>, wrt: &mut W) {
    let h: usize = scan.next();
    let w: usize = scan.next();
    let mut mark = vec![vec![false; w]; h + 1];
    for i in 0..h {
        let a: usize = scan.next();
        let b: usize = scan.next();
        for j in a - 1..b {
            mark[i][j] = true;
        }
    }
    let mark = mark;
    let mut opt = vec![vec![0; w]; h + 1];

    let mut blocked = vec![vec![false; w]; h + 1];
    for r in 1..h + 1 {
        let c = 0;
        if mark[r - 1][c] || blocked[r-1][c] {
            blocked[r][c] = true;
        } else {
            opt[r][c] = opt[r - 1][c] + 1
        }
    }
    // dbg!(&blocked);
    for r in 1..h + 1 {
        let mut res = if blocked[r][0] { None } else { Some(opt[r][0]) };
        for c in 1..w {
            if mark[r - 1][c] {
                // cannot come from top
                if blocked[r][c - 1] {
                    //cannot come from left
                    blocked[r][c] = true;
                } else {
                    opt[r][c] = opt[r][c - 1] + 1;
                }
            } else {
                if blocked[r][c - 1] {
                    //cannot come from left
                    opt[r][c] = opt[r - 1][c] + 1;
                } else {
                    opt[r][c] = min(opt[r - 1][c], opt[r][c - 1]) + 1;
                }
            }
            if !blocked[r][c] {
                res = if let Some(prev) = res {
                    Some(min(prev, opt[r][c]))
                } else {
                    Some(opt[r][c])
                };
            }
        }
        writeln!(wrt, "{}", if let Some(res) = res { res as i64 } else { -1 }).unwrap();
    }
}

fn main() {
    let (stdin, stdout) = (io::stdin(), io::stdout());
    let mut scan = Scanner::new(stdin.lock());
    let mut out = io::BufWriter::new(stdout.lock());
    // let t: u64 = scan.next();
    let t = 1;
    for _ in 0..t {
        solve(&mut scan, &mut out);
    }
}