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

#include <bits/stdc++.h>
using namespace std;
#define maxn 300005
int tree[4 * maxn], f[maxn], a[maxn], lazy[4 * maxn];
void pushup(int i) { tree[i] = max(tree[i << 1], tree[i << 1 | 1]); }
void xia(int i, int l, int r) {
  tree[i << 1] = max(tree[i << 1], lazy[i]);
  tree[i << 1 | 1] = max(tree[i << 1 | 1], lazy[i]);
  lazy[i << 1] = max(lazy[i << 1], lazy[i]);
  lazy[i << 1 | 1] = max(lazy[i << 1 | 1], lazy[i]);
  lazy[i] = 0;
}
void update(int i, int l, int r, int x, int y, int val) {
  if (x <= l && r <= y) {
    tree[i] = max(tree[i], val);
    lazy[i] = max(lazy[i], val);
    return;
  }
  if (lazy[i])
    xia(i, l, r);
  int mid = (l + r) / 2;
  if (x <= mid)
    update(i << 1, l, mid, x, y, val);
  if (y > mid)
    update(i << 1 | 1, mid + 1, r, x, y, val);
  pushup(i);
}
int query(int i, int l, int r, int x, int y) {
  if (x <= l && r <= y)
    return tree[i];
  int maxm = 0;
  if (lazy[i])
    xia(i, l, r);
  int mid = (l + r) / 2;
  if (x <= mid)
    maxm = max(maxm, query(i << 1, l, mid, x, y));
  if (y > mid)
    maxm = max(maxm, query(i << 1 | 1, mid + 1, r, x, y));
  return maxm;
}
int tree1[4 * maxn], lazy1[4 * maxn];
void pushup1(int i) { tree1[i] = max(tree1[i << 1], tree1[i << 1 | 1]); }
void xia1(int i, int l, int r) {
  tree1[i << 1] = max(tree1[i << 1], lazy1[i]);
  tree1[i << 1 | 1] = max(tree1[i << 1 | 1], lazy1[i]);
  lazy1[i << 1] = max(lazy1[i << 1], lazy1[i]);
  lazy1[i << 1 | 1] = max(lazy1[i << 1 | 1], lazy1[i]);
  lazy1[i] = 0;
}
void update1(int i, int l, int r, int x, int y, int val) {
  if (x <= l && r <= y) {
    tree1[i] = max(tree1[i], val);
    lazy1[i] = max(lazy1[i], val);
    return;
  }
  if (lazy1[i])
    xia1(i, l, r);
  int mid = (l + r) / 2;
  if (x <= mid)
    update1(i << 1, l, mid, x, y, val);
  if (y > mid)
    update1(i << 1 | 1, mid + 1, r, x, y, val);
  pushup1(i);
}
int query1(int i, int l, int r, int x, int y) {
  if (x <= l && r <= y)
    return tree1[i];
  int maxm = 0;
  if (lazy1[i])
    xia1(i, l, r);
  int mid = (l + r) / 2;
  if (x <= mid)
    maxm = max(maxm, query1(i << 1, l, mid, x, y));
  if (y > mid)
    maxm = max(maxm, query1(i << 1 | 1, mid + 1, r, x, y));
  return maxm;
}
int ans = 0, n, k;
int da = 0;
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]), da = max(da, a[i]);
  for (int i = 1; i <= n; i++) {
    int tmp = 0;
    tmp = max(tmp, query1(1, 0, da, max(0, a[i] - k), a[i]));
    tmp = max(tmp, query(1, 0, da, a[i], min(da, a[i] + k)));
    f[i] = tmp + 1;
    ans = max(f[i], ans);
    update1(1, 0, da, max(a[i] - k, 0), a[i], f[i]);
    update(1, 0, da, a[i], min(da, a[i] + k), f[i]);
  }
  cout << ans;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define maxn 3000005
int tree[4 * maxn], f[maxn], a[maxn], lazy[4 * maxn];
void pushup(int i) { tree[i] = max(tree[i << 1], tree[i << 1 | 1]); }
void xia(int i, int l, int r) {
  tree[i << 1] = max(tree[i << 1], lazy[i]);
  tree[i << 1 | 1] = max(tree[i << 1 | 1], lazy[i]);
  lazy[i << 1] = max(lazy[i << 1], lazy[i]);
  lazy[i << 1 | 1] = max(lazy[i << 1 | 1], lazy[i]);
  lazy[i] = 0;
}
void update(int i, int l, int r, int x, int y, int val) {
  if (x <= l && r <= y) {
    tree[i] = max(tree[i], val);
    lazy[i] = max(lazy[i], val);
    return;
  }
  if (lazy[i])
    xia(i, l, r);
  int mid = (l + r) / 2;
  if (x <= mid)
    update(i << 1, l, mid, x, y, val);
  if (y > mid)
    update(i << 1 | 1, mid + 1, r, x, y, val);
  pushup(i);
}
int query(int i, int l, int r, int x, int y) {
  if (x <= l && r <= y)
    return tree[i];
  int maxm = 0;
  if (lazy[i])
    xia(i, l, r);
  int mid = (l + r) / 2;
  if (x <= mid)
    maxm = max(maxm, query(i << 1, l, mid, x, y));
  if (y > mid)
    maxm = max(maxm, query(i << 1 | 1, mid + 1, r, x, y));
  return maxm;
}
int tree1[4 * maxn], lazy1[4 * maxn];
void pushup1(int i) { tree1[i] = max(tree1[i << 1], tree1[i << 1 | 1]); }
void xia1(int i, int l, int r) {
  tree1[i << 1] = max(tree1[i << 1], lazy1[i]);
  tree1[i << 1 | 1] = max(tree1[i << 1 | 1], lazy1[i]);
  lazy1[i << 1] = max(lazy1[i << 1], lazy1[i]);
  lazy1[i << 1 | 1] = max(lazy1[i << 1 | 1], lazy1[i]);
  lazy1[i] = 0;
}
void update1(int i, int l, int r, int x, int y, int val) {
  if (x <= l && r <= y) {
    tree1[i] = max(tree1[i], val);
    lazy1[i] = max(lazy1[i], val);
    return;
  }
  if (lazy1[i])
    xia1(i, l, r);
  int mid = (l + r) / 2;
  if (x <= mid)
    update1(i << 1, l, mid, x, y, val);
  if (y > mid)
    update1(i << 1 | 1, mid + 1, r, x, y, val);
  pushup1(i);
}
int query1(int i, int l, int r, int x, int y) {
  if (x <= l && r <= y)
    return tree1[i];
  int maxm = 0;
  if (lazy1[i])
    xia1(i, l, r);
  int mid = (l + r) / 2;
  if (x <= mid)
    maxm = max(maxm, query1(i << 1, l, mid, x, y));
  if (y > mid)
    maxm = max(maxm, query1(i << 1 | 1, mid + 1, r, x, y));
  return maxm;
}
int ans = 0, n, k;
int da = 0;
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]), da = max(da, a[i]);
  for (int i = 1; i <= n; i++) {
    int tmp = 0;
    tmp = max(tmp, query1(1, 1, da, max(0, a[i] - k), a[i]));
    tmp = max(tmp, query(1, 1, da, a[i], min(da, a[i] + k)));
    f[i] = tmp + 1;
    ans = max(f[i], ans);
    update1(1, 1, da, max(a[i] - k, 0), a[i], f[i]);
    update(1, 1, da, a[i], min(da, a[i] + k), f[i]);
  }
  cout << ans;
}