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

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

// T : ????????????
// F : ?????????
// unit : f ?????????????????????
template <class T, class QueryFunc, class UpdateFunc, T unit>
class SegmentTree {
public:
  int n;
  vector<T> data;
  QueryFunc qf;
  UpdateFunc uf;
  SegmentTree(int nn) : n(1) {
    while (n < nn)
      n *= 2;
    data.resize(2 * n - 1, unit);
  }
  // index k in [0, n) ????????? val ?????´??°
  void update(int k, T val) {
    assert(0 <= k and k < n);
    k += n - 1;
    data[k] = uf(data[k], val);
    while (k > 0) {
      k = parent(k);
      data[k] = qf(data[left_child(k)], data[right_child(k)]);
    }
  }
  T query(int a) const { return query(a, a + 1); }
  // [a, b)
  T query(int a, int b) const {
    assert(0 <= a and a < b and b <= n);
    return query(a, b, 0, 0, n);
  }

private:
  //[a, b) ????????¨???, now???????????´???, [l,r)?????????????????´???
  T query(int a, int b, int now, int l, int r) const {
    if (out_of_range(a, b, l, r))
      return unit;
    if (within_range(a, b, l, r))
      return data[now];
    else { // cross range
      T vleft = query(a, b, left_child(now), l, (l + r) / 2);
      T vright = query(a, b, right_child(now), (l + r) / 2, r);
      return qf(vleft, vright);
    }
  }
  static bool out_of_range(int a, int b, int l, int r) {
    return r <= a or b <= l;
  }
  static bool within_range(int a, int b, int l, int r) {
    return a <= l and r <= b;
  }
  static int left_child(int i) { return i * 2 + 1; }
  static int right_child(int i) { return i * 2 + 2; }
  static int parent(int i) { return (i - 1) / 2; }
};

// -----------------------------------------------------------------------

template <class T> struct Min {
  T operator()(const T &x, const T &y) const { return min(x, y); }
};
template <class T> struct Max {
  T operator()(const T &x, const T &y) const { return max(x, y); }
};
template <class T> struct OverWrite {
  T operator()(const T &x, const T &y) const { return y; }
};

template <class T, T INF>
using RangeMinimumQuery = SegmentTree<T, Min<T>, OverWrite<T>, INF>;

template <class T, T MINF>
using RangeMaximumQuery = SegmentTree<T, Max<T>, OverWrite<T>, MINF>;

template <class T, T ZERO>
using RangeSumQuery =
    SegmentTree<T, plus<T>, plus<T>, ZERO>; // update ??? add ??¨??????????£?

// template<class T, T ZERO>
// using RangeSumQuery = SegmentTree<T, plus<T>, OverWrite<T>, ZERO>; // update
// ??? overwrite

template <class SegTree>
void debug_segment_tree(const SegTree &st, ostream &os = cerr) {
  os << "\\033[33m";
  os << endl;
  int n = 1, m = 1;
  for (int i = 0; i < st.data.size(); i++) {
    os << st.data[i];
    if (i + 1 == m) {
      os << endl;
      n *= 2;
      m += n;
    } else {
      os << " ";
    }
  }
  os << "\\033[39m";
}

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_2_A&lang=jp
int main() {
  int n, q;
  cin >> n >> q;
  RangeSumQuery<long long, 0> rsq(n);
  for (int i = 0; i < q; i++) {
    int com, x, y;
    cin >> com >> x >> y;
    if (com == 0)
      rsq.update(x - 1, y);
    else
      cout << rsq.query(x - 1, y - 1 + 1) << endl;
    // debug_segment_tree(rsq);
  }
  return 0;
}

Error: 0
[33m
1
1 0
1 0 0 0
[39m[33m
3
3 0
1 2 0 0
[39m[33m
6
3 3
1 2 3 0
[39m[33m
6
3 3
1 2 3 0
[39m[33m
6
3 3
1 2 3 0
[39m

Fixed submission:

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

// T : ????????????
// F : ?????????
// unit : f ?????????????????????
template <class T, class F, T unit> class SegmentTree {
public:
  int n;
  vector<T> data;
  F f;
  SegmentTree(int nn) : n(1) {
    while (n < nn)
      n *= 2;
    data.resize(2 * n - 1, unit);
  }
  // index k in [0, n) ????????? val ?????´??°
  void update(int k, T val) {
    assert(0 <= k and k < n);
    k += n - 1;
    data[k] = val;
    while (k > 0) {
      k = parent(k);
      data[k] = f(data[left_child(k)], data[right_child(k)]);
    }
  }
  T query(int a) const { return query(a, a + 1); }
  // [a, b)
  T query(int a, int b) const {
    assert(0 <= a and a < b and b <= n);
    return query(a, b, 0, 0, n);
  }

private:
  //[a, b) ????????¨???, now???????????´???, [l,r)?????????????????´???
  T query(int a, int b, int now, int l, int r) const {
    if (out_of_range(a, b, l, r))
      return unit;
    if (within_range(a, b, l, r))
      return data[now];
    else { // cross range
      T vleft = query(a, b, left_child(now), l, (l + r) / 2);
      T vright = query(a, b, right_child(now), (l + r) / 2, r);
      return f(vleft, vright);
    }
  }
  static bool out_of_range(int a, int b, int l, int r) {
    return r <= a or b <= l;
  }
  static bool within_range(int a, int b, int l, int r) {
    return a <= l and r <= b;
  }
  static int left_child(int i) { return i * 2 + 1; }
  static int right_child(int i) { return i * 2 + 2; }
  static int parent(int i) { return (i - 1) / 2; }
};

// -----------------------------------------------------------------------

template <class T> struct Min {
  T operator()(const T &x, const T &y) const { return min(x, y); }
};
template <class T> struct Max {
  T operator()(const T &x, const T &y) const { return max(x, y); }
};

template <class T, T INF> using RangeMinimumQuery = SegmentTree<T, Min<T>, INF>;

template <class T, T MINF>
using RangeMaximumQuery = SegmentTree<T, Max<T>, MINF>;

template <class T, T ZERO> using RangeSumQuery = SegmentTree<T, plus<T>, ZERO>;

template <class SegTree>
void debug_segment_tree(const SegTree &st, ostream &os = cerr) {
  os << "\\033[33m";
  os << endl;
  int n = 1, m = 1;
  for (int i = 0; i < st.data.size(); i++) {
    os << st.data[i];
    if (i + 1 == m) {
      os << endl;
      n *= 2;
      m += n;
    } else {
      os << " ";
    }
  }
  os << "\\033[39m";
}

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_2_A&lang=jp
int main() {
  int n, q;
  cin >> n >> q;
  RangeSumQuery<long long, 0> rsq(n);
  for (int i = 0; i < q; i++) {
    int com, x, y;
    cin >> com >> x >> y;
    if (com == 0)
      rsq.update(x - 1, y);
    else
      cout << rsq.query(x - 1, y - 1 + 1) << endl;
    debug_segment_tree(rsq);
  }
  return 0;
}