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)

#![allow(unused_imports)]

use text_io::*;
use proconio::*;
use std::collections::*;
use itertools::Itertools;
use std::process::exit;
use std::cmp::*;
use num::*;
use num::integer::Roots;
use std::str::FromStr;
use std::io::stdin;

fn main(){
    input! {
    n:usize,
    k:isize,
    c:[isize;n],
    }
    let v=vec![0;300000];
    let mut sg:SegmentTree<RMQ>=SegmentTree::<RMQ>::from_slice(&v);
    for i in 0..n {
        let max=sg.fold(max(0,c[i]-k-1)as usize,min(300000,c[i]+k)as usize);
        sg.set((c[i]-1) as usize,(max+1));
    }
    println!("{}",sg.fold(0,300000))
}

struct RMQ;
impl Monoid for RMQ{
    type Item=isize;

    fn id() -> Self::Item {
        0
    }

    fn op(a: &Self::Item, b: &Self::Item) -> Self::Item {
        max(*a,*b)
    }
}

/// セグメント木に乗せるMonoid
///
/// 以下の性質を満たさないといけない
/// - op(a, id()) == op(id(), a) == a
/// - op(op(a, b), c) == op(a, op(b,c))
pub trait Monoid {
    /// 群の型
    type Item: Sized + Clone + std::fmt::Debug;
    /// 単位元
    fn id() -> Self::Item;
    /// 二項演算
    fn op(a: &Self::Item, b: &Self::Item) -> Self::Item;
}

/// 一点更新、区間クエリにO(logN)で答える
pub enum SegmentTree<M: Monoid> {
    /// 子を持たない葉ノード
    Leaf { value: M::Item },
    /// 子を持つノード
    Node {
        value: M::Item,
        len: usize,
        left: Box<SegmentTree<M>>,
        right: Box<SegmentTree<M>>,
    },
}

impl<M: Monoid> SegmentTree<M> {
    /// そのNodeの持つ要素の長さを返す
    pub fn len(&self) -> usize {
        match self {
            SegmentTree::Leaf { .. } => 1,
            SegmentTree::Node { len, .. } => *len,
        }
    }

    /// そのNodeの値を返す
    pub fn val(&self) -> &M::Item {
        match self {
            SegmentTree::Leaf { value } => value,
            SegmentTree::Node { value, .. } => value,
        }
    }

    /// スライスからセグメント木を作る
    pub fn from_slice(slice: &[M::Item]) -> Self {
        assert!(!slice.is_empty());

        if slice.len() == 1 {
            Self::Leaf {
                value: slice[0].clone(),
            }
        } else {
            let mid = slice.len() / 2;
            let left = Self::from_slice(&slice[..mid]);
            let right = Self::from_slice(&slice[mid..]);
            Self::Node {
                value: M::op(left.val(), right.val()),
                len: slice.len(),
                left: Box::new(left),
                right: Box::new(right),
            }
        }
    }

    /// 0-indexedでその要素を得る
    pub fn get(&self, i: usize) -> &M::Item {
        assert!(i < self.len());

        match self {
            SegmentTree::Leaf { value, .. } => value,
            SegmentTree::Node { left, right, .. } => {
                let l = left.len();

                if i < l {
                    left.get(i)
                } else {
                    right.get(i - l)
                }
            }
        }
    }

    /// 0-indexedで一点更新
    pub fn set(&mut self, i: usize, item: M::Item) {
        assert!(i < self.len());

        match self {
            SegmentTree::Leaf { value } => *value = item,
            SegmentTree::Node {
                left, right, value, ..
            } => {
                let l = left.len();

                if i < l {
                    left.set(i, item);
                } else {
                    right.set(i - l, item);
                }

                *value = M::op(left.val(), right.val());
            }
        }
    }

    /// sg[start..end]の値を返す
    pub fn fold(&self, start: usize, end: usize) -> M::Item {
        assert!(start <= end);
        assert!(end <= self.len());

        let len = end - start;
        if len == 0 {
            return M::id();
        } else if len == self.len() {
            return self.val().clone();
        }

        match self {
            SegmentTree::Leaf { .. } => unreachable!(),
            SegmentTree::Node { left, right, .. } => {
                let l = left.len();

                if end <= l {
                    left.fold(start, end)
                } else if l <= start {
                    right.fold(start - l, end - l)
                } else {
                    M::op(&left.fold(start, l), &right.fold(0, end - l))
                }
            }
        }
    }
}

impl<M: Monoid> From<&[M::Item]> for SegmentTree<M> {
    fn from(slice: &[<M as Monoid>::Item]) -> Self {
        SegmentTree::from_slice(slice)
    }
}