Language: C++

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int,int>;

template<class Monoid> struct SegTree{
    using Func = function<Monoid(Monoid, Monoid)>;
    const Func F;
    cosnt Monoid UNITY;
    int SIZE_R;
    vector<Monoid> dat;

    SegTree(int n, const Func f, const Monoid &unity): F(f), UNITY(unity) {init(n);}
    void init(int n){
        SIZE_R = 1;
        while(SIZE_R < n) SIZE_R *= 2;
        dat.assign(SIZE_R*2, UNITY);
    }

    /* set, a is 0-indexed */
    void set(int a, const Monoid &v){ dat[a + SIZE_R] = v; }
    void build(){
        for(int k = SIZE_R - 1; k > 0; k--){
            dat[k] = F(dat[k*2], dat[k*2+1]);
        }
    }

    /*update a, a is 0-indexed */
    void update(int a, const Monoid &v){
        int k = a + SIZE_R;
        dat[k] = v;
        while(k >>= 1) dat[k] = F(dat[k*2], dat[k*2+1]);
    }

    /* get [a, b), a and b are 0-indexed */
    Monoid get(int a, int b){
        Monoid vleft = UNITY, vright = UNITY;
        for(int left = a + SIZE_R, right = b + SIZE_R; left < right; left >>= 1, right >= 1){
            if(left & 1) vleft = F(vleft, dat[left++]);
            if(right & 1) vright = F(dat[--right], vright);
        }
        return F(vleft, vright);
    }
    inline Monoid operator [] (int a){ return dat[a + SIZE_R]; }

    /* debug */
    void print(){
        for(int i = 0; i < SIZE_R; i++){
            cout << (*this)[i];
            if(i != SIZE_R-1) cout << ",";
        }
        cout << endl;
    }
};

int solve(int N, int K){
    const int MAX = 510000;
    auto func = [&](int a, int b) {return max(a, b); };
    SegTree<int> seg(MAX, func, 0);
    for(int i = 0; i < N; i++){
        int a;
        cin >> a;
        int left = max(0, a - K);
        int right = min(MAX, a + K + 1);
        int val = seg.get(left, right);
        seg.update(a, val + 1);
    }
    return seg.get(0, MAX);
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, k;
    cin >> n >> k;
    cout << solve(n, k) << endl;
}

Compiler output:
a.cc:10:5: error: 'cosnt' does not name a type; did you mean 'const'?
   10 |     cosnt Monoid UNITY;
      |     ^~~~~
      |     const
a.cc: In constructor 'SegTree<Monoid>::SegTree(int, Func, const Monoid&)':
a.cc:14:62: error: class 'SegTree<Monoid>' does not have any field named 'UNITY'
   14 |     SegTree(int n, const Func f, const Monoid &unity): F(f), UNITY(unity) {init(n);}
      |                                                              ^~~~~
a.cc: In member function 'void SegTree<Monoid>::init(int)':
a.cc:18:30: error: 'UNITY' was not declared in this scope
   18 |         dat.assign(SIZE_R*2, UNITY);
      |                              ^~~~~
a.cc: In member function 'Monoid SegTree<Monoid>::get(int, int)':
a.cc:38:24: error: 'UNITY' was not declared in this scope
   38 |         Monoid vleft = UNITY, vright = UNITY;
      |                        ^~~~~
a.cc:41:27: error: 'vright' was not declared in this scope; did you mean 'right'?
   41 |             if(right & 1) vright = F(dat[--right], vright);
      |                           ^~~~~~
      |                           right
a.cc:43:25: error: 'vright' was not declared in this scope
   43 |         return F(vleft, vright);
      |                         ^~~~~~