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

#include <algorithm>
#include <climits>
#include <iostream>
#include <vector>
using namespace std;

template <typename T> class Segment_Tree {
private:
  int n;
  std::vector<T> SegTree;

public:
  Segment_Tree(int n_) {
    n = 1;
    while (n < n_)
      n *= 2;
    SegTree = std::vector<T>(2 * n - 1, INT_MAX);
    for (int i = 0; i < 2 * n - 1; i++)
      SegTree[i] = INT_MAX;
  }

  void update(int k, T a) {
    k += n - 1; // ??????index
    SegTree[k] = a;
    while (k > 0) { // ????????§?????????????????´??°
      k = (k - 1) / 2;
      SegTree[k] =
          min(SegTree[k * 2 + 1], SegTree[k * 2 + 2]); // ?????????????°????
    }
  }

  T range_query(int a, int b) { // [a,b)?????????????§£
    return query(a, b, 0, 0, n);
  }

  // [a,b)???????°????????±?????????¢??°
  // k:?????????index???l,r:????????????????????????????????????[l,r)
  // ?????£?????????????????? query(a,b,0,0,n) ????????¶
  T query(int a, int b, int k, int l, int r) {
    if (r <= a or b <= l)
      return INT_MAX; // ?????????????????§??????
    if (a <= l and r <= b)
      return SegTree[k]; // [a,b)???[l,r)????????¨???????????????????????´???

    T vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    T vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
    return min(vl, vr);
  }

  void dump() {
    cout << "Tree : ";
    for (int i = 0; i < n; i++)
      cout << SegTree[i] << " ";
    cout << endl;
  }
};

int main() {

  int n, q;
  cin >> n >> q;
  Segment_Tree<long long> seg(n);

  for (int i = 0; i < q; i++) {
    long long c, x, y;
    cin >> c >> x >> y;
    if (c == 0)
      seg.update((int)x, y);
    else
      cout << seg.range_query((int)x, (int)y + 1) << endl;
  }
}

Error: -6
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)


Fixed submission:

#include <algorithm>
#include <climits>
#include <iostream>
#include <vector>
using namespace std;

template <typename T> class Segment_Tree {
private:
  int n;
  std::vector<T> SegTree;

public:
  Segment_Tree(int n_) {
    n = 1;
    while (n < n_)
      n *= 2;
    SegTree = std::vector<T>(n, INT_MAX);
    for (int i = 0; i < 2 * n - 1; i++)
      SegTree[i] = INT_MAX;
  }

  void update(int k, T a) {
    k += n - 1; // ??????index
    SegTree[k] = a;
    while (k > 0) { // ????????§?????????????????´??°
      k = (k - 1) / 2;
      SegTree[k] =
          min(SegTree[k * 2 + 1], SegTree[k * 2 + 2]); // ?????????????°????
    }
  }

  T range_query(int a, int b) { // [a,b)?????????????§£
    return query(a, b, 0, 0, n);
  }

  // [a,b)???????°????????±?????????¢??°
  // k:?????????index???l,r:????????????????????????????????????[l,r)
  // ?????£?????????????????? query(a,b,0,0,n) ????????¶
  T query(int a, int b, int k, int l, int r) {
    if (r <= a or b <= l)
      return INT_MAX; // ?????????????????§??????
    if (a <= l and r <= b)
      return SegTree[k]; // [a,b)???[l,r)????????¨???????????????????????´???

    T vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    T vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
    return min(vl, vr);
  }

  void dump() {
    cout << "Tree : ";
    for (int i = 0; i < n; i++)
      cout << SegTree[i] << " ";
    cout << endl;
  }
};

int main() {

  int n, q;
  cin >> n >> q;
  Segment_Tree<long long> seg(n);

  for (int i = 0; i < q; i++) {
    long long c, x, y;
    cin >> c >> x >> y;
    if (c == 0)
      seg.update((int)x, y);
    else
      cout << seg.range_query((int)x, (int)y + 1) << endl;
    //	seg.dump();
  }
}