Problem p02537 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define sz(a) a.size()
#define all(a) a.begin(), a.end()
#define lb lower_bound
#define ub upper_bound
#define owo                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);
#define MOD (ll)(1e8)
#define INF (ll)(1e18)
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#define time__(d)                                                              \
  for (long blockTime = 0;                                                     \
       (blockTime == 0 ? (blockTime = clock()) != 0 : false);                  \
       debug("%s time : %.4fs\n", d,                                           \
             (double)(clock() - blockTime) / CLOCKS_PER_SEC))
typedef long long int ll;
typedef long double ld;
typedef pair<ll, ll> PII;
typedef pair<int, int> pii;
typedef vector<vector<int>> vii;
typedef vector<vector<ll>> VII;
ll gcd(ll a, ll b) {
  if (!b)
    return a;
  else
    return gcd(b, a % b);
}
class pointseg {
private:
  int n;
  vector<ll> t, a;
  void upd(int v, int tl, int tr, int pos, ll val) {
    if (tl == tr) {
      t[v] = val;
      return;
    }
    int tm = (tl + tr) / 2;
    if (pos <= tm) {
      upd(2 * v, tl, tm, pos, val);
    } else {
      upd(2 * v + 1, tm + 1, tr, pos, val);
    }
    t[v] = merge(t[2 * v], t[2 * v + 1]);
  }
  ll query(int v, int l, int r, int tl, int tr) {
    if (l > r)
      return 0;
    if (l == tl && r == tr) {
      return t[v];
    }
    int tm = (tl + tr) / 2;
    return merge(query(2 * v, l, min(r, tm), tl, tm),
                 query(2 * v + 1, max(l, tm + 1), r, tm + 1, tr));
  }
  void build(int v, int tl, int tr) {
    if (tl == tr) {
      t[v] = a[tl];
      return;
    }
    int tm = (tl + tr) / 2;
    build(2 * v, tl, tm);
    build(2 * v + 1, tm + 1, tr);
    t[v] = merge(t[2 * v], t[2 * v + 1]);
  }

public:
  ll merge(ll a, ll b) { return max(a, b); }
  void upd(int pos, ll val) { upd(1, 1, n, pos, val); }
  ll query(int l, int r) { return query(1, l, r, 1, n); }
  void build() { build(1, 1, n); }
  pointseg(int _n, vector<ll> _a = {}) {
    n = _n;
    t.assign(4 * (n + 4), 0);
    a = _a;
  }
  pointseg() {}
  void reset(int _n, vector<ll> _a = {}) {
    n = _n;
    t.clear();
    t.assign(4 * (_n + 4), 0);
    a.clear();
    a = _a;
  }
};
int main() {
  owo ll n, k;
  cin >> n >> k;
  vector<ll> a(n + 1);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]++;
  }
  ll ans = 1;
  pointseg s(400000);
  for (int i = 0; i < n; i++) {
    ll l = max(1LL, a[i] - k);
    ll r = a[i] + k;
    ll res = s.query(l, r);
    ll now = s.query(a[i], a[i]);
    if (res + 1 > now)
      s.upd(a[i], res + 1);
    ans = max(ans, now);
    ans = max(ans, res + 1);
  }
  cout << ans;
}


Fixed submission:
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define sz(a) a.size()
#define all(a) a.begin(), a.end()
#define lb lower_bound
#define ub upper_bound
#define owo                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);
#define MOD (ll)(1e8)
#define INF (ll)(1e18)
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#define time__(d)                                                              \
  for (long blockTime = 0;                                                     \
       (blockTime == 0 ? (blockTime = clock()) != 0 : false);                  \
       debug("%s time : %.4fs\n", d,                                           \
             (double)(clock() - blockTime) / CLOCKS_PER_SEC))
typedef long long int ll;
typedef long double ld;
typedef pair<ll, ll> PII;
typedef pair<int, int> pii;
typedef vector<vector<int>> vii;
typedef vector<vector<ll>> VII;
ll gcd(ll a, ll b) {
  if (!b)
    return a;
  else
    return gcd(b, a % b);
}
class pointseg {
private:
  int n;
  vector<ll> t, a;
  void upd(int v, int tl, int tr, int pos, ll val) {
    if (tl == tr) {
      t[v] = val;
      return;
    }
    int tm = (tl + tr) / 2;
    if (pos <= tm) {
      upd(2 * v, tl, tm, pos, val);
    } else {
      upd(2 * v + 1, tm + 1, tr, pos, val);
    }
    t[v] = merge(t[2 * v], t[2 * v + 1]);
  }
  ll query(int v, int l, int r, int tl, int tr) {
    if (l > r)
      return 0;
    if (l == tl && r == tr) {
      return t[v];
    }
    int tm = (tl + tr) / 2;
    return merge(query(2 * v, l, min(r, tm), tl, tm),
                 query(2 * v + 1, max(l, tm + 1), r, tm + 1, tr));
  }
  void build(int v, int tl, int tr) {
    if (tl == tr) {
      t[v] = a[tl];
      return;
    }
    int tm = (tl + tr) / 2;
    build(2 * v, tl, tm);
    build(2 * v + 1, tm + 1, tr);
    t[v] = merge(t[2 * v], t[2 * v + 1]);
  }

public:
  ll merge(ll a, ll b) { return max(a, b); }
  void upd(int pos, ll val) { upd(1, 1, n, pos, val); }
  ll query(int l, int r) { return query(1, l, r, 1, n); }
  void build() { build(1, 1, n); }
  pointseg(int _n, vector<ll> _a = {}) {
    n = _n;
    t.assign(4 * (n + 4), 0);
    a = _a;
  }
  pointseg() {}
  void reset(int _n, vector<ll> _a = {}) {
    n = _n;
    t.clear();
    t.assign(4 * (_n + 4), 0);
    a.clear();
    a = _a;
  }
};
int main() {
  owo ll n, k;
  cin >> n >> k;
  vector<ll> a(n + 1);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]++;
  }
  ll ans = 1;
  pointseg s(400000);
  for (int i = 0; i < n; i++) {
    ll l = max(1LL, a[i] - k);
    ll r = min(ll(300001), a[i] + k);
    ll res = s.query(l, r);
    ll now = s.query(a[i], a[i]);
    if (res + 1 > now)
      s.upd(a[i], res + 1);
    ans = max(ans, now);
    ans = max(ans, res + 1);
  }
  cout << ans;
}