Language: C++

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int,int> pii;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;

#define pb push_back
#define eb emplace_back
#define ff first
#define ss second
#define mp make_pair
#define endl '\n'

const double PI = acos(-1);
const double eps = 1e-9;
const int inf = 2000000000;
const ll infLL = 9000000000000000000;
#define MOD 1000000007
//#define harmonic(n) 0.57721566490153286l+log(n)

#define mem(a,b) memset(a, b, sizeof(a) )
#define sqr(a) ((a) * (a))

#define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define fraction() cout.unsetf(ios::floatfield); cout.precision(10); cout.setf(ios::fixed,ios::floatfield);

//int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; ///4 Direction
//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};///8 direction
//int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};///Knight Direction
//int dx[]={2,1,-1,-2,-1,1};int dy[]={0,1,1,0,-1,-1}; ///Hexagonal Direction

inline int in() { int x; scanf("%d", &x); return x; }
int gcd(int a,int b) { return b==0 ? a:gcd(b,a%b);}
int power( int x, int n)
{
    if(n==0)return 1;
    else if(n%2==0)
    {
        int y=power(x,n/2);
        return y*y;
    }
    else
        return x*power(x,n-1);
}
//abcdefghijklmnopqrstuvwxyz//
const int sz=300010;
const int mx=sz*4;
#define left(x) x<<1
#define right(x) (x<<1)+1
int a[sz];
struct segmenttree
{
    int n;
    int st[mx];
    build()
    {
        mem(st,0);
        //build(1,0,n-1);
    }
    void update(int p,int l,int r,int i,int j,int v)
    {
        //printf("%d %d %d %d\n",l,r,i,j);
        if(l>r||l>j||r<i) return;
        if(l>=i&&r<=j)
        {
            st[p]=v;
            return;
        }
        //printf("%d %d\n",st[p],v);
        update(left(p),l,(l+r)/2,i,j,v);
        update(right(p),1+(l+r)/2,r,i,j,v);
        int p1=st[left(p)],p2=st[right(p)];
        st[p]=max(p1,p2);
    }
    void update(int i,int j,int v)
    {
        update(1,0,n-1,i,j,v);
    }

    int query(int p,int l,int r,int i,int j)
    {
        if(i>r||j<l)
            return -1;
        if(l>=i&&r<=j) return st[p];
        int p1=query(left(p),l,(l+r)/2,i,j);
        int p2=query(right(p),(l+r)/2+1,r,i,j);
        if(p1==-1) return p2;
        if(p2==-1) return p1;
        return max(p1,p2);
    }
    int query(int i,int j)
    {
        return query(1,0,n-1,i,j);
    }
};

segmenttree seg;
int dp[300010];
int main()
{
    #ifdef CP
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
    #endif // CP
    int n=in(),k=in(),i;
    seg.n=300005;
    for(i=0;i<n;i++)
        a[i]=in();
    seg.build();
    dp[0]=1;
    seg.update(a[0],a[0],1);
    int x,y,ans=1;
    for(int i=1;i<n;i++)
    {
        x=max(a[i]-k,0);
        y=min(a[i]+k,300000);
        dp[i]=seg.query(x,y)+1;
        seg.update(a[i],a[i],dp[i]);
        ans=max(ans,dp[i]);
    }
    printf("%d\n",ans);
}


Compiler output:
a.cc:63:5: error: ISO C++ forbids declaration of 'build' with no type [-fpermissive]
   63 |     build()
      |     ^~~~~
a.cc: In member function 'int segmenttree::build()':
a.cc:67:5: warning: no return statement in function returning non-void [-Wreturn-type]
   67 |     }
      |     ^