Language: C++

#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
struct lazy_segment_tree{
  using it_type = long long;
  int n;
  vector<it_type> it;
  vector<long long> lz;
  const it_type FAILED_STAGE = -INF;
  void init(int _){
    n = _;
    it.resize((n << 2)|1, -INF);
    lz.resize((n << 2)|1, -INF); // check init lazy
  }
  it_type merge(it_type a, it_type b){
    return max(a, b);
  }
  it_type build(int u, int l, int r, auto &a){ // 1 indexed
    if(l == r) return it[u] = a[l];
    return it[u] = merge(build(u << 1, l, ((l + r)>>1), a), build((u << 1)|1, ((l + r)>>1) + 1, r, a)); // merge stage
  }
  void lazy(int u, int l, int r){ // lazy 
    if(lz[u] == -INF) return;
    it[u] = max(it[u], lz[u]);
    if(l != r){
      lz[u << 1] = max(lz[u << 1], lz[u]);
      lz[(u << 1)|1] = max(lz[(u << 1)|1], lz[u]);
    }
    lz[u] = -INF;
  }
  void update(int u, int l, int r, int L, int R, int val){
    if(R < L) return;
    lazy(u, l, r);
    if(l > R || r < L) return;
    if(L <= l && r <= R){
      lz[u] = val; // lazy update 
      lazy(u, l, r);
      return;
    }
    update(u << 1, l, ((l + r)>>1), L, R, val);
    update((u << 1)|1, ((l + r)>>1) + 1, r, L, R, val);
    it[u] = merge(it[u << 1], it[(u << 1)|1]); // merge stage
  }
  it_type get(int u, int l, int r, int L, int R){
    if(l > R || r < L) return FAILED_STAGE; // check for failed stage
    lazy(u, l, r);
    if(L <= l && r <= R) return it[u];
    return merge(get(u << 1, l, ((l + r)>>1), L, R), get((u << 1)|1, ((l + r)>>1) + 1, r, L, R)); // merge stage
  }
};
signed main(){
  ios_base::sync_with_stdio(0); 
  cin.tie(0);
  // freopen("FDT.txt", "r", stdin);
  lazy_segment_tree tr;
  tr.init(300001);
  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 left = max(1LL, a[i] - k), right = min(n, a[i] + k);
    int d = tr.get(1, 1, n, left, right);
    d = max(d, 0LL);
    d ++;
    tr.update(1, 1, n, a[i], a[i], d);
  }
  cout << tr.get(1, 1, n, 1, 300000);
}

Compiler output:
a.cc:40:38: warning: use of 'auto' in parameter declaration only available with '-std=c++20' or '-fconcepts'
   40 |   it_type build(int u, int l, int r, auto &a){ // 1 indexed
      |                                      ^~~~
a.cc: In function 'int main()':
a.cc:84:19: error: no matching function for call to 'max(long long int, __gnu_cxx::__alloc_traits<std::allocator<long int>, long int>::value_type)'
   84 |     int left = max(1LL, a[i] - k), right = min(n, a[i] + k);
      |                ~~~^~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:84:19: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and '__gnu_cxx::__alloc_traits<std::allocator<long int>, long int>::value_type' {aka 'long int'})
   84 |     int left = max(1LL, a[i] - k), right = min(n, a[i] + k);
      |                ~~~^~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61:
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:84:19: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   84 |     int left = max(1LL, a[i] - k), right = min(n, a[i] + k);
      |                ~~~^~~~~~~~~~~~~~~
a.cc:85:35: error: invalid conversion from 'std::ios_base& (*)(std::ios_base&)' to 'int64_t' {aka 'long int'} [-fpermissive]
   85 |     int d = tr.get(1, 1, n, left, right);
      |                                   ^~~~~
      |                                   |
      |                                   std::ios_base& (*)(std::ios_base&)
a.cc:66:47: note:   initializing argument 5 of 'lazy_segment_tree::it_type lazy_segment_tree::get(int64_t, int64_t, int64_t, int64_t, int64_t)'
   66 |   it_type get(int u, int l, int r, int L, int R){
      |                                               ^
a.cc:86:12: error: no matching function for call to 'max(int64_t&, long long int)'
   86 |     d = max(d, 0LL);
      |         ~~~^~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:86:12: note:   deduced conflicting types for parameter 'const _Tp' ('long int' and 'long long int')
   86 |     d = max(d, 0LL);
      |         ~~~^~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:86:12: note:   mismatched types 'std::initializer_list<_Tp>' and 'long int'
   86 |     d = max(d, 0LL);
      |         ~~~^~~~~~~~