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

#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i)
#define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i)

namespace IO {
#define gc getchar()
template <typename T> inline void read(T &x) {
  x = 0;
  bool f = 1;
  char ch;
  for (ch = gc; ch < '0' || '9' < ch; ch = gc)
    if (ch == '-')
      f ^= 1;
  for (; '0' <= ch && ch <= '9'; ch = gc)
    x = (x << 3) + (x << 1) + (ch ^ 48);
  x = f ? x : -x;
}
#undef gc
} // namespace IO

const int MAXN = 1e6 + 10;
const int MAXVAL = 3e5;

int n, arr[MAXN], K, f[MAXN], ans;

struct Segment_Tree {
#define mid ((l + r) >> 1)
#define ls k << 1, l, mid
#define rs k << 1 | 1, mid + 1, r
  struct Node {
    int val;
  } t[MAXN << 2];
  void pushup(int k) { t[k].val = max(t[k << 1 | 1].val, t[k << 1].val); }
  void modify(int k, int l, int r, int pos, int val) {
    if (l == r)
      return t[k].val = val, void();
    if (pos <= mid)
      modify(ls, pos, val);
    else
      modify(rs, pos, val);
    pushup(k);
  }
  int query(int k, int l, int r, int ql, int qr) {
    if (ql <= l && r <= qr)
      return t[k].val;
    int res = 0;
    if (ql <= mid)
      res = max(res, query(ls, ql, qr));
    if (qr > mid)
      res = max(res, query(rs, ql, qr));
    return res;
  }
} sgt;

int main() {
  IO::read(n), IO::read(K);
  rep(i, 1, n) IO::read(arr[i]);
  rep(i, 1, n) {
    f[i] = sgt.query(1, 1, MAXVAL, max(0, arr[i] - K) + 2,
                     min(MAXVAL, arr[i] + K) + 2) +
           1;
    sgt.modify(1, 1, MAXVAL, arr[i] + 2, f[i]);
    ans = max(ans, f[i]);
  }
  printf("%d\\n", ans);
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i)
#define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i)

namespace IO {
#define gc getchar()
template <typename T> inline void read(T &x) {
  x = 0;
  bool f = 1;
  char ch;
  for (ch = gc; ch < '0' || '9' < ch; ch = gc)
    if (ch == '-')
      f ^= 1;
  for (; '0' <= ch && ch <= '9'; ch = gc)
    x = (x << 3) + (x << 1) + (ch ^ 48);
  x = f ? x : -x;
}
#undef gc
} // namespace IO

const int MAXN = 1e6 + 10;
const int MAXVAL = 3e5;

int n, arr[MAXN], K, f[MAXN], ans;

struct Segment_Tree {
#define mid ((l + r) >> 1)
#define ls k << 1, l, mid
#define rs k << 1 | 1, mid + 1, r
  struct Node {
    int val;
  } t[MAXN << 2];
  void pushup(int k) { t[k].val = max(t[k << 1 | 1].val, t[k << 1].val); }
  void modify(int k, int l, int r, int pos, int val) {
    if (l == r)
      return t[k].val = val, void();
    if (pos <= mid)
      modify(ls, pos, val);
    else
      modify(rs, pos, val);
    pushup(k);
  }
  int query(int k, int l, int r, int ql, int qr) {
    if (ql <= l && r <= qr)
      return t[k].val;
    int res = 0;
    if (ql <= mid)
      res = max(res, query(ls, ql, qr));
    if (qr > mid)
      res = max(res, query(rs, ql, qr));
    return res;
  }
} sgt;

int main() {
  IO::read(n), IO::read(K);
  rep(i, 1, n) IO::read(arr[i]);
  rep(i, 1, n) {
    f[i] = sgt.query(1, 1, n, max(0, arr[i] - K) + 1,
                     min(MAXVAL, arr[i] + K) + 1) +
           1;
    sgt.modify(1, 1, n, arr[i] + 1, f[i]);
    ans = max(ans, f[i]);
  }
  printf("%d\\n", ans);
  return 0;
}