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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll INF = 1e9 + 7;
ll MOD = 998244353;
typedef pair<long long, ll> ii;
#define iii pair<ii, ll>
#define f(i, a, b) for (ll i = a; i < b; i++)
#define rf(i, a, b) for (long long i = a; i >= b; i--)
#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define w(t) while (t--)
#define c(n)                                                                   \\
  ;                                                                            \\
  cin >> n;
#define p(n) cout << n;
#define pl(n) cout << n << "\\n";
#define ps(n)                                                                  \\
  ;                                                                            \\
  cout << n << " ";
#define F first
#define S second
#define pb(a) push_back(a)
#define all(x) (x).begin(), (x).end()
#define ull unsigned long long
#define vll vector<ll>
#define vii vector<ii>
#define mkp make_pair
#define ld long double
#define arrin(a, n)                                                            \\
  f(i, 0, n) { cin >> a[i]; }
#define arrout(a, n)                                                           \\
  f(i, 0, n) { cout << a[i] << " "; }
#define printclock                                                             \\
  cerr << "Time : " << *(ld)clock() / (ld)CLOCKS_PER_SEC << "ms\\n";
#define PI (2 * acos(0))
#define EPS 1e-18
ll seg[1200005] = {0};
void update(ll s, ll e, ll qs, ll qe, ll idx, ll k) {
  if (s == e && s == qs) {
    seg[idx] = k;
    return;
  }
  if (qs > e || qe < s) {
    return;
  }
  ll mid = (s + e) / 2;
  update(s, mid, qs, qe, idx * 2, k);
  update(mid + 1, e, qs, qe, idx * 2 + 1, k);
  seg[idx] = max(seg[idx * 2], seg[idx * 2 + 1]);
}
ll query(ll s, ll e, ll qs, ll qe, ll idx) {
  if (qs <= s && e <= qe) {
    return seg[idx];
  }
  if (qs > e || qe < s) {
    return 0;
  }
  ll mid = (s + e) / 2;
  ll a = query(s, mid, qs, qe, idx * 2);
  ll b = query(mid + 1, e, qs, qe, idx * 2 + 1);
  return max(a, b);
}
int main(void) {
  fastio;
  ll n, k;
  cin >> n >> k;
  ll arr[n];
  arrin(arr, n);
  f(i, 0, n) { arr[i]++; }
  ll dp[n];
  ll ans = 0;
  ll n2 = 300001;
  rf(i, n - 1, 0) {
    dp[i] = 1 + query(1, n2, max(1LL, arr[i] - k), min(n2, arr[i] + k), 1);
    update(1, n2, arr[i], arr[i], 1, dp[i]);
    ans = max(ans, dp[i]);
  }
  pl(ans);
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll INF = 1e9 + 7;
ll MOD = 998244353;
typedef pair<long long, ll> ii;
#define iii pair<ii, ll>
#define f(i, a, b) for (ll i = a; i < b; i++)
#define rf(i, a, b) for (long long i = a; i >= b; i--)
#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define w(t) while (t--)
#define c(n)                                                                   \\
  ;                                                                            \\
  cin >> n;
#define p(n) cout << n;
#define pl(n) cout << n << "\\n";
#define ps(n)                                                                  \\
  ;                                                                            \\
  cout << n << " ";
#define F first
#define S second
#define pb(a) push_back(a)
#define all(x) (x).begin(), (x).end()
#define ull unsigned long long
#define vll vector<ll>
#define vii vector<ii>
#define mkp make_pair
#define ld long double
#define arrin(a, n)                                                            \\
  f(i, 0, n) { cin >> a[i]; }
#define arrout(a, n)                                                           \\
  f(i, 0, n) { cout << a[i] << " "; }
#define printclock                                                             \\
  cerr << "Time : " << *(ld)clock() / (ld)CLOCKS_PER_SEC << "ms\\n";
#define PI (2 * acos(0))
#define EPS 1e-18
ll seg[1000005] = {0};
void update(ll s, ll e, ll qs, ll qe, ll idx, ll k) {
  if (s == e && s == qs) {
    seg[idx] = k;
    return;
  }
  if (qs > e || qe < s) {
    return;
  }
  ll mid = (s + e) / 2;
  update(s, mid, qs, qe, idx * 2, k);
  update(mid + 1, e, qs, qe, idx * 2 + 1, k);
  seg[idx] = max(seg[idx * 2], seg[idx * 2 + 1]);
}
ll query(ll s, ll e, ll qs, ll qe, ll idx) {
  if (qs <= s && e <= qe) {
    return seg[idx];
  }
  if (qs > e || qe < s) {
    return 0;
  }
  ll mid = (s + e) / 2;
  ll a = query(s, mid, qs, qe, idx * 2);
  ll b = query(mid + 1, e, qs, qe, idx * 2 + 1);
  return max(a, b);
}
int main(void) {
  fastio;
  ll n, k;
  cin >> n >> k;
  ll arr[n];
  arrin(arr, n);
  ll dp[n];
  ll ans = 0;
  ll n2 = 300000;
  rf(i, n - 1, 0) {
    dp[i] = 1 + query(1, n2, max(1LL, arr[i] - k), min(n2, arr[i] + k), 1);
    update(1, n2, arr[i], arr[i], 1, dp[i]);
    ans = max(ans, dp[i]);
  }
  pl(ans);
}