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 Haskell (GHC 8.8.3) (judge verdict: Runtime Error)

import qualified Data.Text.Lazy as T
import qualified Data.Text.Lazy.IO as T
import qualified Data.Text.Lazy.Read as T
import Data.Bits

main :: IO ()
main = do
  let
    m = 300000 :: Int
  [n, k] <- map unsafeTextToInt . T.words <$> T.getLine :: IO [Int]
  let
    f :: [Int] -> Tree
    f = foldl step tree0
      where
        step :: Tree -> Int -> Tree
        step t x = modify (m + 1, t) x (y + 1)
          where
            y = query (m + 1, t) (max 0 (x - k), min m (x + k) + 1) :: Int
        tree0 = fromList . replicate (m + 1) $ 0 :: Tree
  as <- map unsafeTextToInt . T.lines <$> T.getContents :: IO [Int]
  print . extract . f $ as

data Tree = Node Int (Maybe Tree, Maybe Tree)

extract :: Tree -> Int
extract (Node x _) = x

unsafeLeft :: Tree -> Tree
unsafeLeft (Node _ (Just t, _)) = t

unsafeRight :: Tree -> Tree
unsafeRight (Node _ (_, Just t)) = t

singleton :: Int -> Tree
singleton = flip Node (Nothing, Nothing)

fromList :: [Int] -> Tree
fromList = unsafeLeft . head . rec . map singleton
  where
    rec :: [Tree] -> [Tree]
    rec (t : t' : ts) = rec (Node (extract t `max` extract t') (Just t, Just t') : rec ts)
    rec (t : _) = [Node (extract t) (Just t, Nothing)]
    rec _ = []

modify :: (Int, Tree) -> Int -> Int -> Tree
modify (1, Node x lr) _ d = Node (x `max` d) lr
modify (n, Node x (Just l, r)) i d | i < nl    = Node (x `max` d) (Just (modify (nl, l) i d), r)
  where
    nl = flip shiftR 1 . bit $ finiteBitSize n - countLeadingZeros n :: Int
modify (n, Node x (l, Just r)) i d = Node (x `max` d) (l, Just (modify (n - nl, r) (i - nl) d))
  where
    nl = flip shiftR 1 . bit $ finiteBitSize n - countLeadingZeros n :: Int

query :: (Int, Tree) -> (Int, Int) -> Int
query (n, t) (i, j) | i == 0 && n <= j = extract t
                    | j <= nl          = query (nl, unsafeLeft t) (i, j)
                    | nl <= i          = query (n - nl, unsafeRight t) (i - nl, j - nl)
                    | otherwise        = query (nl, unsafeLeft t) (i, nl) `max` query (n - nl, unsafeRight t) (nl, j - nl)
  where
    nl = flip shiftR 1 . bit $ finiteBitSize n - countLeadingZeros n :: Int

unsafeTextToInt :: T.Text -> Int
unsafeTextToInt s = case T.signed T.decimal s of
  Right (n, _) -> n
{-# INLINE unsafeTextToInt #-}