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: Accepted)

// This code is generated by [cargo-atcoder](https://github.com/tanakh/cargo-atcoder)
// Original source code:
/*
use competitive::prelude::*;

#[derive(Debug)]
struct MultiSet<T>(BTreeMap<T, usize>);

impl<T: Ord> MultiSet<T> {
    fn new() -> Self {
        Self(BTreeMap::new())
    }

    fn insert(&mut self, v: T) {
        *self.0.entry(v).or_default() += 1;
    }

    fn remove(&mut self, v: &T) {
        let r = self.0.get_mut(v).unwrap();
        *r -= 1;
        if *r == 0 {
            self.0.remove(v);
        }
    }

    fn min(&self) -> Option<&T> {
        self.0.iter().next().map(|r| r.0)
    }
}

#[argio(output = AtCoder)]
fn main(h: usize, w: usize, ab: [(Usize1, Usize1); h]) -> Vertical<Option<usize>> {
    let mut ss = BTreeSet::new();
    let mut ms = MultiSet::new();

    for i in 0..w {
        ss.insert((i, 0));
        ms.insert(0);
    }

    let mut ret = vec![];

    for (i, (l, r)) in ab.into_iter().enumerate() {
        let r = r + 1;
        let mut rem = vec![];
        let mut minv = usize::max_value();

        for &(c, d) in ss.range((l, 0)..(r, 0)) {
            if r != w {
                let cv = d + r - c;
                minv = min(minv, cv);
            }
            rem.push((c, d));
        }

        for rem in rem {
            ms.remove(&rem.1);
            ss.remove(&rem);
        }

        if minv < usize::max_value() {
            ss.insert((r, minv));
            ms.insert(minv);
        }

        ret.push(ms.min().map(|r| (i + 1 + r)));
    }

    Vertical(ret)
}

// #[derive(Ord, PartialOrd, Eq, PartialEq, Clone, Debug)]
// struct Range {
//     r: usize,
//     l: usize,
//     start: usize,
//     inc: bool,
// }

// impl Range {
//     fn new(l: usize, r: usize, start: usize, inc: bool) -> Self {
//         Self { l, r, start, inc }
//     }

//     fn term(v: usize) -> Self {
//         Self {
//             l: v,
//             r: v,
//             start: 0,
//             inc: false,
//         }
//     }

//     fn cut_l(&self, l: usize) -> Self {
//         Self::new(
//             l,
//             self.r,
//             self.start + if self.inc { 1 } else { 0 } * (l - self.l),
//             self.inc,
//         )
//     }

//     fn cut_r(&self, r: usize) -> Self {
//         Self::new(self.l, r, self.start, self.inc)
//     }

//     fn next_val(&self) -> usize {
//         self.start + if self.inc { 1 } else { 0 } * (self.r - self.l - 1) + 1
//     }
// }

// #[argio(output = AtCoder)]
// fn main(h: usize, w: usize, ab: [(Usize1, Usize1); h]) {
//     let mut ss = BTreeSet::new();
//     let mut ms = MultiSet::new();

//     ss.insert(Range::new(0, w, 0, false));
//     ms.insert(0);

//     for (i, (l, r)) in ab.into_iter().enumerate() {
//         let r = r + 1;
//         // dbg!(l, r);

//         let mut rem = vec![];
//         let mut add = vec![];

//         for rng in ss.range(Range::term(l)..) {
//             if rng.l >= r {
//                 break;
//             }
//             // dbg!(rng);

//             rem.push(rng.clone());

//             if l <= rng.l && rng.r <= r {
//                 continue;
//             }

//             if rng.r <= l || r <= rng.l {
//                 continue;
//             }

//             if l < rng.r && rng.l < l {
//                 add.push(rng.cut_r(l));
//             }

//             if rng.l < r && r < rng.r {
//                 add.push(rng.cut_l(r));
//             }
//         }

//         // dbg!(&add);

//         for rem in rem {
//             ms.remove(&rem.start);
//             ss.remove(&rem);
//         }

//         let mut last = None;
//         if let Some(rng) = ss.range(..=Range::term(l)).rev().next() {
//             last = Some((rng.r, rng.next_val()));
//         }

//         for add in add {
//             if let &Some((la, lv)) = &last {
//                 if la < add.l {
//                     ms.insert(lv);
//                     ss.insert(Range::new(la, add.l, lv, true));
//                 }
//             }
//             last = Some((add.r, add.next_val()));
//             ms.insert(add.start);
//             ss.insert(add);
//         }

//         if let Some((la, lv)) = last {
//             if la < r {
//                 ms.insert(lv);
//                 ss.insert(Range::new(la, r, lv, true));
//             }
//         }

//         // dbg!(&ss);

//         if let Some(v) = ms.min() {
//             println!("{}", i + 1 + v);
//         } else {
//             println!("-1");
//         }
//     }
// }
*/

fn main() {
    let exe = "/tmp/bin8DE5580E";
    std::io::Write::write_all(&mut std::fs::File::create(exe).unwrap(), &decode(BIN)).unwrap();
    std::fs::set_permissions(exe, std::os::unix::fs::PermissionsExt::from_mode(0o755)).unwrap();
    std::process::exit(std::process::Command::new(exe).status().unwrap().code().unwrap())
}

fn decode(v: &str) -> Vec<u8> {
    let mut ret = vec![];
    let mut buf = 0;
    let mut tbl = vec![64; 256];
    for i in 0..64 { tbl[TBL[i] as usize] = i as u8; }
    for (i, c) in v.bytes().filter_map(|c| { let c = tbl[c as usize]; if c < 64 { Some(c) } else { None } }).enumerate() {
        match i % 4 {
            0 => buf = c << 2,
            1 => { ret.push(buf | c >> 4); buf = c << 4; }
            2 => { ret.push(buf | c >> 2); buf = c << 6; }
            3 => ret.push(buf | c),
            _ => unreachable!(),
        }
    }
    ret
}

const TBL: &'static [u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const BIN: &'static str = "<base64-encoded ELF executable removed>";