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

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define all(x) (x).begin(), (x).end()

typedef vector<int> vi;

const int N = 300010;
int nxt = 0;
int root[N];

struct Node {
  int ls, rs, val, id = 0;
} node[N * 50];

void update(int &n, int pre, int b, int e, int pos, int id) {
  n = ++nxt;
  node[n] = node[pre];
  if (b == e && b == pos) {
    node[n].val++;
    node[n].id = max(node[n].id, id);
    node[n].ls = node[n].rs = 0;
    return;
  }
  int mid = (b + e) / 2;
  if (pos <= mid)
    update(node[n].ls, node[pre].ls, b, mid, pos, id);
  else
    update(node[n].rs, node[pre].rs, mid + 1, e, pos, id);
  node[n].val = node[node[n].ls].val + node[node[n].rs].val;
  node[n].id = max(node[node[n].ls].id, node[node[n].rs].id);
}
int q;
vi A;

int query_sum(int root, int b, int e, int l, int r) {

  if (l > r)
    return 0;
  if (l > e || b > r)
    return 0;
  if (b == l && r == e) {
    return node[root].val;
  }
  int mid = (b + e) / 2;
  return query_sum(node[root].ls, b, mid, l, min(r, mid)) +
         query_sum(node[root].rs, mid + 1, e, max(l, mid + 1), r);
}

int query(int root, int b, int e, int l, int r) {
  if (l > r)
    return 0;
  if (l > e || b > r)
    return 0;
  if (b == l && r == e)
    return node[root].id;
  int mid = (b + e) / 2;
  return max(query(node[root].ls, b, mid, l, min(r, mid)),
             query(node[root].rs, mid + 1, e, max(l, mid + 1), r));
}

int main() {
  int n;
  cin >> n >> q;
  int K = 300001;
  A.resize(K);
  for (int j = 0; j < n; ++j) {
    cin >> A[j];
  }
  vi ans(K, 1);
  int res = 1;
  for (int k = 1; k <= n; ++k)
    update(root[k], root[k - 1], 0, K - 1, A[k - 1],
           k - 1); // 0 -> is the initial empty segment tree

  for (int i = 1; i <= n; ++i) {
    int sum1 = query_sum(root[i - 1], 0, K - 1, max(A[i - 1] - q, 0), A[i - 1]);
    int sum2 =
        query_sum(root[i - 1], 0, K - 1, A[i - 1], min(K - 1, A[i - 1] + q));
    if (sum1) {
      int ind = query(root[i - 1], 0, K - 1, max(A[i - 1] - q, 0), A[i - 1]);
      ans[i - 1] = 1 + ans[ind];
    }
    if (sum2) {
      int ind =
          query(root[i - 1], 0, K - 1, A[i - 1], min(K - 1, A[i - 1] + q));
      ans[i - 1] = max(ans[i - 1], 1 + ans[ind]);
    }

    res = max(res, ans[i - 1]);
  }
  cout << res << "\\n";
}

Error: -11

Fixed submission:

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define all(x) (x).begin(), (x).end()

typedef vector<int> vi;

const int N = 300010;
int nxt = 0;
int root[N];

struct Node {
  int ls, rs, val, id = 0;
} node[N * 50];

void update(int &n, int pre, int b, int e, int pos, int id) {
  n = ++nxt;
  node[n] = node[pre];
  if (b == e && b == pos) {
    node[n].val++;
    node[n].id = max(node[n].id, id);
    node[n].ls = node[n].rs = 0;
    return;
  }
  int mid = (b + e) / 2;
  if (pos <= mid)
    update(node[n].ls, node[pre].ls, b, mid, pos, id);
  else
    update(node[n].rs, node[pre].rs, mid + 1, e, pos, id);
  node[n].val = node[node[n].ls].val + node[node[n].rs].val;
  node[n].id = max(node[node[n].ls].id, node[node[n].rs].id);
}
int q;
vi A;

int query_sum(int root, int b, int e, int l, int r) {

  if (l > r)
    return 0;
  if (l > e || b > r)
    return 0;
  if (b == l && r == e) {
    return node[root].val;
  }
  int mid = (b + e) / 2;
  return query_sum(node[root].ls, b, mid, l, min(r, mid)) +
         query_sum(node[root].rs, mid + 1, e, max(l, mid + 1), r);
}

int query(int root, int b, int e, int l, int r) {
  if (l > r)
    return 0;
  if (l > e || b > r)
    return 0;
  if (b == l && r == e)
    return node[root].id;
  int mid = (b + e) / 2;
  return max(query(node[root].ls, b, mid, l, min(r, mid)),
             query(node[root].rs, mid + 1, e, max(l, mid + 1), r));
}

int main() {
  int n;
  cin >> n >> q;
  int K = 300000;
  A.resize(K);
  for (int j = 0; j < n; ++j) {
    cin >> A[j];
  }
  vi ans(K, 1);
  int res = 1;
  for (int k = 1; k <= n; ++k)
    update(root[k], root[k - 1], 0, K - 1, A[k - 1],
           k - 1); // 0 -> is the initial empty segment tree

  for (int i = 1; i <= n; ++i) {
    int sum1 = query_sum(root[i - 1], 0, K - 1, max(A[i - 1] - q, 0), A[i - 1]);
    int sum2 =
        query_sum(root[i - 1], 0, K - 1, A[i - 1], min(K - 1, A[i - 1] + q));
    if (sum1) {
      int ind = query(root[i - 1], 0, K - 1, max(A[i - 1] - q, 0), A[i - 1]);
      ans[i - 1] = 1 + ans[ind];
    }
    if (sum2) {
      int ind =
          query(root[i - 1], 0, K - 1, A[i - 1], min(K - 1, A[i - 1] + q));
      ans[i - 1] = max(ans[i - 1], 1 + ans[ind]);
    }

    res = max(res, ans[i - 1]);
  }
  cout << res << "\\n";
  cout << nxt << "\\n";
}