Language: C++

#include <bits/stdc++.h>
using namespace std;
#define M 1000000007
long long k,tree[2000020],b[500005],ans=0,x,n;
long long query(int node,int s,int e,int l,int r)
{
    if(r<s || e<l)
    return 0LL;
    
    if(s>=l && e<=r)
    return tree[node];
    
    int m=(s+e)/2;
    return (query(2*node,s,m,l,r)%M+query(2*node+1,m+1,e,l,r)%M)%M;
}
void update(int node,int s,int e,int idx,long long val)
{
    if(s==e)
    {
        tree[node]=val;
        return;
    }
    int m= (s+e)/2;
    if(idx<=m)
        update(2*node,s,m,idx,val);
    else
        update(2*node+1,m+1,e,idx,val);
    tree[node] = (tree[2*node]%M+tree[2*node+1]%M)%M;
}
int main()
{
    cin>>n>>k;
    vector< pair<long long,long long> > a;
    for(int i=0;i<n;i++)
    {
        cin>>x;
        a.push_back(make_pair(x,i));
        b[i]=a[i].first;
    }
    sort(a.begin(),a.end());
    for(long long int i=0;i<n;i++)
    {
        int l=lower_bound(a.begin(),a.end(),make_pair(b[i]-k,0LL) )-a.begin();
        int r= (upper_bound(a.begin(),a.end(),make_pair(b[i]+k,n+1LL) )-a.begin())-1;
        if(l<n && r<n && l<=r)
        {
            long long dd=query(1,0,n-1,l,r);
            ans=max(ans,r-l);
           
            update(1,0,n-1,lower_bound(a.begin(),a.end(),make_pair(b[i],i) )-a.begin(),(dd+1LL)%M);
        }
    }
    cout<<ans<<endl;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:48:20: error: no matching function for call to 'max(long long int&, int)'
   48 |             ans=max(ans,r-l);
      |                 ~~~^~~~~~~~~
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:48:20: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
   48 |             ans=max(ans,r-l);
      |                 ~~~^~~~~~~~~
/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:48:20: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   48 |             ans=max(ans,r-l);
      |                 ~~~^~~~~~~~~