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

#include <bits/stdc++.h>
using namespace std;

template <typename T> struct SegmentTree { // 1-indexed
  T m_id;
  T op(const T &a, const T &b) const { return max(a, b); }
  vector<T> m_SegT;

  SegmentTree(int N) : m_id(0), m_SegT(2 * N, m_id) {}
  SegmentTree(const vector<T> &data) : m_id(0), m_SegT(2 * data.size()) {
    int N = m_SegT.size() >> 1;
    for (int i = 0; i < N; i++) {
      m_SegT.at(i + N) = data.at(i);
    }
    for (int i = N - 1; i > 0; i--) {
      m_SegT.at(i) = op(m_SegT.at(i << 1), m_SegT.at((i << 1) | 1));
    }
  }

  void set_val(int idx, T val) {
    idx += m_SegT.size() >> 1;
    m_SegT.at(idx) = val;
    while ((idx >> 1) > 0) {
      idx >>= 1;
      m_SegT.at(idx) = op(m_SegT.at(idx << 1), m_SegT.at((idx << 1) | 1));
    }
  }

  T range(int left, int right) const { // [left, right)
    left += m_SegT.size() >> 1;
    right += m_SegT.size() >> 1;
    T calc_l = m_id;
    T calc_r = m_id;

    while (left < right) {
      if (left & 1) {
        calc_l = op(calc_l, m_SegT.at(left));
        left++;
      }
      if (right & 1) {
        calc_r = op(m_SegT.at(right - 1), calc_r);
      }

      left >>= 1;
      right >>= 1;
    }

    return op(calc_l, calc_r);
  }
};

int main() {
  int N, K;
  cin >> N >> K;

  vector<int> data(N);
  for (int i = 0; i < N; i++) {
    cin >> data.at(i);
    data.at(i)++; // [0,N] -> [1,N+1]
  }

  constexpr int Vmax = 300'001; // '
  SegmentTree<int> st(Vmax + 1);
  for (int i = 0; i < N; i++) {
    int left = max(1, data.at(i) - K);
    int right = min(Vmax, data.at(i) + K);
    int len_prev = st.range(left, right + 1);
    st.set_val(data.at(i), len_prev + 1);
  }

  int ans = st.range(0, Vmax + 1);
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

template <typename T> struct SegmentTree { // 1-indexed
  T m_id;
  T op(const T &a, const T &b) const { return max(a, b); }
  vector<T> m_SegT;

  SegmentTree(int N) : m_id(0), m_SegT(2 * N, m_id) {}
  SegmentTree(const vector<T> &data) : m_id(0), m_SegT(2 * data.size()) {
    int N = m_SegT.size() >> 1;
    for (int i = 0; i < N; i++) {
      m_SegT.at(i + N) = data.at(i);
    }
    for (int i = N - 1; i > 0; i--) {
      m_SegT.at(i) = op(m_SegT.at(i << 1), m_SegT.at((i << 1) | 1));
    }
  }

  void set_val(int idx, T val) {
    idx += m_SegT.size() >> 1;
    m_SegT.at(idx) = val;
    while ((idx >> 1) > 0) {
      idx >>= 1;
      m_SegT.at(idx) = op(m_SegT.at(idx << 1), m_SegT.at((idx << 1) | 1));
    }
  }

  T range(int left, int right) const { // [left, right)
    left += m_SegT.size() >> 1;
    right += m_SegT.size() >> 1;
    T calc_l = m_id;
    T calc_r = m_id;

    while (left < right) {
      if (left & 1) {
        calc_l = op(calc_l, m_SegT.at(left));
        left++;
      }
      if (right & 1) {
        calc_r = op(m_SegT.at(right - 1), calc_r);
      }

      left >>= 1;
      right >>= 1;
    }

    return op(calc_l, calc_r);
  }
};

int main() {
  int N, K;
  cin >> N >> K;

  vector<int> data(N);
  for (int i = 0; i < N; i++) {
    cin >> data.at(i);
    data.at(i)++; // [0,N] -> [1,N+1]
  }

  const int Vmax = N + 1;
  SegmentTree<int> st(Vmax + 1);
  for (int i = 0; i < N; i++) {
    int left = max(1, data.at(i) - K);
    int right = min(Vmax, data.at(i) + K);
    int len_prev = st.range(left, right + 1);
    st.set_val(data.at(i), len_prev + 1);
  }

  int ans = st.range(0, Vmax + 1);
  cout << ans << endl;
}