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 OCaml (4.10.0) (judge verdict: Accepted)

module type Segtree = sig
    type s
    type t

    val create   : int -> t
    val of_array : s array -> t
    val set      : t -> int -> s -> unit
    val get      : t -> int -> s
    val prod     : t -> int -> int -> s
    val all_prod : t -> s
end

module type SegtreeParam = sig
    type t

    val op : t -> t -> t
    val e  : t
end 

module Segtree = struct
    module Make(STP: SegtreeParam) : Segtree with type s = STP.t = struct
        type t = int * int * int * STP.t array
        type s = STP.t

        let ceil_pow2 n =
            let rec loop x =
                if 1 lsl x < n then loop (x + 1) else x
            in
            loop 0

        let update d k =
            d.(k) <- STP.op d.(2 * k) d.(2 * k + 1)

        let of_array v =
            let n = Array.length v in
            let log = ceil_pow2 n in
            let size = 1 lsl log in
            let d = Array.make (2 * size) STP.e in
            for i = 0 to n - 1 do d.(size + i) <- v.(i) done;
            for i = size - 1 downto 1 do update d i done;
            n, size, log, d

        let create n =
            of_array (Array.make n STP.e)

        let set (n, size, log, d) p x =
            let p = p + size in
            d.(p) <- x;
            for i = 1 to log do update d (p lsr i) done

        let get (_, size, _, d) p = d.(p + size)

        let prod (n, size, log, d) l r =
            let rec loop l r sml smr =
                if l >= r then STP.op sml smr else
                    let sml, l = if l land 1 = 0 then sml, l else STP.op sml d.(l),     l + 1 in
                    let smr, r = if r land 1 = 0 then smr, r else STP.op d.(r - 1) smr, r - 1 in
                    loop (l lsr 1) (r lsr 1) sml smr
            in
            loop (l + size) (r + size) STP.e STP.e

        let all_prod (_, _, _, d) = d.(1)
    end
end;;
Scanf.scanf "%d %d" (fun n k ->
    let module ST = Segtree.Make (struct type t = int let op = max let e = 0 end) in

    let seg = ST.create 600001 in
    for i = 0 to n - 1 do
        Scanf.scanf " %d" (fun a ->
            let l = max 0 (a - k) in
            let r = min 600000 (a + k + 1) in
            let v = ST.prod seg l r in
            ST.set seg a (v + 1)
        )
    done;
    Printf.printf "%d\n" @@ ST.all_prod seg
)