Problem p02537 (C++) — buggy submission, verdict: Runtime Error

//  RAKSHIT KADAM
// rax_coder
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp> 3
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #define ordered_set tree< int ,  null_type ,  less<int> ,  rb_tree_tag ,
// tree_order_statistics_node_update>
#warning check the size of arrays!!!
// NUMERIC DEFINITIONS :
#define INF 1000000000000000005
#define inf 1000000004
#define MOD 1000000007
#define mod 998244353
#define MAX 300006
#define pi 3.1415926535897932384626433832795
#define ld long double
#define ll long long
#define int long long
#define P(gg)                                                                  \\
  for (auto rax : gg) {                                                        \\
    cout << rax << " ";                                                        \\
  }                                                                            \\
  cout << endl;
#define Need_for_speed(activated)                                              \\
  ios_base ::sync_with_stdio(0);                                               \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define satisfy                                                                \\
  ll ter;                                                                      \\
  cin >> ter;                                                                  \\
  while (ter--)
#define inp(n)                                                                 \\
  int n;                                                                       \\
  cin >> n
#define mp make_pair
#define pb push_back
#define endl "\\n"
#define x first
#define y second
#define fore(i, a, b) for (ll i = a; i < b; i++)
#define forci(i, n) for (ll i = 0; i < n; i++)
#define vi vector<ll>
#define Endl endl
#define lb lower_bound
#define ub upper_bound
#define pii pair<ll, ll>

#define input                                                                  \\
  freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#define time                                                                   \\
  cerr << "\\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\\n";
using namespace std;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

int power(int x, unsigned int y, unsigned int m) {
  if (y == 0)
    return 1;
  int p = power(x, y / 2, m) % m;
  p = (p * p) % m;
  return (y % 2 == 0) ? p : (x * p) % m;
}
ll modInverse(int a, int m) {
  { return power(a, m - 2, m); }
}
const int maxn = 500003;
// ll fact[300009];
// ll modIN[100009];
// void facto()
//{
//     ll lim =300005;fact[0]=1;
//     for(ll i=1;i<lim;i++)
//     {
//         fact[i]=(fact[i-1]*i)%mod;
//     }
//
// }
// ll ncr(ll a,ll b)
//{
//     ll ret = 0;
//     ret = fact[a]*(modInverse(fact[b],mod));
//
//     ret%=mod;
//     ret*=modInverse(fact[a-b],mod);
//     ret%=mod;
//
//     return ret;
// }
//   """""   1LL << x    """""

ll seg[maxn * 4];
ll arr[maxn];

void update(ll ind, ll l, ll r, ll pos, ll val) {
  if (l == r) {
    seg[ind] = val;
    //        cout<<ind<<" -- "<<val<<endl;
    return;
  }
  ll mid = l + r >> 1;
  if (mid >= pos) {
    update(ind * 2, l, mid, pos, val);
  } else
    update(ind * 2 + 1, mid + 1, r, pos, val);
  seg[ind] = max(seg[ind * 2], seg[ind * 2 + 1]);
}
ll cnt = 0;
ll query(ll l, ll r, ll ind, ll L, ll R) {
  if (L > R)
    return 0;
  if (l >= L && r <= R) {
    return seg[ind];
  }
  ll mid = l + r >> 1;
  ll ret = 0;
  ret = max(query(l, mid, ind * 2, L, min(R, mid)),
            query(mid + 1, r, ind * 2 + 1, max(L, mid + 1), R));
  return ret;
}
void solve() {
  ll n;
  cin >> n;
  ll x;
  ll k;
  cin >> k;
  ll ans = 0;
  for (ll i = 1; i <= n; i++) {
    cin >> x;
    ll p = query(1, 300000, 1, max(0LL, x - k), min(x + k, 600000LL));
    update(1, 1, 300000, x, p + 1);
  }
  cout << seg[1] << endl;
}

signed main() {
  Need_for_speed(activated);

  //    satisfy
  //    {
  // solve();
  //    }

  solve();
  return 0;
}


Error: 0

Fixed submission:

//  RAKSHIT KADAM
// rax_coder
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp> 3
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #define ordered_set tree< int ,  null_type ,  less<int> ,  rb_tree_tag ,
// tree_order_statistics_node_update>
#warning check the size of arrays!!!
// NUMERIC DEFINITIONS :
#define INF 1000000000000000005
#define inf 1000000004
#define MOD 1000000007
#define mod 998244353
#define MAX 300006
#define pi 3.1415926535897932384626433832795
#define ld long double
#define ll long long
#define int long long
#define P(gg)                                                                  \\
  for (auto rax : gg) {                                                        \\
    cout << rax << " ";                                                        \\
  }                                                                            \\
  cout << endl;
#define Need_for_speed(activated)                                              \\
  ios_base ::sync_with_stdio(0);                                               \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define satisfy                                                                \\
  ll ter;                                                                      \\
  cin >> ter;                                                                  \\
  while (ter--)
#define inp(n)                                                                 \\
  int n;                                                                       \\
  cin >> n
#define mp make_pair
#define pb push_back
#define endl "\\n"
#define x first
#define y second
#define fore(i, a, b) for (ll i = a; i < b; i++)
#define forci(i, n) for (ll i = 0; i < n; i++)
#define vi vector<ll>
#define Endl endl
#define lb lower_bound
#define ub upper_bound
#define pii pair<ll, ll>

#define input                                                                  \\
  freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#define time                                                                   \\
  cerr << "\\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\\n";
using namespace std;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

int power(int x, unsigned int y, unsigned int m) {
  if (y == 0)
    return 1;
  int p = power(x, y / 2, m) % m;
  p = (p * p) % m;
  return (y % 2 == 0) ? p : (x * p) % m;
}
ll modInverse(int a, int m) {
  { return power(a, m - 2, m); }
}
const int maxn = 500003;
// ll fact[300009];
// ll modIN[100009];
// void facto()
//{
//     ll lim =300005;fact[0]=1;
//     for(ll i=1;i<lim;i++)
//     {
//         fact[i]=(fact[i-1]*i)%mod;
//     }
//
// }
// ll ncr(ll a,ll b)
//{
//     ll ret = 0;
//     ret = fact[a]*(modInverse(fact[b],mod));
//
//     ret%=mod;
//     ret*=modInverse(fact[a-b],mod);
//     ret%=mod;
//
//     return ret;
// }
//   """""   1LL << x    """""

ll seg[maxn << 1];
ll arr[maxn];

void update(ll ind, ll l, ll r, ll pos, ll val) {
  if (l == r) {
    seg[ind] = val;
    //        cout<<ind<<" -- "<<val<<endl;
    return;
  }
  ll mid = l + r >> 1;
  if (mid >= pos) {
    update(ind * 2, l, mid, pos, val);
  } else
    update(ind * 2 + 1, mid + 1, r, pos, val);
  seg[ind] = max(seg[ind * 2], seg[ind * 2 + 1]);
}
ll cnt = 0;
ll query(ll l, ll r, ll ind, ll L, ll R) {
  if (L > R)
    return 0;
  if (l >= L && r <= R) {
    return seg[ind];
  }
  ll mid = l + r >> 1;
  ll ret = 0;
  ret = max(query(l, mid, ind * 2, L, min(R, mid)),
            query(mid + 1, r, ind * 2 + 1, max(L, mid + 1), R));
  return ret;
}
void solve() {
  ll n;
  cin >> n;
  ll x;
  ll k;
  cin >> k;
  ll ans = 0;
  for (ll i = 1; i <= n; i++) {
    cin >> x;
    ll p = query(1, 300000, 1, max(0LL, x - k), min(x + k, 600000LL));
    //        cout<<p<<endl;
    update(1, 1, 300000, x, p + 1);
  }
  cout << seg[1] << endl;
}

signed main() {
  Need_for_speed(activated);

  //    satisfy
  //    {
  // solve();
  //    }

  solve();
  return 0;
}