Problem p02537 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, x, y) for (int i = x, i##end = y; i <= i##end; ++i)
#define _rep(i, x, y) for (int i = x, i##end = y; i >= i##end; --i)
#define ll long long
typedef std::pair<int, int> pii;
inline bool up(int &x, const int y) { return x < y ? x = y, 1 : 0; }
inline bool down(int &x, const int y) { return x > y ? x = y, 1 : 0; }
#define N 300005
int n, k, a[N];
int dp[N];
struct segment {
#define ls rt << 1
#define rs rt << 1 | 1
#define lson ls, l, mid
#define rson rs, mid + 1, r
  int mx[N];
  inline void pushup(int rt) { mx[rt] = std::max(mx[ls], mx[rs]); }
  inline void build(int rt, int l, int r) {
    mx[rt] = 0;
    if (l == r)
      return;
    int mid = l + r >> 1;
    build(lson), build(rson), pushup(rt);
  }
  inline void ins(int rt, int l, int r, int x, int v) {
    if (l == r)
      return mx[rt] = v, void();
    int mid = l + r >> 1;
    if (x <= mid)
      ins(lson, x, v);
    else
      ins(rson, x, v);
    pushup(rt);
  }
  inline int qry(int rt, int l, int r, int x, int y) {
    if (x <= l && r <= y)
      return mx[rt];
    int mid = l + r >> 1, ret = 0;
    if (x <= mid)
      up(ret, qry(lson, x, y));
    if (mid < y)
      up(ret, qry(rson, x, y));
    //		std::cerr<<x<<' '<<y<<' '<<ret<<'\n';
    return ret;
  }
} T;
int m;
int main() {
  std::ios::sync_with_stdio(0);
  std::cin >> n >> k;
  rep(i, 1, n) std::cin >> a[i], ++a[i], up(m, a[i]);
  memset(dp, 0, sizeof(dp));
  T.build(1, 1, m);
  int ret = 0;
  rep(i, 1, n) {
    int l = std::max(1, a[i] - k), r = std::min(m, a[i] + k);
    dp[i] = T.qry(1, 1, m, l, r) + 1;
    T.ins(1, 1, m, a[i], dp[i]);
    up(ret, dp[i]);
  }
  //	rep(i,1,n) std::cerr<<dp[i]<<" \n"[i==n];
  std::cout << ret << '\n';
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, x, y) for (int i = x, i##end = y; i <= i##end; ++i)
#define _rep(i, x, y) for (int i = x, i##end = y; i >= i##end; --i)
#define ll long long
typedef std::pair<int, int> pii;
inline bool up(int &x, const int y) { return x < y ? x = y, 1 : 0; }
inline bool down(int &x, const int y) { return x > y ? x = y, 1 : 0; }
#define N 300005
int n, k, a[N];
int dp[N];
struct segment {
#define ls rt << 1
#define rs rt << 1 | 1
#define lson ls, l, mid
#define rson rs, mid + 1, r
  int mx[N << 2];
  inline void pushup(int rt) { mx[rt] = std::max(mx[ls], mx[rs]); }
  inline void build(int rt, int l, int r) {
    mx[rt] = 0;
    if (l == r)
      return;
    int mid = l + r >> 1;
    build(lson), build(rson), pushup(rt);
  }
  inline void ins(int rt, int l, int r, int x, int v) {
    if (l == r)
      return mx[rt] = v, void();
    int mid = l + r >> 1;
    if (x <= mid)
      ins(lson, x, v);
    else
      ins(rson, x, v);
    pushup(rt);
  }
  inline int qry(int rt, int l, int r, int x, int y) {
    if (x <= l && r <= y)
      return mx[rt];
    int mid = l + r >> 1, ret = 0;
    if (x <= mid)
      up(ret, qry(lson, x, y));
    if (mid < y)
      up(ret, qry(rson, x, y));
    //		std::cerr<<x<<' '<<y<<' '<<ret<<'\n';
    return ret;
  }
} T;
int m;
int main() {
  std::ios::sync_with_stdio(0);
  std::cin >> n >> k;
  rep(i, 1, n) std::cin >> a[i], ++a[i], up(m, a[i]);
  memset(dp, 0, sizeof(dp));
  T.build(1, 1, m);
  int ret = 0;
  rep(i, 1, n) {
    int l = std::max(1, a[i] - k), r = std::min(m, a[i] + k);
    dp[i] = T.qry(1, 1, m, l, r) + 1;
    T.ins(1, 1, m, a[i], dp[i]);
    up(ret, dp[i]);
  }
  //	rep(i,1,n) std::cerr<<dp[i]<<" \n"[i==n];
  std::cout << ret << '\n';
  return 0;
}