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

use std::cmp;
use std::fmt::Debug;
use std::str::FromStr;

pub struct TokenReader {
    reader: std::io::Stdin,
    tokens: Vec<String>,
    index: usize,
}

impl TokenReader {
    pub fn new() -> Self {
        Self {
            reader: std::io::stdin(),
            tokens: Vec::new(),
            index: 0,
        }
    }

    pub fn next<T>(&mut self) -> T
    where
        T: FromStr,
        T::Err: Debug,
    {
        if self.index >= self.tokens.len() {
            self.load_next_line();
        }
        self.index += 1;
        self.tokens[self.index - 1].parse().unwrap()
    }

    pub fn vector<T>(&mut self) -> Vec<T>
    where
        T: FromStr,
        T::Err: Debug,
    {
        if self.index >= self.tokens.len() {
            self.load_next_line();
        }
        self.index = self.tokens.len();
        self.tokens.iter().map(|tok| tok.parse().unwrap()).collect()
    }

    pub fn load_next_line(&mut self) {
        let mut line = String::new();
        self.reader.read_line(&mut line).unwrap();

        self.tokens = line
            .split_whitespace()
            .map(String::from)
            .collect();
        self.index = 0;
    }
}

struct SegTree {
    nodes: Vec<usize>,
    size: usize,
}

impl SegTree {
    fn new(size: usize) -> Self {
        Self { nodes: vec![0; 4 * size + 10], size }
    }

    fn max(&self, l: usize, r: usize) -> usize {
        self.get_max(0, 0, self.size, l, r)
    }

    fn get_max(&self, node: usize, le: usize, ri: usize, l: usize, r: usize) -> usize {
        if l <= le && ri <= r {
            return self.nodes[node];
        }

        let mi = le + (ri - le) / 2;
        let mut res = 0;
        if l <= mi {
            res = cmp::max(res, self.get_max(self.left(node), le, mi, l, r));
        }
        if mi < r {
            res = cmp::max(res, self.get_max(self.right(node), mi + 1, ri, l, r));
        }
        res
    }

    fn update(&mut self, pos: usize, val: usize) {
        self.do_update(0, 0, self.size, pos, val)
    }

    fn do_update(&mut self, node: usize, le: usize, ri: usize, pos: usize, val: usize) {
        if le == ri {
            self.nodes[node] = cmp::max(self.nodes[node], val);
            return;
        }

        let mi = le + (ri - le) / 2;
        if pos <= mi {
            self.do_update(self.left(node), le, mi, pos, val);
        } else {
            self.do_update(self.right(node), mi + 1, ri, pos, val);
        }
        self.nodes[node] = cmp::max(
            self.nodes[self.left(node)],
            self.nodes[self.right(node)]
        );
    }

    fn left(&self, node: usize) -> usize {
        node * 2 + 1
    }

    fn right(&self, node: usize) -> usize {
        node * 2 + 2
    }
}

fn main() {
    let mut reader = TokenReader::new();
    let n = reader.next();
    let k: i32 = reader.next();

    let mut best = SegTree::new(1e5 as usize + 10);
    let mut res = 0;

    for _ in 0..n {
        let num: usize = reader.next();
        let l = cmp::max(0, num as i32 - k) as usize;
        let r = num + k as usize;

        let curr = best.max(l, r) + 1;
        best.update(num, curr);
        res = cmp::max(res, curr);
    }
    println!("{}", res);
}