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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ordered_set                                                            \\
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_Node_update>
#define ll long long
#define ull unsigned long long
#define pb push_back
#define inf 1e18
#define mk make_pair
#define ld long double
#define mod 998244353
#define fi first
#define se second
#define fastIO                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define test                                                                   \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
#define setbits __builtin_popcount
#define endl '\\n'
#define LOCAL
#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x)                                                                 \\
  sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(    \\
            c i) {
sim > struct rge {
  c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
  ~debug() { cerr << endl; }
  eni(!=) cerr << boolalpha << i;
  ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
  sim dor(const c &) { ris; }
#endif
}
;
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
const ll maxn = 600001;
ll seg[4 * maxn];
void build(ll v, ll l, ll r) {
  if (l == r) {
    seg[v] = 0;
    return;
  }
  ll mid = (l + r) / 2;
  build(2 * v, l, mid);
  build(2 * v + 1, mid + 1, r);
  seg[v] = max(seg[2 * v], seg[2 * v + 1]);
}
void update(ll v, ll l, ll r, ll pos, ll val) {
  if (l == r) {
    seg[v] = val;
    return;
  }
  ll mid = (l + r) / 2;
  if (pos <= mid) {
    update(2 * v, l, mid, pos, val);
  } else {
    update(2 * v + 1, mid + 1, r, pos, val);
  }
  seg[v] = max(seg[2 * v], seg[2 * v + 1]);
}
ll query(ll v, ll l, ll r, ll x, ll y) {
  if (y < x) {
    return 0;
  }
  if (x <= l && r <= y) {
    return seg[v];
  }
  ll mid = (l + r) / 2;
  ll val, val1;
  val = query(2 * v, l, mid, x, min(mid, y));
  val1 = query(2 * v + 1, mid + 1, r, max(mid + 1, x), y);
  return max(val, val1);
}
int main() {
  fastIO;
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  build(1, 0, n - 1);
  ll x, y, val, val1;
  for (int i = 0; i < n; i++) {
    x = max((ll)0, a[i] - k);
    y = a[i] + k;
    val = query(1, 0, maxn, x, y);
    update(1, 0, maxn, a[i], val + 1);
  }
  cout << seg[1] << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ordered_set                                                            \\
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_Node_update>
#define ll long long
#define ull unsigned long long
#define pb push_back
#define inf 1e18
#define mk make_pair
#define ld long double
#define mod 998244353
#define fi first
#define se second
#define fastIO                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define test                                                                   \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
#define setbits __builtin_popcount
#define endl '\\n'
#define LOCAL
#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x)                                                                 \\
  sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(    \\
            c i) {
sim > struct rge {
  c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
  ~debug() { cerr << endl; }
  eni(!=) cerr << boolalpha << i;
  ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
  sim dor(const c &) { ris; }
#endif
}
;
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
const ll maxn = 600001;
ll seg[4 * maxn];
void build(ll v, ll l, ll r) {
  if (l == r) {
    seg[v] = 0;
    return;
  }
  ll mid = (l + r) / 2;
  build(2 * v, l, mid);
  build(2 * v + 1, mid + 1, r);
  seg[v] = max(seg[2 * v], seg[2 * v + 1]);
}
void update(ll v, ll l, ll r, ll pos, ll val) {
  if (l == r) {
    seg[v] = max(val, seg[v]);
    return;
  }
  ll mid = (l + r) / 2;
  if (pos <= mid) {
    update(2 * v, l, mid, pos, val);
  } else {
    update(2 * v + 1, mid + 1, r, pos, val);
  }
  seg[v] = max(seg[2 * v], seg[2 * v + 1]);
}
ll query(ll v, ll l, ll r, ll x, ll y) {
  if (y < x) {
    return -inf;
  }
  if (x <= l && r <= y) {
    return seg[v];
  }
  ll mid = (l + r) / 2;
  ll val, val1;
  val = query(2 * v, l, mid, x, min(mid, y));
  val1 = query(2 * v + 1, mid + 1, r, max(mid + 1, x), y);
  return max(val, val1);
}
int main() {
  fastIO;
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  build(1, 0, n - 1);
  ll x, y, val, val1;
  for (int i = 0; i < n; i++) {
    x = max((ll)0, a[i] - k);
    y = a[i] + k;
    val = query(1, 0, n - 1, x, y);
    update(1, 0, n - 1, a[i], val + 1);
  }
  cout << seg[1] << endl;
}