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

#include <bits/stdc++.h>
#define ll long long int
#define mk make_pair
#define pb push_back
#define INF (ll)1e18
#define pii pair<ll, ll>
#define mod 1000000007 // 998244353
#define f(i, a, b) for (ll i = a; i < b; i++)
#define fb(i, a, b) for (ll i = a; i > b; i--)
#define ff first
#define ss second
#define srt(v)                                                                 \\
  if (!v.empty())                                                              \\
  sort(v.begin(), v.end())
#define PI 3.141592653589793238
#define pq priority_queue<ll>
#define pqr priority_queue<ll, vector<ll>, greater<ll>()>
using namespace std;
ll pow_mod(ll a, ll b) {
  ll res = 1;
  while (b != 0) {
    if (b & 1) {
      res = (res * a) % mod;
    }
    a = (a * a) % mod;
    b /= 2;
  }
  return res;
}
ll st[4 * 300000 + 2];
void solve() {

  // GENERAL PURPOSE SEGMENT TREE FOR RMQ

  ll n, k;

  cin >> n >> k;

  ll a[n];

  for (ll i = 0; i < n; i++)
    cin >> a[i];

  function<void(ll, ll, ll, ll, ll)> update = [&](ll ss, ll se, ll si, ll id,
                                                  ll val) {
    if (ss == se && ss == id) {
      st[si] = val;
      return;
    }

    if (id > se || id < ss)
      return;

    ll mid = (ss + se) / 2;

    update(ss, mid, 2 * si + 1, id, val);
    update(mid + 1, se, 2 * si + 2, id, val);

    st[si] = max(st[2 * si + 1], st[2 * si + 2]);
  };

  function<ll(ll, ll, ll, ll, ll)> get_min = [&](ll ss, ll se, ll si, ll l,
                                                 ll r) {
    if (l > r)

      return 0LL;

    if (r < ss || se < l)
      return 0LL;

    if (ss >= l && se <= r)
      return st[si];

    ll mid = (ss + se) / 2;

    return max(get_min(ss, mid, 2 * si + 1, l, r),
               get_min(mid + 1, se, 2 * si + 2, l, r));
  };

  // build_sgt(0,30000,0);

  ll dp[300000 + 1];

  for (ll i = 0; i < 300000LL + 1LL; i++) {
    update(0LL, 300000LL, 0LL, i, 0LL);

    dp[i] = 0;
  }

  ll ans = 0LL;

  for (ll i = 0; i < n; i++) {
    ll l = max(0LL, a[i] - k);
    ll r = min(300000LL, a[i] + k);

    ll res = get_min(0LL, 300000LL, 0LL, l, r);

    update(0LL, 300000LL, 0LL, a[i], 1LL);

    dp[a[i]] = max(dp[a[i]], 1 + res);

    update(0LL, 300000LL, 0LL, a[i], dp[a[i]]);
  }

  for (ll i = 0; i < n; i++)
    ans = max(ans, dp[a[i]]);

  cout << ans << endl;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  // Start from Here.

  ll t;
  t = 1;
  // cin>>t;
  while (t--)
    solve();

  // Good Bye!
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
#define mk make_pair
#define pb push_back
#define INF (ll)1e18
#define pii pair<ll, ll>
#define mod 1000000007 // 998244353
#define f(i, a, b) for (ll i = a; i < b; i++)
#define fb(i, a, b) for (ll i = a; i > b; i--)
#define ff first
#define ss second
#define srt(v)                                                                 \\
  if (!v.empty())                                                              \\
  sort(v.begin(), v.end())
#define PI 3.141592653589793238
#define pq priority_queue<ll>
#define pqr priority_queue<ll, vector<ll>, greater<ll>()>
using namespace std;
ll pow_mod(ll a, ll b) {
  ll res = 1;
  while (b != 0) {
    if (b & 1) {
      res = (res * a) % mod;
    }
    a = (a * a) % mod;
    b /= 2;
  }
  return res;
}
ll st[4 * 30000 + 2];
void solve() {

  // GENERAL PURPOSE SEGMENT TREE FOR RMQ

  ll n, k;

  cin >> n >> k;

  ll a[n];

  for (ll i = 0; i < n; i++)
    cin >> a[i];

  function<void(ll, ll, ll, ll, ll)> update = [&](ll ss, ll se, ll si, ll id,
                                                  ll val) {
    if (ss == se && ss == id) {
      st[si] = val;
      return;
    }

    if (id > se || id < ss)
      return;

    ll mid = (ss + se) / 2;

    update(ss, mid, 2 * si + 1, id, val);
    update(mid + 1, se, 2 * si + 2, id, val);

    st[si] = max(st[2 * si + 1], st[2 * si + 2]);
  };

  function<ll(ll, ll, ll, ll, ll)> get_min = [&](ll ss, ll se, ll si, ll l,
                                                 ll r) {
    if (l > r)

      return 0LL;

    if (r < ss || se < l)
      return 0LL;

    if (ss >= l && se <= r)
      return st[si];

    ll mid = (ss + se) / 2;

    return max(get_min(ss, mid, 2 * si + 1, l, r),
               get_min(mid + 1, se, 2 * si + 2, l, r));
  };

  // build_sgt(0,30000,0);

  ll dp[30000 + 1];

  for (ll i = 0; i < 30000 + 1; i++) {
    update(0LL, 30000LL, 0LL, i, 0LL);

    dp[i] = 0;
  }

  ll ans = 0LL;

  for (ll i = 0; i < n; i++) {
    ll l = max(0LL, a[i] - k);
    ll r = min(30000LL, a[i] + k);

    ll res = get_min(0LL, 30000LL, 0LL, l, r);

    update(0LL, 30000LL, 0LL, a[i], 1LL);

    dp[a[i]] = max(dp[a[i]], 1 + res);

    update(0LL, 30000LL, 0LL, a[i], dp[a[i]]);
  }

  for (ll i = 0; i < n; i++)
    ans = max(ans, dp[a[i]]);

  cout << ans << endl;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  // Start from Here.

  ll t;
  t = 1;
  // cin>>t;
  while (t--)
    solve();

  // Good Bye!
  return 0;
}