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

#include <bits/stdc++.h>
#ifdef _DEBUG
#include "_DEBUG.hpp"
#endif
#define int long long
using namespace std;
using P = pair<int, int>;
const int inf = 2e18;
const int mod = 1e9 + 7;

template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (T &in : v)
    is >> in;
  return is;
}

template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }

template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}

template <class T, class V>
typename enable_if<is_class<T>::value == 0>::type fill(T &t, const V &v) {
  t = v;
}

template <class T, class V>
typename enable_if<is_class<T>::value != 0>::type fill(T &t, const V &v) {
  for (auto &e : t)
    fill(e, v);
}

template <class T> struct SegmentTree {
  using F = function<T(T, T)>;
  F f;
  T inf;
  int n;
  vector<T> node;

  SegmentTree() {}
  SegmentTree(vector<T> v, T inf, F f) : inf(inf), f(f) {
    n = 1;
    while (n < v.size())
      n *= 2;
    node.resize(2 * n - 1, inf);
    for (int i = 0; i < v.size(); i++)
      node[i + n - 1] = v[i];
    for (int i = n - 2; i >= 0; i--)
      node[i] = f(node[2 * i + 1], node[2 * i + 2]);
  }

  void update(int k, T val) {
    k += n - 1;
    node[k] = val;
    while (k > 0) {
      k = (k - 1) / 2;
      node[k] = f(node[2 * k + 1], node[2 * k + 2]);
    }
  }

  void add(int k, T val) {
    k += n - 1;
    node[k] += val;
    while (k > 0) {
      k = (k - 1) / 2;
      node[k] = f(node[2 * k + 1], node[2 * k + 2]);
    }
  }

  // 区間[a, b)の値を返す
  T operator[](int x) { return getval(x, x + 1); }
  T getval(int a, int b) { return getval(a, b, 0, 0, n); }
  T getval(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return inf;
    if (a <= l && r <= b)
      return node[k];
    T vl = getval(a, b, 2 * k + 1, l, (l + r) / 2);
    T vr = getval(a, b, 2 * k + 2, (l + r) / 2, r);
    return f(vl, vr);
  }

  void print(int n) {
    for (int i = 0; i < n; i++) {
      cout << getval(i, i + 1) << " ";
    }
    cout << endl;
  }
};

signed main() {

  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  cin >> a;

  SegmentTree<int> seg(vector<int>(4e5 + 1, 0), -inf,
                       [&](int x, int y) { return max(x, y); });
  for (int i = 0; i < n; i++) {
    int l = max(0ll, a[i] - k);
    int r = min((int)4e5, a[i] + k);
    seg.update(a[i], seg.getval(l, r + 1) + 1);
  }
  int ans = seg.getval(0, 4e5);
  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#ifdef _DEBUG
#include "_DEBUG.hpp"
#endif
#define int long long
using namespace std;
using P = pair<int, int>;
const int inf = 2e18;
const int mod = 1e9 + 7;

template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (T &in : v)
    is >> in;
  return is;
}

template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }

template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}

template <class T, class V>
typename enable_if<is_class<T>::value == 0>::type fill(T &t, const V &v) {
  t = v;
}

template <class T, class V>
typename enable_if<is_class<T>::value != 0>::type fill(T &t, const V &v) {
  for (auto &e : t)
    fill(e, v);
}

template <class T> struct SegmentTree {
  using F = function<T(T, T)>;
  F f;
  T inf;
  int n;
  vector<T> node;

  SegmentTree() {}
  SegmentTree(vector<T> v, T inf, F f) : inf(inf), f(f) {
    n = 1;
    while (n < v.size())
      n *= 2;
    node.resize(2 * n - 1, inf);
    for (int i = 0; i < v.size(); i++)
      node[i + n - 1] = v[i];
    for (int i = n - 2; i >= 0; i--)
      node[i] = f(node[2 * i + 1], node[2 * i + 2]);
  }

  void update(int k, T val) {
    k += n - 1;
    node[k] = val;
    while (k > 0) {
      k = (k - 1) / 2;
      node[k] = f(node[2 * k + 1], node[2 * k + 2]);
    }
  }

  void add(int k, T val) {
    k += n - 1;
    node[k] += val;
    while (k > 0) {
      k = (k - 1) / 2;
      node[k] = f(node[2 * k + 1], node[2 * k + 2]);
    }
  }

  // 区間[a, b)の値を返す
  T operator[](int x) { return getval(x, x + 1); }
  T getval(int a, int b) { return getval(a, b, 0, 0, n); }
  T getval(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return inf;
    if (a <= l && r <= b)
      return node[k];
    T vl = getval(a, b, 2 * k + 1, l, (l + r) / 2);
    T vr = getval(a, b, 2 * k + 2, (l + r) / 2, r);
    return f(vl, vr);
  }

  void print(int n) {
    for (int i = 0; i < n; i++) {
      cout << getval(i, i + 1) << " ";
    }
    cout << endl;
  }
};

signed main() {

  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  cin >> a;

  SegmentTree<int> seg(vector<int>(n + 10, 0), -inf,
                       [&](int x, int y) { return max(x, y); });
  for (int i = 0; i < n; i++) {
    int l = max(0ll, a[i] - k);
    int r = min(n, a[i] + k);
    seg.update(a[i], seg.getval(l, r + 1) + 1);
  }
  int ans = seg.getval(0, n + 1);
  cout << ans << endl;

  return 0;
}