alexjercan/bugnet huggingface dataset:
Problem p02346 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
typedef long long ll;

int main(void) {
  ll i, j, k, n, q, c, x, y;
  scanf("%lld%lld", &n, &q);
  ll seg[n * 4], f = 1;
  while (f < n)
    f *= 2;
  for (i = 0; i < f * 2; ++i)
    seg[i] = 0;
  for (i = 0; i < q; ++i) {
    scanf("%lld%lld%lld", &c, &x, &y);
    x--;
    if (c) {
      y--;
      ll ans = 0;
      ll r = x + f;
      while (1) {
        ll las, ra = 1;
        while (!(r % 2))
          r /= 2;
        while (ra <= r)
          ra *= 2;
        ra /= 2;
        las = f / ra * (r + 1 - ra) - 1;
        // printf("f%lld r%lld ra%lld\\n", f, r, ra);
        while (las > y) {
          r *= 2;
          ra *= 2;
          las = f / ra * (r + 1 - ra) - 1;
          // printf("f%lld r%lld ra%lld\\n", f, r, ra);
        }
        ans += seg[r];
        if (las == y)
          break;
        r++;
        // printf("f%lld r%lld ra%lld\\n", f, r, ra);
      }
      printf("%lld\\n", ans);
    } else {
      int now = (x + f) / 2;
      seg[x + f] += y;
      while (now) {
        seg[now] = seg[now * 2] + seg[now * 2 + 1];
        now /= 2;
      }
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <stdio.h>
typedef long long ll;

int main(void) {
  ll i, j, k, n, q, c, x, y;
  scanf("%lld%lld", &n, &q);
  ll seg[n * 4], f = 1;
  while (f < n)
    f *= 2;
  for (i = 0; i < f * 2; ++i)
    seg[i] = 0;
  for (i = 0; i < q; ++i) {
    scanf("%lld%lld%lld", &c, &x, &y);
    if (c) {
      ll ans = 0;
      ll r = x + f;
      while (1) {
        ll las, ra = 1;
        while (!(r % 2))
          r /= 2;
        while (ra <= r)
          ra *= 2;
        ra /= 2;
        las = f / ra * (r + 1 - ra) - 1;
        // printf("f%lld r%lld ra%ld l%lld\\n", f, r, ra, l);
        while (las > y) {
          r *= 2;
          ra *= 2;
          las = f / ra * (r + 1 - ra) - 1;
          // printf("f%lld r%lld ra%ld l%lld\\n", f, r, ra, l);
        }
        ans += seg[r];
        if (las == y)
          break;
        r++;
        // printf("f%lld r%lld ra%ld l%lld\\n", f, r, ra, l);
      }
      printf("%lld\\n", ans);
    } else {
      int now = (x + f) / 2;
      seg[x + f] += y;
      while (now) {
        seg[now] = seg[now * 2] + seg[now * 2 + 1];
        now /= 2;
      }
    }
  }
  return 0;
}
Problem p02346 (C++) — buggy submission, verdict: Runtime Error

#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#include <algorithm>
#include <complex>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>

typedef long long ll;
typedef unsigned long long ull;

#define FOR(i, n) for (int i = 0; i < (n); i++)
#define ten(x) ((int)1e##x)

using namespace std;

int main() {
  int n;
  cin >> n;
  int q;
  cin >> q;
  vector<int> A(n, 0);
  int interval = sqrt(n);
  vector<int> B(n / interval + 1, 0);

  FOR(i, q) {
    int com, x, y;
    cin >> com >> x >> y;
    if (com == 0) {
      x--;
      int s = x / interval;
      B[s] -= A[x];
      A[x] += y;
      B[s] += A[x];
    }
    if (com == 1) {
      x--;
      y--;
      int j = x;
      int sum = 0;
      // cout << "fin:"<< x << " " << y << endl;
      // FOR(ii,B.size()) cout << B[ii] << " ";
      // cout << endl;
      // FOR(ii,A.size()) cout << A[ii] << " ";
      // cout << endl;
      while (j <= y) {
        // cout << j << " ";
        if (j % interval == 0 && y - j >= interval - 1) {
          sum += B[j / interval];
          j += interval;
        } else {
          sum += A[j];
          j++;
        }
      }
      // cout << "ans:" << tmp << endl << endl;
      cout << sum << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#include <algorithm>
#include <complex>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>

typedef long long ll;
typedef unsigned long long ull;

#define FOR(i, n) for (int i = 0; i < (n); i++)
#define ten(x) ((int)1e##x)

using namespace std;

int main() {
  int n;
  cin >> n;
  int q;
  cin >> q;
  vector<int> A(n, 0);
  int interval = sqrt(n);
  vector<int> B(n / interval + 1, 0);

  FOR(i, q) {
    int com, x, y;
    cin >> com >> x >> y;
    if (com == 0) {
      int s = x / interval;
      B[s] -= A[x];
      A[x] += y;
      B[s] += A[x];
    }
    if (com == 1) {
      int j = x;
      int sum = 0;
      // cout << "fin:"<< x << " " << y << endl;
      // FOR(ii,B.size()) cout << B[ii] << " ";
      // cout << endl;
      // FOR(ii,A.size()) cout << A[ii] << " ";
      // cout << endl;
      while (j <= y) {
        // cout << j << " ";
        if (j % interval == 0 && y - j >= interval - 1) {
          sum += B[j / interval];
          j += interval;
        } else {
          sum += A[j];
          j++;
        }
      }
      // cout << "ans:" << tmp << endl << endl;
      cout << sum << endl;
    }
  }
  return 0;
}
Problem p02346 (C++) — buggy submission, verdict: Time Limit Exceeded

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

class RangeSumQuery {
private:
  vector<int> value;
  int _size;

public:
  void Init(int size) {
    int bitcount = 0; //?????°
    int t = size;

    while (t != 0) {
      t /= 2;
      ++bitcount;
    }
    if (size % (1 << (bitcount - 1)) != 0)
      bitcount++;
    _size = (1 << bitcount) - 1;

    value.resize(_size);

    for (vector<int>::iterator it = value.begin(); it != value.end(); ++it) {
      (*it) = 0;
    }
  }

  void Update(int index, int v) {
    int n = index + (_size >> 1);
    value[n] += v;

    while (n > 0) {
      n = (n - 1) / 2;
      value[n] = value[n * 2 + 1] + value[n * 2 + 2];
    }
  }

  int _RunQuery(int s, int t, int k, int l, int r) {
    if (s <= l && r <= t) {
      return value[k];
    }
    if (r < s || t < l) {
      return 0;
    }
    return _RunQuery(s, t, 2 * k + 1, l, (l + r) / 2) +
           _RunQuery(s, t, 2 * k + 2, (l + r) / 2 + 1, r);
  }

  int RunQuery(int s, int t) { return _RunQuery(s, t, 0, 0, (_size >> 1)); }
};

int main() {
  int n, q;
  int com, x, y;
  RangeSumQuery rsq;

  cin >> n >> q;
  rsq.Init(n);

  for (int i = 0; i < q; ++i) {
    cin >> com >> x >> y;

    if (com == 0)
      rsq.Update(x - 1, y);
    else
      cout << rsq.RunQuery(x - 1, y - 1) << endl;
  }
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <numeric>
using namespace std;

int main() {
  int n, q, a[100000] = {}, i, com, x, y;

  scanf("%d %d", &n, &q);

  for (i = q; i--;) {
    scanf("%d %d %d", &com, &x, &y);

    if (com) {
      printf("%d\\n", accumulate(a + x - 1, a + y, 0));
    } else {
      *(a + x - 1) += y;
    }
  }
  return 0;
}
Problem p02346 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
using namespace std;
#define MAX (1 << 17)
int n, seg[MAX * 2 - 1];
void init(int size) {
  n = 1;
  while (n < size)
    n *= 2;
}
int query(int a, int b, int k, int l, int r) {
  if (r <= a || b <= l)
    return 0;
  if (a <= l && r <= b)
    return seg[k];
  int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
  int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
  return vl + vr;
}
void update(int i, int x) {
  i += n - 1;
  seg[i] += x;
  while (i > 0) {
    i = (i - 1) / 2;
    seg[i] = seg[2 * i + 1] + seg[2 * i + 2];
  }
}
int main() {
  int size, q;
  scanf("%d %d", &size, &q);
  init(size);
  while (q--) {
    int com, x, y;
    scanf("%d %d %d", &com, &x, &y);
    if (com == 0)
      update(x - 1, y);
    else
      printf("%d\\n", query(x - 1, y, 0, 0, n));
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
using namespace std;
#define MAX 100001
int n, seg[MAX * 2 - 1];
void init(int size) {
  n = 1;
  while (n < size)
    n *= 2;
}
int query(int a, int b, int k, int l, int r) {
  if (r <= a || b <= l)
    return 0;
  if (a <= l && r <= b)
    return seg[k];
  int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
  int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
  return vl + vr;
}
void update(int i, int x) {
  i += n - 1;
  seg[i] += x;
  while (i > 0) {
    i = (i - 1) / 2;
    seg[i] = seg[2 * i + 1] + seg[2 * i + 2];
  }
}
int main() {
  int size, q;
  scanf("%d %d", &size, &q);
  init(size);
  while (q--) {
    int com, x, y;
    scanf("%d %d %d", &com, &x, &y);
    if (com == 0)
      update(x - 1, y);
    else
      printf("%d\\n", query(x - 1, y, 0, 0, n));
  }
}
Problem p02346 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

struct SegTree {
  vector<int> node;
  int n;
  SegTree(int sz) {
    n = 1;
    while (sz > n)
      n *= 2;
    node.resize(2 * n - 1, 0);
  }
  void add(int p, int x) {
    p += n - 1;
    node[p] += x;
    do {
      p = (p - 1) / 2;
      node[p] += x;
    } while (p);
  }
  int get(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0)
      r = n;
    if (b <= l || r <= a)
      return 0;
    if (a <= l && r <= b) {
      return node[k];
    }
    return get(a, b, k * 2 + 1, l, (l + r) / 2) +
           get(a, b, k * 2 + 2, (r + l) / 2, r);
  }
};

int main() {
  int n, q;
  cin >> n >> q;
  SegTree s(n);
  if (n == 1) {
    int s = 0;
    while (q--) {
      int c, x, y;
      cin >> c >> x >> y;
      if (c == 0) {
        s += y;
      } else {
        cout << s << endl;
      }
    }
    return 0;
  }
  while (q--) {
    int c, x, y;
    cin >> c >> x >> y;
    if (c == 0) {
      s.add(x - 1, y);
    } else {
      cout << s.get(x - 1, y) << endl;
    }
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

struct SegTree {
  vector<int> node;
  int n;
  SegTree(int sz) {
    n = 1;
    while (sz > n)
      n *= 2;
    node.resize(2 * n - 1, 0);
  }
  void add(int p, int x) {
    p += n - 1;
    node[p] += x;
    do {
      p = (p - 1) / 2;
      node[p] += x;
    } while (p);
  }
  int get(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0)
      r = n;
    if (b <= l || r <= a)
      return 0;
    if (a <= l && r <= b) {
      return node[k];
    }
    return get(a, b, k * 2 + 1, l, (l + r) / 2) +
           get(a, b, k * 2 + 2, (r + l) / 2, r);
  }
};

int main() {
  int n, q;
  cin >> n >> q;
  SegTree s(n);
  if (n == 1) {
    int s = 0;
    while (q--) {
      int c, x, y;
      cin >> c >> x >> y;
      if (c == 0) {
        s += y;
      } else {
        cout << s << endl;
      }
    }
  }
  while (q--) {
    int c, x, y;
    cin >> c >> x >> y;
    if (c == 0) {
      s.add(x - 1, y);
    } else {
      cout << s.get(x - 1, y) << endl;
    }
  }
}
Problem p02346 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

struct SegTree {
  vector<int> node;
  int n;
  SegTree(int sz) {
    n = 1;
    while (sz > n)
      n *= 2;
    node.resize(2 * n - 1, 0);
  }
  void add(int k, int val) {
    k += (n - 1);
    node[k] += val;

    while (k > 0) {
      k = (k - 1) / 2;
      node[k] = node[2 * k + 1] + node[2 * k + 2];
    }
  } /*
   void add(int p, int x) {
       p += n-1;
       do {
           node[p] += x;
           p = (p-1) / 2;
       } while (p);
   }*/
  int get(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0)
      r = n;
    if (b <= l || r <= a)
      return 0;
    if (a <= l && r <= b) {
      return node[k];
    }
    return get(a, b, k * 2 + 1, l, (l + r) / 2) +
           get(a, b, k * 2 + 2, (r + l) / 2, r);
  }
};

int main() {
  int n, q;
  cin >> n >> q;
  SegTree s(n);
  while (q--) {
    int c, x, y;
    cin >> c >> x >> y;
    if (c == 0) {
      s.add(x - 1, y);
    } else {
      cout << s.get(x - 1, y) << endl;
    }
  }
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

struct SegTree {
  vector<int> node;
  int n;
  SegTree(int sz) {
    n = 1;
    while (sz > n)
      n *= 2;
    node.resize(2 * n - 1, 0);
  }
  void add(int p, int x) {
    p += n - 1;
    do {
      node[p] += x;
      p /= 2;
    } while (p);
  }
  int get(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0)
      r = n;
    if (b < l || r < a)
      return 0;
    if (a <= l && r <= b) {
      return node[k];
    }
    return get(a, b, k * 2 + 1, l, (l + r) / 2) +
           get(a, b, k * 2 + 2, (r + l) / 2, r);
  }
};

int main() {
  int n, q;
  cin >> n >> q;
  SegTree s(n);
  while (q--) {
    int c, x, y;
    cin >> c >> x >> y;
    if (c == 0) {
      s.add(x - 1, y);
    } else {
      cout << s.get(x - 1, y - 1) << endl;
    }
  }
}
Problem p02346 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef long long ll;

const int MAX_N = 100001;

ll st[1 << 18];

void init(int n) {
  int nn = 1;
  while (nn < n)
    nn *= 2;
  fill(st, st + 2 * nn - 1, 0);
}

void add(int i, ll x, int k, int l, int r) {
  if (i < l || r <= i)
    return;
  if (r - l == 1)
    st[k] += x;
  else {
    int chl = 2 * k + 1;
    int chr = 2 * k + 2;
    add(i, x, chl, l, (l + r) / 2);
    add(i, x, chr, (l + r) / 2, r);
    st[k] = st[chl] + st[chr];
  }
}

ll get_sum(int a, int b, int k, int l, int r) {
  if (r <= a || b <= l)
    return 0LL;
  if (a <= l && r <= b)
    return st[k];
  ll res_l = get_sum(a, b, 2 * k + 1, l, (l + r) / 2);
  ll res_r = get_sum(a, b, 2 * k + 2, (l + r) / 2, r);
  return res_l + res_r;
}

int main() {
  int n, q;
  scanf("%d%d", &n, &q);
  init(++n);
  for (int _ = 0; _ < q; ++_) {
    int c, x, y;
    scanf("%d%d%d", &c, &x, &y);
    if (c == 0) {
      add(x, y, 0, 0, n);
    } else {
      ll ret = get_sum(x, y + 1, 0, 0, n);
      printf("%lld\\n", ret);
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

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

typedef long long ll;

const int MAX_N = 100000;

ll st[2 * MAX_N];

void init(int n) {
  int nn = 1;
  while (nn < n)
    n *= 2;
  fill(st, st + 2 * nn - 1, 0);
}

void add(int i, ll a, int k, int l, int r) {
  if (i < l || r <= i)
    return;
  if (r - l == 1)
    st[k] += a;
  else {
    int chl = 2 * k + 1;
    int chr = 2 * k + 2;
    add(i, a, chl, l, (l + r) / 2);
    add(i, a, chr, (l + r) / 2, r);
    st[k] = st[chl] + st[chr];
  }
}

ll get_sum(int a, int b, int k, int l, int r) {
  if (r <= a || b <= l)
    return 0;
  if (a <= l && r <= b)
    return st[k];
  ll res_l = get_sum(a, b, 2 * k + 1, l, (l + r) / 2);
  ll res_r = get_sum(a, b, 2 * k + 2, (l + r) / 2, r);
  return res_l + res_r;
}

int main() {
  int n, q;
  scanf("%d%d", &n, &q);
  init(n);
  for (int _ = 0; _ < q; ++_) {
    int c, x, y;
    scanf("%d%d%d", &c, &x, &y);
    if (c == 0) {
      add(x, y, 0, 0, n);
    } else {
      ll ret = get_sum(x, y + 1, 0, 0, n);
      printf("%lld\\n", ret);
    }
  }
  return 0;
}
Problem p02346 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

struct SegTree {
public:
  int n;
  const int MAX = 100000;

  vector<int> tree;

  SegTree(int n) {
    tree = vector<int>(MAX * 4, 0);

    SegTree::n = 1;
    while (SegTree::n < n)
      SegTree::n *= 2;
  }

  void update(int i, int x) {
    i += (n - 1);
    tree[i] += x;
    while (i > 0) {
      i = (i - 1) / 2;
      tree[i] += x;
    }
  }

  int find(int s, int t) { return find_sub(s, t, 0, 0, n); }

private:
  int find_sub(int s, int t, int k, int l, int r) {
    if (r <= s || t <= l)
      return 0;

    if (s <= l && r <= t)
      return tree[k];

    int u, v;
    u = find_sub(s, t, k * 2 + 1, l, (l + r) / 2);
    v = find_sub(s, t, k * 2 + 2, (l + r) / 2, r);
    return u + v;
  }
};

int main() {
  int n, q;
  cin >> n >> q;
  SegTree st(n);

  for (int i = 0; i < q; i++) {
    int com, x, y;
    cin >> com >> x >> y;
    if (com == 0) {
      st.update(x - 1, y);
    } else {
      int ret = st.find(x - 1, y);
      cout << ret << endl;
    }
  }

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

struct SegTree {
public:
  int n;
  const int MAX = 100000;

  vector<int> tree;

  SegTree(int n) {
    tree = vector<int>(MAX, 0);

    SegTree::n = 1;
    while (SegTree::n < n)
      SegTree::n *= 2;
  }

  void update(int i, int x) {
    i += (n - 1);
    tree[i] += x;
    while (i > 0) {
      i = (i - 1) / 2;
      tree[i] += x;
    }
  }

  int find(int s, int t) { return find_sub(s, t, 0, 0, n); }

private:
  int find_sub(int s, int t, int k, int l, int r) {
    if (r <= s || t <= l)
      return 0;

    if (s <= l && r <= t)
      return tree[k];

    int u, v;
    u = find_sub(s, t, k * 2 + 1, l, (l + r) / 2);
    v = find_sub(s, t, k * 2 + 2, (l + r) / 2, r);
    return u + v;
  }
};

int main() {
  int n, q;
  cin >> n >> q;
  SegTree st(n);

  for (int i = 0; i < q; i++) {
    int com, x, y;
    cin >> com >> x >> y;
    if (com == 0) {
      st.update(x - 1, y);
    } else {
      int ret = st.find(x - 1, y);
      cout << ret << endl;
    }
  }

  return 0;
}
Problem p02346 (C++) — buggy submission, verdict: Runtime Error

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

template <typename T, int MAX_N> struct segment_tree_sum {
  T arr[MAX_N << 1];
  int N;

  segment_tree_sum(int n) {
    N = 1;
    while (N < n)
      N *= 2;

    for (int i = 0; i < 2 * N - 1; ++i) {
      arr[i] = (T)0;
    }
  }

  void add(int i, T v) {
    int j = i + N - 1;
    arr[j] += v;
    while (j > 0) {
      j = (j - 1) / 2;
      arr[j] += v;
    }
  }

  T query(int a, int b) { return query(a, b, 0, 0, N); }

  T query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return 0;
    if (a <= l && r <= b)
      return arr[k];

    int m = (l + r) / 2;
    return query(a, b, 2 * k + 1, l, m) + query(a, b, 2 * k + 2, m, r);
  }
};

int N, Q;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N >> Q;
  segment_tree_sum<int, 201010> segt(N + 1);
  for (int j = 0; j < Q; ++j) {
    int c, x, y;
    cin >> c >> x >> y;
    if (c == 0) {
      segt.add(x, y);
    } else {
      cout << segt.query(x, y + 1) << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

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

template <typename T, int MAX_N> struct segment_tree_sum {
  T arr[MAX_N << 1];
  int N;

  segment_tree_sum(int n) {
    N = 1;
    while (N < n)
      N *= 2;

    for (int i = 0; i < 2 * N - 1; ++i) {
      arr[i] = (T)0;
    }
  }

  void add(int i, T v) {
    int j = i + N - 1;
    arr[j] += v;
    while (j > 0) {
      j = (j - 1) / 2;
      arr[j] += v;
    }
  }

  T query(int a, int b) { return query(a, b, 0, 0, N); }

  T query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return 0;
    if (a <= l && r <= b)
      return arr[k];

    int m = (l + r) / 2;
    return query(a, b, 2 * k + 1, l, m) + query(a, b, 2 * k + 2, m, r);
  }
};

int N, Q;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N >> Q;
  segment_tree_sum<int, 101010> segt(N + 1);
  for (int j = 0; j < Q; ++j) {
    int c, x, y;
    cin >> c >> x >> y;
    if (c == 0) {
      segt.add(x, y);
    } else {
      cout << segt.query(x, y + 1) << endl;
    }
  }
  return 0;
}
Problem p02346 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>

#define MAX 100000

using namespace std;

vector<int> S(MAX * 4, 0);

int N;
void update(int i, int x) {
  i += (N - 1);
  S[i] += x;

  while (i > 0) {
    i = (i - 1) / 2;
    S[i] += x;
  }
}

int find(int s, int t, int k, int l, int r) {
  if (r <= s || t <= l)
    return 0;

  if (s <= l && r <= t)
    return S[k];

  int u, v;
  u = find(s, t, k * 2 + 1, l, (l + r) / 2);
  v = find(s, t, k * 2 + 2, (l + r) / 2, r);
  return u + v;
}

int main() {
  int n, q;
  cin >> n >> q;
  N = 1;
  while (N < n)
    N *= 2;

  for (int i = 0; i < q; i++) {
    int com, x, y;
    cin >> com >> x >> y;
    if (com == 0) {
      update(x - 1, y);
    } else {
      int ret = find(x - 1, y, 0, 0, N);
      cout << ret << endl;
    }
  }

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

#define MAX 100000

using namespace std;

vector<int> S(MAX, 0);

int N;
void update(int i, int x) {
  i += (N - 1);
  S[i] += x;

  while (i > 0) {
    i = (i - 1) / 2;
    S[i] += x;
  }
}

int find(int s, int t, int k, int l, int r) {
  if (r <= s || t <= l)
    return 0;

  if (s <= l && r <= t)
    return S[k];

  int u, v;
  u = find(s, t, k * 2 + 1, l, (l + r) / 2);
  v = find(s, t, k * 2 + 2, (l + r) / 2, r);
  return u + v;
}

int main() {
  int n, q;
  cin >> n >> q;
  N = 1;
  while (N < n)
    N *= 2;

  for (int i = 0; i < q; i++) {
    int com, x, y;
    cin >> com >> x >> y;
    if (com == 0) {
      update(x - 1, y);
    } else {
      int ret = find(x - 1, y, 0, 0, N);
      cout << ret << endl;
    }
  }

  return 0;
}
Problem p02346 (C++) — buggy submission, verdict: Runtime Error


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

int bit[234567];
int siz;
void init(int n) {
  siz = 1;
  while (siz < n)
    siz *= 2;
  for (int i = 0; i <= siz; ++i)
    bit[i] = 0;
}
void add(int i, int x) {
  ++i;
  while (i <= siz) {
    bit[i] += x;
    i += (i & -i);
    // cerr << i << " " << bit[i] << endl;
  }
}
int sum(int i) {
  int ans = 0;
  while (i > 0) {
    ans += bit[i];
    i -= (i & -i);
  }
  return ans;
}

int main() {
  int n, q;
  cin >> n >> q;
  init(n);
  for (int i = 0; i < q; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    if (a == 0) {
      --b;
      add(b, c);
    } else {
      --b;
      --c;
      cout << sum(c + 1) - sum(b) << endl;
    }
  }
  return 0;
}

Error: 0
2 0
4 0
8 0
4 1
8 0
4 3
8 0


Fixed submission:


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

int bit[934567];
int siz;
void init(int n) {
  siz = 1;
  while (siz < n)
    siz *= 2;
  for (int i = 0; i <= siz; ++i)
    bit[i] = 0;
}
void add(int i, int x) {
  ++i;
  while (i <= siz) {
    bit[i] += x;
    i += (i & -i);
    cerr << i << " " << bit[i] << endl;
  }
}
int sum(int i) {
  int ans = 0;
  while (i > 0) {
    ans += bit[i];
    i -= (i & -i);
  }
  return ans;
}

int main() {
  int n, q;
  cin >> n >> q;
  init(n);
  for (int i = 0; i < q; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    if (a == 0) {
      --b;
      add(b, c);
    } else {
      --b;
      --c;
      cout << sum(c + 1) - sum(b) << endl;
    }
  }
  return 0;
}
Problem p02346 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
const int maxn = 100005;
int bit[maxn];
int n;

int get_sum(int i) {
  int ret = 0;
  while (i > 0) {
    ret += bit[i];
    i -= i & -i;
  }
  return ret;
}

void add(int i, int x) {
  while (i <= n) {
    bit[i] += x;
    i += i & -i;
  }
}

int main(void) {
  int op, q, a, b;
  string str;
  ios::sync_with_stdio(false);
  cin >> n >> q;
  while (q--) {
    cin >> op >> a >> b;
    if (op == 0)
      add(a, b);
    else
      cout << get_sum(b) - get_sum(a - 1) << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
const int maxn = 10005;
int bit[maxn];
int n;

int get_sum(int i) {
  int ret = 0;
  while (i > 0) {
    ret += bit[i];
    i -= i & -i;
  }
  return ret;
}

void add(int i, int x) {
  while (i <= n) {
    bit[i] += x;
    i += i & -i;
  }
}

int main(void) {
  int op, q, a, b;
  string str;
  ios::sync_with_stdio(false);
  cin >> n >> q;
  while (q--) {
    cin >> op >> a >> b;
    if (op == 0)
      add(a, b);
    else
      cout << get_sum(b) - get_sum(a - 1) << endl;
  }

  return 0;
}