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

#include <bits/stdc++.h>
using namespace std;
template <typename T> using vec = vector<T>;
using lint = long long;
using ulint = unsigned long long;

template <typename T> ostream &operator<<(ostream &os, vector<T> v) {
  os << "[";
  for (int i = 0; i < v.size() - 1; i++) {
    os << v.at(i) << ", ";
  }
  return os << v.at(v.size() - 1) << "]";
}

template <typename T, typename E> struct SegmentTree {
private:
  typedef function<T(T, T)> F;
  typedef function<T(T, E)> G;
  int n;
  F merge_func;
  G update_func;
  T identity_item;
  vector<T> nodes;

public:
  SegmentTree(int n_, F merge_func, G update_func, T identity_item,
              const vec<T> &v = vector<T>()) {
    this->merge_func = merge_func;
    this->update_func = update_func;
    this->identity_item = identity_item;
    nodes_init(n_);
    if (n_ == (int)v.size())
      build(n_, v);
  }

  void nodes_init(int n_) {
    n = 1;
    while (n <= n_)
      n *= 2;
    nodes.clear();
    nodes.resize(2 * n - 1, identity_item);
  }

  void build(int n_, const vector<T> &v) {
    for (int i = 0; i < n_; i++) {
      nodes.at(i + n - 1) = v.at(i);
    }
    for (int i = n - 2; i >= 0; i--) {
      nodes.at(i) = merge_func(nodes.at(i * 2 + 1), nodes.at(i * 2 + 2));
    }
  }

  void update(int idx, E value) {
    int node_idx = idx + n - 1;
    nodes.at(node_idx) = update_func(nodes.at(node_idx), value);
    int current_idx = node_idx;
    while (current_idx) {
      current_idx = (current_idx - 1) / 2;
      nodes.at(current_idx) = merge_func(nodes.at(2 * current_idx + 1),
                                         nodes.at(2 * current_idx + 2));
    }
  }

  T query(int query_left, int query_right, int cur_idx, int cur_left,
          int cur_right) {
    if (cur_right <= query_left || query_right <= cur_left)
      return identity_item;
    if (query_left <= cur_left && cur_right <= query_right)
      return nodes.at(cur_idx);
    T value_from_left = query(query_left, query_right, 2 * cur_idx + 1,
                              cur_left, (cur_left + cur_right) / 2);
    T value_from_right = query(query_left, query_right, 2 * cur_idx + 2,
                               (cur_left + cur_right) / 2, cur_right);
    return merge_func(value_from_left, value_from_right);
  }

  T query(int query_left, int query_right) {
    return query(query_left, query_right, 0, 0, n);
  }
};

int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  int n, q;
  cin >> n >> q;

  SegmentTree<lint, lint> segtree(
      n, [](lint a, lint b) { return a + b; },
      [](lint a, lint b) { return a + b; }, 0);

  for (int i = 0; i < q; i++) {
    int com, x, y;
    cin >> com >> x >> y;
    if (com == 0)
      segtree.update(x - 1, y);
    else {
      cout << segtree.query(x - 1, y) << "\\n";
    }
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
template <typename T> using vec = vector<T>;
using lint = long long;
using ulint = unsigned long long;

template <typename T> ostream &operator<<(ostream &os, vector<T> v) {
  os << "[";
  for (int i = 0; i < v.size() - 1; i++) {
    os << v.at(i) << ", ";
  }
  return os << v.at(v.size() - 1) << "]";
}

template <typename T, typename E> struct SegmentTree {
private:
  typedef function<T(T, T)> F;
  typedef function<T(T, E)> G;
  int n;
  F merge_func;
  G update_func;
  T identity_item;
  vector<T> nodes;

public:
  SegmentTree(int n_, F merge_func, G update_func, T identity_item,
              const vec<T> &v = vector<T>()) {
    this->merge_func = merge_func;
    this->update_func = update_func;
    this->identity_item = identity_item;
    nodes_init(n_);
    if (n_ == (int)v.size())
      build(n_, v);
  }

  void nodes_init(int n_) {
    n = 1;
    while (n < n_)
      n *= 2;
    nodes.clear();
    nodes.resize(2 * n - 1, identity_item);
  }

  void build(int n_, const vector<T> &v) {
    for (int i = 0; i < n_; i++) {
      nodes.at(i + n - 1) = v.at(i);
    }
    for (int i = n - 2; i >= 0; i--) {
      nodes.at(i) = merge_func(nodes.at(i * 2 + 1), nodes.at(i * 2 + 2));
    }
  }

  void update(int idx, E value) {
    int node_idx = idx + n - 1;
    nodes.at(node_idx) = update_func(nodes.at(node_idx), value);
    int current_idx = node_idx;
    while (current_idx) {
      current_idx = (current_idx - 1) / 2;
      nodes.at(current_idx) = merge_func(nodes.at(2 * current_idx + 1),
                                         nodes.at(2 * current_idx + 2));
    }
  }

  T query(int query_left, int query_right, int cur_idx, int cur_left,
          int cur_right) {
    if (cur_right <= query_left || query_right <= cur_left)
      return identity_item;
    if (query_left <= cur_left && cur_right <= query_right)
      return nodes.at(cur_idx);
    T value_from_left = query(query_left, query_right, 2 * cur_idx + 1,
                              cur_left, (cur_left + cur_right) / 2);
    T value_from_right = query(query_left, query_right, 2 * cur_idx + 2,
                               (cur_left + cur_right) / 2, cur_right);
    return merge_func(value_from_left, value_from_right);
  }

  T query(int query_left, int query_right) {
    return query(query_left, query_right, 0, 0, n);
  }
};

int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  int n, q;
  cin >> n >> q;

  SegmentTree<lint, lint> segtree(
      n, [](lint a, lint b) { return a + b; },
      [](lint a, lint b) { return a + b; }, 0);

  for (int i = 0; i < q; i++) {
    int com, x, y;
    cin >> com >> x >> y;
    if (com == 0)
      segtree.update(x, y);
    else {
      cout << segtree.query(x, y + 1) << "\\n";
    }
  }
}