Language: C++

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define pp pair<pair<ll, ll>,pair<ll, ll>>
#define pll pair<ll,ll>
#define ppll pair<ll,pll>
#define pdd pair<double,double>
#define vii vector<int>
#define vll vector<ll>
#define mat vector<vector<ll>>
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define eb emplace_back
#define fi first
#define sc second
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep2(i,a,b) for(ll i=a;i<b;i++)
#define repr(i,n) for(ll i=n-1;i>=0;i--)
#define all(x) x.begin(),x.end()
#define sz(x) (ll) (x).size()
#define pq priority_queue<ll>
#define pqg priority_queue<ll,vector<ll>,greater<ll>>
#define LB(v,x) (lower_bound(v.begin(),v.end(),x)-v.begin())
#define UB(v,x) (upper_bound(v.begin(),v.end(),x)-v.begin())
#define ERASE(v) sort(v.begin(),v.end());v.erase(unique(v.begin(),v.end()),v.end())
#define int ll
// #define ll int
using namespace std;
const ll INF = 1 << 29;
const ll LLINF = (1LL << 60LL);
const ll MOD = 1000000007;
const int MAX_V = 500;
const ll mod = 998244353;
const ll MAX = 2100000;
const double pi = acos(-1);
const double eps = 1e-10;
ll dx[8] ={1,0,-1,0,1,-1,1,-1};
ll dy[8] ={0,1,0,-1,1,-1,-1,1};
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }



template <class T>
struct SegmentTree {
    ll n;
    vector<T> data;
    T def;
    function<T(T, T)> operation;
    function<T(T, T)> update;

    SegmentTree(ll _n, T _def, function<T(T, T)> _operation, function<T(T, T)> _update)
        : def(_def), operation(_operation), update(_update) {
        n = 1;
        while (n < _n) {
            n *= 2;
        }
        data = vector<T>(2 * n - 1, def);
    }

    void change(ll i, T x) {
        i += n - 1;
        data[i] = update(data[i], x);
        while (i > 0) {
            i = (i - 1) / 2;
            data[i] = operation(data[i * 2 + 1], data[i * 2 + 2]);
        }
    }

    T _query(ll a, ll b, ll k, ll l, ll r) {
        if (r <= a || b <= l)
            return def;
        if (a <= l && r <= b) {
            return data[k];
        } else {
            T vl = _query(a, b, 2 * k + 1, l, (l + r) / 2);
            T vr = _query(a, b, 2 * k + 2, (l + r) / 2, r);
            return operation(vr, vl);
        }
    }

    T query(ll a, ll b) {
        return _query(a, b, 0, 0, n);
    }

    T operator[](ll i) {
        return data[i + n - 1];
    }

    void print() {
        rep(i,10){
            cout<<data[i+n-1]<<" ";
        }
        cout<<endl;
    }
};





void solve() {

    ll a[300010];
    ll n;
    cin>>n;
    ll k;cin>>k;

    rep(i,n) cin>>a[i];

    ll ex = 0;
    auto fx = [](ll a, ll b){return max(a,b);};
    auto update = [](ll a, ll b){return b;};
    SegmentTree<ll> sg(300010, ex, fx, update);


    rep(i,n){
        ll l = max(0ll, a[i]-k);
        ll r = min(300000ll, a[i]+k);
        ll sum = sg.query(l,r+1);
        sg.change(a[i], sum+1);
        // cout<<a[i]<<",,//  ";
        // sg.print();
    }

    rep(i,300001){
        chmax(ans, sg[i]);
    }

    cout<<ans<<endl;

}



signed main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);


    ll t=1;
    
    // cin>>t;

    rep(i,t){
        solve(); 
    }

    return 0;
}


Compiler output:
a.cc: In function 'void solve()':
a.cc:129:15: error: 'ans' was not declared in this scope; did you mean 'abs'?
  129 |         chmax(ans, sg[i]);
      |               ^~~
      |               abs
a.cc:132:11: error: 'ans' was not declared in this scope; did you mean 'abs'?
  132 |     cout<<ans<<endl;
      |           ^~~
      |           abs