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

#include <bits/stdc++.h>
#define pp pair<int, int>
#define ppp pair<pp, int>
#define fi first
#define si second
#define N 300100
using namespace std;
int seg[4 * N], lazy[4 * N], v, m;
void build(int l, int h, int pos) {
  if (l == h) {
    seg[pos] = lazy[pos] = 0;
    return;
  }
  int mid = (l + h) / 2;
  build(l, mid, 2 * pos);
  build(mid + 1, h, 2 * pos + 1);
  seg[pos] = lazy[pos] = 0;
}
void update(int l, int h, int x, int y, int pos) {
  if (lazy[pos]) {
    seg[pos] = lazy[pos];
    if (l != h) {
      lazy[2 * pos] = lazy[pos];
      lazy[2 * pos + 1] = lazy[pos];
    }
    lazy[pos] = 0;
  }
  if (h < x || l > y)
    return;
  if (x <= l && h <= y) {
    seg[pos] = v;

    // cout << l << " D " << h << ' ' << x << ' ' << y << ' ' << seg[pos] <<
    // endl;
    if (l != h) {
      lazy[2 * pos] = v;
      lazy[2 * pos + 1] = v;
    }
    return;
  }
  int mid = (l + h) / 2;
  update(l, mid, x, y, 2 * pos);
  update(mid + 1, h, x, y, 2 * pos + 1);
  seg[pos] = max(seg[2 * pos], seg[2 * pos + 1]);
  //	cout << l << " D " << h << ' ' << seg[pos] << endl;
}
int query(int l, int h, int x, int pos) {
  if (lazy[pos]) {
    seg[pos] = lazy[pos];
    if (l != h) {
      lazy[2 * pos] = lazy[pos];
      lazy[2 * pos + 1] = lazy[pos];
    }
    lazy[pos] = 0;
  }
  if (h < x || l > x)
    return 0;
  if (l == h) {
    return seg[pos];
  }
  int mid = (l + h) / 2;
  int q1 = query(l, mid, x, 2 * pos);
  int q2 = query(mid + 1, h, x, 2 * pos + 1);
  return max(q1, q2);
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cout.precision(10);
  int n, k;
  cin >> n >> k;
  int a[n + 1], dp[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    a[i]++;
    assert(a[i] < N);
  }
  int g[n + 1], s[n + 1];
  for (int i = 1; i <= n; i++) {
    s[i] = query(1, N, a[i], 1);
    v = i;
    update(1, N, a[i], min(N - 1, a[i] + k), 1);
    //	cout << a[i] << " F " << a[i] + k << ' ' << s[i] << endl;
    //	cout << s[i] << ' ';
  }
  // cout << endl;
  build(1, N, 1);
  for (int i = 1; i <= n; i++) {
    g[i] = query(1, N, a[i], 1);
    v = i;
    update(1, N, max(1, a[i] - k), a[i], 1);
  }
  for (int i = 1; i <= n; i++) {
    dp[i] = 1;
    if (s[i])
      dp[i] = max(dp[i], dp[s[i]] + 1);
    if (g[i])
      dp[i] = max(dp[i], dp[g[i]] + 1);
  }
  cout << *max_element(dp + 1, dp + n + 1) << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define pp pair<int, int>
#define ppp pair<pp, int>
#define fi first
#define si second
#define N int(3e5 + 10)
using namespace std;
int seg[4 * N], lazy[4 * N], v;
void build(int l, int h, int pos) {
  if (l == h) {
    seg[pos] = lazy[pos] = 0;
    return;
  }
  int mid = (l + h) / 2;
  build(l, mid, 2 * pos);
  build(mid + 1, h, 2 * pos + 1);
  seg[pos] = lazy[pos] = 0;
}
void update(int l, int h, int x, int y, int pos) {
  if (lazy[pos]) {
    seg[pos] = lazy[pos];
    if (l != h) {
      lazy[2 * pos] = lazy[pos];
      lazy[2 * pos + 1] += lazy[pos];
    }
    lazy[pos] = 0;
  }
  if (h < x || l > y)
    return;
  if (x <= l && h <= y) {
    seg[pos] = v;

    // cout << l << " D " << h << ' ' << x << ' ' << y << ' ' << seg[pos] <<
    // endl;
    if (l != h) {
      lazy[2 * pos] = v;
      lazy[2 * pos + 1] = v;
    }
    return;
  }
  int mid = (l + h) / 2;
  update(l, mid, x, y, 2 * pos);
  update(mid + 1, h, x, y, 2 * pos + 1);
  seg[pos] = max(seg[2 * pos], seg[2 * pos + 1]);
  //	cout << l << " D " << h << ' ' << seg[pos] << endl;
}
int query(int l, int h, int x, int pos) {
  if (lazy[pos]) {
    seg[pos] = lazy[pos];
    if (l != h) {
      lazy[2 * pos] = lazy[pos];
      lazy[2 * pos + 1] = lazy[pos];
    }
    lazy[pos] = 0;
  }
  if (h < x || l > x)
    return 0;
  if (l == h) {
    return seg[pos];
  }
  int mid = (l + h) / 2;
  int q1 = query(l, mid, x, 2 * pos);
  int q2 = query(mid + 1, h, x, 2 * pos + 1);
  return max(q1, q2);
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cout.precision(10);
  int n, k;
  cin >> n >> k;
  int a[n + 1], dp[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    a[i]++;
  }
  int g[n + 1], s[n + 1];
  for (int i = 1; i <= n; i++) {
    s[i] = query(1, N, a[i], 1);
    v = i;
    update(1, N, a[i], min(N - 1, a[i] + k), 1);
    //	cout << a[i] << " F " << a[i] + k << ' ' << s[i] << endl;
    //	cout << s[i] << ' ';
  }
  // cout << endl;
  build(1, N, 1);
  for (int i = 1; i <= n; i++) {
    g[i] = query(1, N, a[i], 1);
    v = i;
    update(1, N, max(1, a[i] - k), a[i], 1);
  }
  for (int i = 1; i <= n; i++) {
    dp[i] = 1;
    if (s[i])
      dp[i] = max(dp[i], dp[s[i]] + 1);
    if (g[i])
      dp[i] = max(dp[i], dp[g[i]] + 1);
  }
  cout << *max_element(dp + 1, dp + n + 1) << endl;
  return 0;
}