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

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>

#define LL long long
#define VI vector<int>
#define VL vector<long long>
#define FOR(i, a, b) for (int i = (a); i < ((int)b); ++i)
#define RFOR(i, a) for (int i = (a); i >= 0; --i)
#define FOE(i, a) for (auto i : a)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define DUMP(x) cerr << #x << " = " << (x) << endl;
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end())
#define BIT(n) (1LL << (n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
#define EPS 1e-14

using namespace std;

class BinaryIndexedTree {
public:
  unsigned int n = 0; // (a.size() - 1)
  vector<long long> a;

  BinaryIndexedTree(unsigned int n) : n(n) {
    this->a = vector<long long>(n + 1, 0);
  }

  // sum[0, i)
  long long sum(int i) {
    assert(0 <= i && i <= this->n);

    long long s = 0;
    i -= 1;
    while (i >= 0) {
      s += this->a[i];
      i = (i & (i + 1)) - 1;
    }
    return s;
  }

  long long sum(int left, int right) {
    assert(left <= right);
    return this->sum(right) - this->sum(left);
  }

  // a[i] += x(0-origin)
  void add(int i, int x) {
    assert(0 <= i && i < this->n);

    while (i < this->n) {
      this->a[i] += x;
      i |= i + 1;
    }
  }
};

int main(int argc, char *argv[]) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, q;
  cin >> n >> q;

  BinaryIndexedTree bit(n);
  FOR(i, 0, q) {
    int com, x, y;
    cin >> com >> x >> y;
    if (com == 0) {
      bit.add(x - 1, y);
    } else {
      cout << bit.sum(x - 1, y) << endl;
    }
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>

#define LL long long
#define VI vector<int>
#define VL vector<long long>
#define FOR(i, a, b) for (int i = (a); i < ((int)b); ++i)
#define RFOR(i, a) for (int i = (a); i >= 0; --i)
#define FOE(i, a) for (auto i : a)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define DUMP(x) cerr << #x << " = " << (x) << endl;
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end())
#define BIT(n) (1LL << (n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
#define EPS 1e-14

using namespace std;

class BinaryIndexedTree {
public:
  unsigned int n = 0;
  vector<long long> a;

  BinaryIndexedTree(unsigned int n) : n(n) {
    this->a = vector<long long>(n, 0);
  }

  // sum[0, i)
  long long sum(int i) {
    assert(0 <= i && i < this->n);

    long long s = 0;
    i -= 1;
    while (i >= 0) {
      s += this->a[i];
      i = (i & (i + 1)) - 1;
    }
    return s;
  }

  // a[i] += x(0-origin)
  void add(int i, int x) {
    assert(0 <= i && i < this->n);

    while (i < this->n) {
      this->a[i] += x;
      i |= i + 1;
    }
  }
};

int main(int argc, char *argv[]) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, q;
  cin >> n >> q;

  BinaryIndexedTree bit(n);
  FOR(i, 0, q) {
    int com, x, y;
    cin >> com >> x >> y;
    if (com == 0) {
      bit.add(x - 1, y);
    } else {
      cout << bit.sum(y) - bit.sum(x - 1) << endl;
    }
  }

  return 0;
}