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 C++ (GCC 9.2.1) (judge verdict: Runtime Error)

#include <bits/stdc++.h>
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp> 
// #include <ext/pb_ds/tree_policy.hpp> 
// using namespace __gnu_pbds; 
// #pragma GCC target ("avx2")
// #pragma GCC optimization ("O3")
// #pragma GCC optimization ("unroll-loops")
#define fi first
#define se second
#define endl '\n'
#define int int64_t
#define double long double
// template<int D, typename T>struct dense:public vector<dense<D-1,T>>{static_assert(D>=1,"dim err");template<typename... Args>dense(int n=0,Args... args):vector<dense<D-1,T>>(n,dense<D-1,T>(args...)){}};
// template<typename T>struct dense<1,T>:public vector<T>{dense(int n=0,const T& val=T()):vector<T>(n,val){}};
// template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// template<typename T> using reverse_queue = priority_queue<T, vector<T>, greater<T>>;
// mt19937_64 rnd(time(0));
// const int FIXED_RANDOM = rnd();
// template<typename T> struct custom_hash{T operator()(T x)const{return (T) x^FIXED_RANDOM;}}; // check hash in whether int64 or int32
// template<typename T1, typename T2> using hash_map = gp_hash_table<T1, T2, custom_hash<T1>>;
const int MAXN = (int) 3e5 + 2;
const int INF = numeric_limits<int>::max() >> 1;
const int MOD = (int) 1000000007;
// Tao luyện kỹ thuật trong 8 năm mày luyện kỹ thuật trong buồng tắm nam
int it[MAXN];
void update(int id, int l, int r, int p, int val){
  if(l > p || r < p || l > r) return;
  if(l == p && r == p){
    if(it[id] < val) it[id] = val;
    return;
  }
  int mid = (l + r)/2;
  update(id << 1, l, mid, p, val);
  update(id << 1|1, mid + 1, r, p, val);
  it[id] = max(it[id << 1], it[id << 1 | 1]);
}
int get(int id, int l, int r, int L, int R){
  if(l > r || l > R || r < L) return -INF;
  if(L <= l && r <= R) return it[id];
  int mid = (l + r)/2;
  return max(get(id << 1, l, mid, L, R), get(id << 1|1, mid + 1, r, L, R));
}
signed main(){
  ios_base::sync_with_stdio(0); 
  cin.tie(0);
  fill(it, it + MAXN, -INF);
  int n, k;
  cin >> n >> k;
  vector<int> a(n + 1);
  for(int i = 1; i <= n; i ++) cin >> a[i];
  for(int i = n; i >= 1; i --){
    int lf = a[i] - k;
    if(lf < 1) lf = 1;
    int rt = a[i] + k;
    if(rt > n) rt = n;
    int d = get(1, 1, n, lf, rt);
    if(d < 0) d = 0;
    d ++;
    update(1, 1, n, a[i], d);
  }
  cout << get(1, 1, n, 1, 300000);
}