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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using pl = pair<ll, ll>;
using pi = pair<int, int>;
#define all(x) x.begin(), x.end()
#define rep(i, j, n) for (long long i = j; i < (long long)(n); i++)
#define _GLIBCXX_DEBUG
const ll MOD = 1000000007;
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;
}
//(a+b-1)/b
// priority_queue<ll, vector<ll>, greater<ll>> q;

class SegmentTree { // Range Maximum Queery
private:
  ll len;
  vl seg;

public:
  SegmentTree(ll n) {
    len = n;
    seg.resize(2 * n, 0);
  }

  void add(ll ind, ll x) {
    ind += len;
    seg[ind] += x;
    while (true) {
      ind /= 2;
      if (ind == 0)
        break;
      seg[ind] = max(seg[2 * ind], seg[2 * ind + 1]);
    }
  }

  void update(ll ind, ll x) {
    ind += len;
    seg[ind] = x;
    while (true) {
      ind /= 2;
      if (ind == 0)
        break;
      seg[ind] = max(seg[2 * ind], seg[2 * ind + 1]);
    }
  }

  ll get_max(ll l, ll r) {
    ll ans = 0;
    l += len;
    r += len;
    while (l < r) {
      if (l % 2 == 1) {
        chmax(ans, seg[l]);
        l++;
      }
      l /= 2;
      if (r % 2 == 1) {
        chmax(ans, seg[r - 1]);
        --r;
      }
      r /= 2;
    }
    return ans;
  }
};

signed main() {
  // cout << fixed << setprecision(10);
  ll n, k;
  cin >> n >> k;
  vl a(n);
  SegmentTree seg(300001);
  rep(i, 0, n) {
    ll x;
    cin >> x;
    a[i] = x;
  }
  rep(i, 0, n) {
    ll l = max((ll)0, a[i] - k);
    ll r = min((ll)300001, a[i] + k);
    seg.update(a[i], seg.get_max(l, r + 1) + 1);
  }
  cout << seg.get_max(0, 300001) << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using pl = pair<ll, ll>;
using pi = pair<int, int>;
#define all(x) x.begin(), x.end()
#define rep(i, j, n) for (long long i = j; i < (long long)(n); i++)
#define _GLIBCXX_DEBUG
const ll MOD = 1000000007;
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;
}
//(a+b-1)/b
// priority_queue<ll, vector<ll>, greater<ll>> q;

class SegmentTree { // Range Maximum Queery
private:
  ll len;
  vl seg;

public:
  SegmentTree(ll n) {
    len = n;
    seg.resize(2 * n, 0);
  }

  void add(ll ind, ll x) {
    ind += len;
    seg[ind] += x;
    while (true) {
      ind /= 2;
      if (ind == 0)
        break;
      seg[ind] = max(seg[2 * ind], seg[2 * ind + 1]);
    }
  }

  void update(ll ind, ll x) {
    ind += len;
    seg[ind] = x;
    while (true) {
      ind /= 2;
      if (ind == 0)
        break;
      seg[ind] = max(seg[2 * ind], seg[2 * ind + 1]);
    }
  }

  ll get_max(ll l, ll r) {
    ll ans = 0;
    l += len;
    r += len;
    while (l < r) {
      if (l % 2 == 1) {
        chmax(ans, seg[l]);
        l++;
      }
      l /= 2;
      if (r % 2 == 1) {
        chmax(ans, seg[r - 1]);
        --r;
      }
      r /= 2;
    }
    return ans;
  }
};

signed main() {
  // cout << fixed << setprecision(10);
  ll n, k;
  cin >> n >> k;
  vl a(n);
  SegmentTree seg(n + 1);
  rep(i, 0, n) {
    ll x;
    cin >> x;
    a[i] = x;
  }
  rep(i, 0, n) {
    ll l = max((ll)1, a[i] - k);
    ll r = min(n, a[i] + k);
    seg.update(a[i], seg.get_max(l, r + 1) + 1);
  }
  cout << seg.get_max(1, n + 1) << endl;

  return 0;
}