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, non_snake_case, dead_code, non_upper_case_globals)]
use {
    proconio::{marker::*, *},
    std::{cmp::*, collections::*, mem::*},
};

#[fastout]
fn main() {
    input! {//
        h:usize,w:usize,
        ab:[(Usize1,Usize1);h],
    }
    let mut dp = vec![0; w];
    let mut seg =
        RangeAddSegmentTree::new(w + 5, INF as usize, |a, b| if a > b { b } else { a }, 0);
    let mut ans = vec![0; h];
    for i in 0..w {
        seg.update(i, 0);
    }
    let mut left = 0;

    for k in 0..h {
        let mut l = ab[k].0;
        let mut r = ab[k].1;
        if (l..=r).contains(&left) {
            ans[k] += r - left;
            left = r + 1;
        }
        seg.add(l..r + 1, 1);
        ans[k] += seg.get(left..w) + 1;
        ans[k] += if k == 0 { 0 } else { ans[k - 1] };
    }
    dbg!(&ans);
    for i in 0..h {
        if ans[i] >= INF as usize {
            println!("{}", -1);
        } else {
            println!("{}", ans[i]);
        }
        // dbg!(seg.get(i..i + 1));
    }
}
const INF: i64 = 1 << 60;
type Range = std::ops::Range<usize>;
pub struct RangeAddSegmentTree<T, F> {
    data: Vec<T>,
    lazy: Vec<T>,
    size: usize,
    f: F,
    init: T,
}
impl<T, F> RangeAddSegmentTree<T, F>
where
    T: PartialOrd + ::std::ops::Add<Output = T> + ::std::ops::Sub<Output = T> + Copy,
    F: Fn(T, T) -> T + Copy,
{
    pub fn new(n: usize, init: T, f: F, zero: T) -> Self {
        let size = n.next_power_of_two();
        RangeAddSegmentTree {
            data: vec![init; size * 2],
            lazy: vec![zero; size * 2],
            size,
            init,
            f,
        }
    }
    pub fn add(&mut self, range: Range, value: T) {
        self.add_to_range(range, value, 0, 0..self.size);
    }
    fn add_to_range(&mut self, add_range: Range, value: T, mut k: usize, seg_range: Range) {
        if add_range.end <= seg_range.start || seg_range.end <= add_range.start {
            return;
        }
        if add_range.start <= seg_range.start && seg_range.end <= add_range.end {
            self.lazy[k] = self.lazy[k] + value;
            while k > 0 {
                k = (k - 1) / 2;
                self.data[k] = (self.f)(
                    self.data[k * 2 + 1] + self.lazy[k * 2 + 1],
                    self.data[k * 2 + 2] + self.lazy[k * 2 + 2],
                );
            }
        } else {
            let mid = (seg_range.start + seg_range.end) / 2;
            self.add_to_range(add_range.clone(), value, k * 2 + 1, seg_range.start..mid);
            self.add_to_range(add_range, value, k * 2 + 2, mid..seg_range.end);
        }
    }
    pub fn update(&mut self, pos: usize, value: T) {
        let cur = self.get(pos..(pos + 1));
        let mut k = pos + self.size - 1;
        let raw = self.data[k];
        self.data[k] = raw + value - cur;
        while k > 0 {
            k = (k - 1) / 2;
            self.data[k] = (self.f)(self.data[k * 2 + 1], self.data[k * 2 + 2]);
        }
    }
    pub fn get(&self, range: Range) -> T {
        self.get_from_range(range, 0, 0..self.size)
    }
    fn get_from_range(&self, get_range: Range, k: usize, seg_range: Range) -> T {
        if get_range.end <= seg_range.start || seg_range.end <= get_range.start {
            self.init
        } else if get_range.start <= seg_range.start && seg_range.end <= get_range.end {
            self.data[k] + self.lazy[k]
        } else {
            let mid = (seg_range.start + seg_range.end) / 2;
            let x = self.get_from_range(get_range.clone(), k * 2 + 1, seg_range.start..mid);
            let y = self.get_from_range(get_range, k * 2 + 2, mid..seg_range.end);
            (self.f)(x, y) + self.lazy[k]
        }
    }
}