Problem:
Score : 400 points

Problem Statement

You are given a sequence A_1, A_2, ..., A_N and an integer K.

Print the maximum possible length of a sequence B that satisfies the following conditions:

B is a (not necessarily continuous) subsequence of A.

For each pair of adjacents elements of B, the absolute difference of the elements is at most K.

Constraints

1 \leq N \leq 300,000

0 \leq A_i \leq 300,000

0 \leq K \leq 300,000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
A_1
A_2
:
A_N

Output

Print the answer.

Sample Input 1

10 3
1
5
4
3
8
6
9
7
2
4

Sample Output 1

7

For example, B = (1, 4, 3, 6, 9, 7, 4) satisfies the conditions.

It is a subsequence of A = (1, 5, 4, 3, 8, 6, 9, 7, 2, 4).

All of the absolute differences between two adjacent elements (|1-4|, |4-3|, |3-6|, |6-9|, |9-7|, |7-4|) are at most K = 3.

Solution in Rust (1.42.0) (judge verdict: Accepted)

#[allow(unused_imports)]
use {
    itertools::Itertools,
    proconio::{fastout, input, marker::*},
    std::cmp::*,
    std::collections::*,
    std::io::Write,
    std::ops::*,
};

#[allow(unused_macros)]
macro_rules! debug { ($($e:expr),*) => { #[cfg(debug_assertions)] $({ let (e, mut err) = (stringify!($e), std::io::stderr()); writeln!(err, "{} = {:?}", e, $e).unwrap() })* }; }

struct SegmentTree<T, F: Fn(&T, &T) -> T> {
    width: usize,
    node: Vec<T>,
    id: T,
    op: F,
}
#[allow(dead_code)]
impl<T: Add<Output = T> + Copy + Clone, F: Fn(&T, &T) -> T> SegmentTree<T, F> {
    fn new(n: usize, id: T, op: F) -> Self {
        let width = n.next_power_of_two();
        Self {
            width,
            node: vec![id; width << 1],
            id,
            op,
        }
    }
    fn from_vec(a: &[T], id: T, op: F) -> Self {
        let width = a.len().next_power_of_two();
        let mut node = vec![id; width << 1];
        for i in 0..a.len() {
            node[i + width] = a[i];
        }
        for i in (1..width).rev() {
            node[i] = op(&node[i << 1], &node[1 + (i << 1)]);
        }
        Self {
            width,
            node,
            id,
            op,
        }
    }
    fn update(&mut self, idx: usize, v: T) {
        let mut now = idx + self.width;
        self.node[now] = v;
        now = now >> 1;
        while now > 0 {
            self.node[now] = (self.op)(&self.node[now << 1], &self.node[1 + (now << 1)]);
            now = now >> 1;
        }
    }
    fn get(&self, idx: usize) -> T {
        self.node[idx + self.width]
    }
    fn __prod(&self, idx: usize, lc: usize, rc: usize, l: usize, r: usize) -> T {
        if rc <= l || r <= lc {
            self.id
        } else if l <= lc && rc <= r {
            self.node[idx]
        } else {
            (self.op)(
                &self.__prod(idx << 1, lc, (lc + rc) / 2, l, r),
                &self.__prod(1 + (idx << 1), (lc + rc) / 2, rc, l, r),
            )
        }
    }
    fn prod<R>(&self, range: R) -> T
    where
        R: std::ops::RangeBounds<usize>,
    {
        let l = match range.start_bound() {
            std::ops::Bound::Included(&a) => a,
            _ => 0,
        };
        let r = match range.end_bound() {
            std::ops::Bound::Excluded(&a) => a,
            std::ops::Bound::Included(&a) => a + 1,
            _ => self.width,
        };
        self.__prod(1, 0, self.width, l, r)
    }
    /// return Some(r): Max f(l..r) == true && f(l..=r) == false
    fn max_right(&self, l: usize, f: fn(T) -> bool) -> Option<usize> {
        if !f(self.get(l)) {
            None
        } else {
            let mut low = l;
            let mut high = self.width;
            while high - low > 1 {
                let mid = (low + high) / 2;
                if f(self.prod(l..=mid)) {
                    low = mid;
                } else {
                    high = mid;
                }
            }
            Some(low)
        }
    }
    /// return l: Min f(self.prod(l, r)) == true
    fn min_left(&self, r: usize, f: fn(T) -> bool) -> Option<usize> {
        if !f(self.get(r)) {
            None
        } else {
            let mut low = 0;
            let mut high = r;
            while high - low > 1 {
                let mid = (low + high) / 2;
                if f(self.prod(mid..=r)) {
                    high = mid;
                } else {
                    low = mid;
                }
            }
            Some(high)
        }
    }
}

#[fastout]
fn main() {
    input! {
        n: usize,
        k: usize,
        a: [usize; n]
    }
    let mut seg = SegmentTree::new(300001, 0i64, |a, b| max(*a, *b));
    for a in a {
        let now = seg.prod(max(a, k) - k..=min(300000, a + k));
        seg.update(a, now + 1);
    }
    let ans = seg.prod(..);
    println!("{}", ans);
}