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

#include <bits/stdc++.h>

using namespace std;

namespace tt {

template <typename T> class SegmentTree {
public:
  using Op = function<T(const T &a, const T &b)>;

private:
  vector<T> data_;
  int size_;
  Op op_;
  T e_;

  T QuerySub(int left, int right, int k, int k_left, int k_right) {
    if (right <= k_left || k_right <= left) {
      return e_;
    } else if (left <= k_left && k_right <= right) {
      return data_[k];
    } else {
      int mid = (k_left + k_right) / 2;
      return op_(QuerySub(left, right, k * 2 + 1, k_left, mid),
                 QuerySub(left, right, k * 2 + 2, mid, k_right));
    }
  }

public:
  T operator[](int pos) { return data_[pos + size_ - 1]; }

  SegmentTree(int size, T e, Op op) : op_(op), e_(e) {
    size_ = 1;
    while (size_ < size) {
      size_ *= 2;
    }

    data_.resize(size_ * 2 - 1);
  };

  void Update(int pos, T val) {
    pos += size_ - 1;
    data_[pos] = val;
    while (pos > 0) {
      pos = (pos - 1) / 2;
      data_[pos] = op_(data_[pos * 2 + 1], data_[pos * 2 + 2]);
    }
  }

  T Query(int left, int right) { return QuerySub(left, right, 0, 0, size_); }
  T QueryAll() { return QuerySub(0, size_, 0, 0, size_); }
};

} // namespace tt

int main(int argc, const char *argv[]) {
  int n, k;
  cin >> n >> k;

  int max_el = 5 * pow(10, 5);
  tt::SegmentTree<int> seg_tree(
      max_el, 0, [](const int &a, const int &b) { return max(a, b); });

  for (int i = 0; i < n; ++i) {
    int a;
    cin >> a;

    int max_n = seg_tree.Query(max(0, a - k), min(max_el, a + k + 1));
    seg_tree.Update(a, max_n + 1);
  }

  cout << seg_tree.QueryAll() << '\\n';
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

namespace tt {

template <typename T> class SegmentTree {
public:
  using Op = function<T(const T &a, const T &b)>;

private:
  vector<T> data_;
  int size_;
  Op op_;
  T e_;

  T QuerySub(int left, int right, int k, int k_left, int k_right) {
    if (right <= k_left || k_right <= left) {
      return e_;
    } else if (left <= k_left && k_right <= right) {
      return data_[k];
    } else {
      int mid = (k_left + k_right) / 2;
      return op_(QuerySub(left, right, k * 2 + 1, k_left, mid),
                 QuerySub(left, right, k * 2 + 2, mid, k_right));
    }
  }

public:
  T operator[](int pos) { return data_[pos + size_ - 1]; }

  SegmentTree(int size, T e, Op op) : op_(op), e_(e) {
    size_ = 1;
    while (size_ < size) {
      size_ *= 2;
    }

    data_.resize(size_ * 2 - 1);
  };

  void Update(int pos, T val) {
    pos += size_ - 1;
    data_[pos] = val;
    while (pos > 0) {
      pos = (pos - 1) / 2;
      data_[pos] = op_(data_[pos * 2 + 1], data_[pos * 2 + 2]);
    }
  }

  T Query(int left, int right) { return QuerySub(left, right, 0, 0, size_); }
};

} // namespace tt

int solve(vector<int> &va, int i, int pre, int k) {
  if (i == va.size()) {
    return 0;
  }

  if (pre < 0 || abs(va[i] - pre) <= k) {
    return max(solve(va, i + 1, va[i], k) + 1, solve(va, i + 1, pre, k));
  } else {
    return solve(va, i + 1, pre, k);
  }
}

int main(int argc, const char *argv[]) {
  int n, k;
  cin >> n >> k;

  int max_el = 3 * pow(10, 5);
  tt::SegmentTree<int> seg_tree(
      n, 0, [](const int &a, const int &b) { return max(a, b); });

  for (int i = 0; i < n; ++i) {
    int a;
    cin >> a;

    int max_n = seg_tree.Query(max(0, a - k), min(max_el, a + k + 1));
    seg_tree.Update(a, max_n + 1);
  }

  cout << seg_tree.Query(0, n) << '\\n';
}