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

#include <algorithm>
#include <iostream>
#include <numeric>
#include <queue>
#include <utility>
#include <vector>

#define faster                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pb push_back
#define mp make_pair

typedef long long ll;
using namespace std;
int inf = 1e9;
const int N = 300005;
int n, k;
int a[N];
int pos[N];

struct segtree {

  int size;
  vector<int> t;

  segtree(int n) {
    size = (int)n;
    t.assign(4 * n, 0);
  }

  void build(int v, int tl, int tr) {
    if (tl == tr) {
      t[v] = pos[tl];
    } else {
      int tm = (tl + tr) >> 1;
      build(2 * v, tl, tm);
      build(2 * v + 1, tm + 1, tr);
      t[v] = max(t[2 * v], t[2 * v + 1]);
    }
  }

  int get_max(int v, int tl, int tr, int l, int r) {
    if (l > r)
      return -inf;
    if (l == tl && r == tr)
      return t[v];
    int tm = (tl + tr) >> 1;
    return max(get_max(2 * v, tl, tm, l, min(tm, r)),
               get_max(2 * v + 1, tm + 1, tr, max(tm + 1, l), r));
  }

  void update(int v, int tl, int tr, int pos, int val) {
    if (tl == tr) {
      t[v] = val;
    } else {
      int tm = (tl + tr) >> 1;
      if (pos <= tm)
        update(2 * v, tl, tm, pos, val);
      else
        update(2 * v + 1, tm + 1, tr, pos, val);
      t[v] = max(t[2 * v], t[2 * v + 1]);
    }
  }

  void build() { build(1, 0, size - 1); }

  int get_max(int l, int r) { return get_max(1, 0, size - 1, l, r); }

  void update(int pos, int val) { update(1, 0, size - 1, pos, val); }
};

int main() {
  faster cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> a[i];

  segtree tree(N);
  tree.build();

  for (int i = 0; i < n; i++) {
    int l = max(0, a[i] - k);
    int r = min(300000, a[i] + k);
    int temp = tree.get_max(l, r);
    tree.update(a[i], temp + 1);
  }
  cout << tree.get_max(0, 300001) << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <numeric>
#include <queue>
#include <utility>
#include <vector>

#define faster                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pb push_back
#define mp make_pair

typedef long long ll;
using namespace std;
int inf = 1e9;
const int N = 300005;
int n, k;
int a[N];
int pos[2 * N];

struct segtree {

  int size;
  vector<int> t;

  segtree(int n) {
    size = (int)n;
    t.assign(4 * n, 0);
  }

  void build(int v, int tl, int tr) {
    if (tl == tr) {
      t[v] = pos[tl];
    } else {
      int tm = (tl + tr) >> 1;
      build(2 * v, tl, tm);
      build(2 * v + 1, tm + 1, tr);
      t[v] = max(t[2 * v], t[2 * v + 1]);
    }
  }

  int get_max(int v, int tl, int tr, int l, int r) {
    if (l > r)
      return -inf;
    if (l == tl && r == tr)
      return t[v];
    int tm = (tl + tr) >> 1;
    return max(get_max(2 * v, tl, tm, l, min(tm, r)),
               get_max(2 * v + 1, tm + 1, tr, max(tm + 1, l), r));
  }

  void update(int v, int tl, int tr, int pos, int val) {
    if (tl == tr) {
      t[v] = val;
    } else {
      int tm = (tl + tr) >> 1;
      if (pos <= tm)
        update(2 * v, tl, tm, pos, val);
      else
        update(2 * v + 1, tm + 1, tr, pos, val);
      t[v] = max(t[2 * v], t[2 * v + 1]);
    }
  }

  void build() { build(1, 0, size - 1); }

  int get_max(int l, int r) { return get_max(1, 0, size - 1, l, r); }

  void update(int pos, int val) { update(1, 0, size - 1, pos, val); }
};

int main() {
  faster cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  segtree tree(N);
  tree.build();

  for (int i = 0; i < n; i++) {
    int p = a[i];
    pos[p] = 1;
    tree.update(p, pos[p]);
    int temp = max(tree.get_max(max(0, p - k), p - 1),
                   tree.get_max(p + 1, min(p + k, 2 * N - 1)));
    pos[p] = max(pos[p], temp + 1);
    tree.update(p, pos[p]);
  }
  cout << tree.t[1] << endl;
  return 0;
}