prithivMLmods/Coder-Stat huggingface dataset:
Problem p02345 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

const int INF = 1e9;
const ll LINF = 1e18;

struct edge {
  int to, cost;
};

const int MAX_N = 1 << 17;

// const int F = 2147483647;//(int)(((ll)(2)<<31) -1);
const int F = (1LL << 31) - 1;

int n;
unsigned int dat[100000]; // セグメント木

void init(int n_) {
  n = 1;
  while (n < n_)
    n *= 2;

  for (int i = 0; i < 2 * n - 1; i++)
    dat[i] = F;
}

void update(int k, int a) {
  k += n - 1;
  dat[k] = a;
  while (k > 0) {
    k = (k - 1) / 2;
    dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]); // RMQ以外はここを変える？
  }
}

//[a,b)の最小値を求める
// kは接点の番号，l,rはその接点が[l,r)に対応していることを示す
// 呼ぶときはquery(a,b,0,0,n)
int query(int a, int b, int k, int l, int r) {
  if (r <= a || b <= l)
    return F;

  if (a <= l && r <= b)
    return dat[k];
  else {
    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 min(vl, vr);
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int m, q;
  cin >> m >> q;
  init(m);

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


Fixed submission:
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

const int INF = 1e9;
const ll LINF = 1e18;

struct edge {
  int to, cost;
};

const int MAX_N = 1 << 17;

// const int F = 2147483647;//(int)(((ll)(2)<<31) -1);
const int F = (1LL << 31) - 1;

int n;
unsigned int dat[1000000]; // セグメント木

void init(int n_) {
  n = 1;
  while (n < n_)
    n *= 2;

  for (int i = 0; i < 2 * n - 1; i++)
    dat[i] = F;
}

void update(int k, int a) {
  k += n - 1;
  dat[k] = a;
  while (k > 0) {
    k = (k - 1) / 2;
    dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]); // RMQ以外はここを変える？
  }
}

//[a,b)の最小値を求める
// kは接点の番号，l,rはその接点が[l,r)に対応していることを示す
// 呼ぶときはquery(a,b,0,0,n)
int query(int a, int b, int k, int l, int r) {
  if (r <= a || b <= l)
    return F;

  if (a <= l && r <= b)
    return dat[k];
  else {
    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 min(vl, vr);
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int m, q;
  cin >> m >> q;
  init(m);

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

struct node {
  int num, l, r, ls, rs;
  node(int _l, int _r) {
    num = INT_MAX;
    l = _l;
    r = _r;
  }
};

vector<node> T;

int make(int l, int r) {
  int id = T.size();
  T.push_back(node(l, r));
  T[id].ls = (l + 1 == r ? -1 : make(l, (l + r) / 2));
  T[id].rs = (l + 1 == r ? -1 : make((l + r) / 2, r));
  return id;
}

void update(int id, int x, int t) {
  if (T[id].l + 1 == T[id].r) {
    if (T[id].l == x)
      T[id].num = t;
    return;
  }
  int mid = (T[id].l + T[id].r) >> 1;
  if (x < mid)
    update(T[id].ls, x, t);
  else
    update(T[id].rs, x, t);
  T[id].num = min(T[T[id].ls].num, T[T[id].rs].num);
}

int getmin(int id, int l, int r) {
  if (l == r || T[id].l >= r || T[id].r <= l)
    return INT_MAX;
  if (T[id].l >= l && T[id].r <= r)
    return T[id].num;
  int mid = (T[id].l + T[id].r) >> 1, mi = INT_MAX;
  if (l < mid)
    mi = min(mi, getmin(T[id].ls, l, r));
  if (r > mid)
    mi = min(mi, getmin(T[id].rs, l, r));
  return mi;
}

int main() {
  freopen("in.txt", "r", stdin);
  T.reserve(200100);
  int n, q;
  scanf("%d%d", &n, &q);
  // printf("|\n");
  make(0, n);
  // printf("||\n");
  for (int i = 0; i < q; i++) {
    int com, x, y;
    scanf("%d%d%d", &com, &x, &y);
    if (com == 0)
      update(0, x, y);
    else
      printf("%d\n", getmin(0, x, y + 1));
  }
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

struct node {
  int num, l, r, ls, rs;
  node(int _l, int _r) {
    num = INT_MAX;
    l = _l;
    r = _r;
  }
};

vector<node> T;

int make(int l, int r) {
  int id = T.size();
  T.push_back(node(l, r));
  T[id].ls = (l + 1 == r ? -1 : make(l, (l + r) / 2));
  T[id].rs = (l + 1 == r ? -1 : make((l + r) / 2, r));
  return id;
}

void update(int id, int x, int t) {
  if (T[id].l + 1 == T[id].r) {
    if (T[id].l == x)
      T[id].num = t;
    return;
  }
  int mid = (T[id].l + T[id].r) >> 1;
  if (x < mid)
    update(T[id].ls, x, t);
  else
    update(T[id].rs, x, t);
  T[id].num = min(T[T[id].ls].num, T[T[id].rs].num);
}

int getmin(int id, int l, int r) {
  if (l == r || T[id].l >= r || T[id].r <= l)
    return INT_MAX;
  if (T[id].l >= l && T[id].r <= r)
    return T[id].num;
  int mid = (T[id].l + T[id].r) >> 1, mi = INT_MAX;
  if (l < mid)
    mi = min(mi, getmin(T[id].ls, l, r));
  if (r > mid)
    mi = min(mi, getmin(T[id].rs, l, r));
  return mi;
}

int main() {
  // freopen("in.txt","r",stdin);
  T.reserve(200100);
  int n, q;
  scanf("%d%d", &n, &q);
  // printf("|\n");
  make(0, n);
  // printf("||\n");
  for (int i = 0; i < q; i++) {
    int com, x, y;
    scanf("%d%d%d", &com, &x, &y);
    if (com == 0)
      update(0, x, y);
    else
      printf("%d\n", getmin(0, x, y + 1));
  }
}
Problem p02345 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#define INF 2147483647
using namespace std;

int n, q;
int com;
int tree[211111];

void init();
void update(int i, int x);
int segtree_find(int x, int y, int now, int left, int right);

int main() {

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

  init();

  for (int i = 0; i < q; i++) {
    scanf(" %d", &com);

    if (com) {
      int x, y;
      scanf(" %d %d", &x, &y);
      printf("%d\n", segtree_find(x, y, 0, 0, n - 1));
    } else {
      int x, y;
      scanf(" %d %d", &x, &y);
      update(x, y);
    }
  }

  return 0;
}

void init() {

  int m = n;
  n = 1;
  while (n < m)
    n = (n << 1);

  fill(tree, tree + n * 2, INF);

  return;
}

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

  while (i > 0) {
    i = (i - 1) / 2;
    tree[i] = min(tree[i * 2 + 1], tree[i * 2 + 2]);
  }

  return;
}

int segtree_find(int x, int y, int now, int left, int right) {

  if (right < x || y < left)
    return INF;

  if (x <= left && right <= y)
    return tree[now];

  if (now > n * 2)
    return -1;

  int find_left = segtree_find(x, y, (now * 2 + 1), left, ((right + left) / 2));
  int find_right =
      segtree_find(x, y, (now * 2 + 2), ((right + left) / 2 + 1), right);

  return min(find_left, find_right);
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#define INF 2147483647
using namespace std;

int n, q;
int com;
int tree[2111111];

void init();
void update(int i, int x);
int segtree_find(int x, int y, int now, int left, int right);

int main() {

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

  init();

  for (int i = 0; i < q; i++) {
    scanf(" %d", &com);

    if (com) {
      int x, y;
      scanf(" %d %d", &x, &y);
      printf("%d\n", segtree_find(x, y, 0, 0, n - 1));
    } else {
      int x, y;
      scanf(" %d %d", &x, &y);
      update(x, y);
    }
  }

  return 0;
}

void init() {

  int m = n;
  n = 1;
  while (n < m)
    n = (n << 1);

  fill(tree, tree + n * 2, INF);

  return;
}

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

  while (i > 0) {
    i = (i - 1) / 2;
    tree[i] = min(tree[i * 2 + 1], tree[i * 2 + 2]);
  }

  return;
}

int segtree_find(int x, int y, int now, int left, int right) {

  if (right < x || y < left)
    return INF;

  if (x <= left && right <= y)
    return tree[now];

  if (now > n * 2)
    return -1;

  int find_left = segtree_find(x, y, (now * 2 + 1), left, ((right + left) / 2));
  int find_right =
      segtree_find(x, y, (now * 2 + 2), ((right + left) / 2 + 1), right);

  return min(find_left, find_right);
}
Problem p02345 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#define INF 2147483647
using namespace std;

int n, q;
int com;
int tree[251111];

void init();
void update(int i, int x);
int segtree_find(int x, int y, int now, int left, int right);

int main() {

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

  init();

  for (int i = 0; i < q; i++) {
    scanf(" %d", &com);

    if (com) {
      int x, y;
      scanf(" %d %d", &x, &y);
      printf("%d\n", segtree_find(x, y, 0, 0, n - 1));
    } else {
      int x, y;
      scanf(" %d %d", &x, &y);
      update(x, y);
    }
  }

  return 0;
}

void init() {

  int m = n;
  n = 1;
  while (n < m)
    n = (n << 1);

  fill(tree, tree + n * 2, INF);

  return;
}

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

  while (i > 0) {
    i = (i - 1) / 2;
    tree[i] = min(tree[i * 2 + 1], tree[i * 2 + 2]);
  }

  return;
}

int segtree_find(int x, int y, int now, int left, int right) {

  if (right < x || y < left)
    return INF;

  if (x <= left && right <= y)
    return tree[now];

  if (now > n * 2)
    return -1;

  int find_left = segtree_find(x, y, (now * 2 + 1), left, ((right + left) / 2));
  int find_right =
      segtree_find(x, y, (now * 2 + 2), ((right + left) / 2 + 1), right);

  return min(find_left, find_right);
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#define INF 2147483647
using namespace std;

int n, q;
int com;
int tree[411111];

void init();
void update(int i, int x);
int segtree_find(int x, int y, int now, int left, int right);

int main() {

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

  init();

  for (int i = 0; i < q; i++) {
    scanf(" %d", &com);

    if (com) {
      int x, y;
      scanf(" %d %d", &x, &y);
      printf("%d\n", segtree_find(x, y, 0, 0, n - 1));
    } else {
      int x, y;
      scanf(" %d %d", &x, &y);
      update(x, y);
    }
  }

  return 0;
}

void init() {

  int m = n;
  n = 1;
  while (n < m)
    n = (n << 1);

  fill(tree, tree + n * 2, INF);

  return;
}

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

  while (i > 0) {
    i = (i - 1) / 2;
    tree[i] = min(tree[i * 2 + 1], tree[i * 2 + 2]);
  }

  return;
}

int segtree_find(int x, int y, int now, int left, int right) {

  if (right < x || y < left)
    return INF;

  if (x <= left && right <= y)
    return tree[now];

  if (now > n * 2)
    return -1;

  int find_left = segtree_find(x, y, (now * 2 + 1), left, ((right + left) / 2));
  int find_right =
      segtree_find(x, y, (now * 2 + 2), ((right + left) / 2 + 1), right);

  return min(find_left, find_right);
}
Problem p02345 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <limits.h>
using namespace std;

#define INF (2147483647)

int n, q;
int com;
int pos, num;
int b, e;

int tree[211111];

void init();
int find(int x, int y, int now, int left, int right);
void update(int i, int x);

int main() {

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

  init();

  for (int i = 0; i < q; i++) {
    scanf(" %d", &com);

    if (com) {
      scanf(" %d %d", &b, &e);
      printf("%d\n", find(b, e, 0, 0, n - 1));
    } else {
      scanf(" %d %d", &pos, &num);
      update(pos, num);
    }
  }
}

void init() {
  int m = n;
  n = 1;

  while (m > n)
    n = (n << 1);

  fill(tree, tree + n * 2, INF);
  return;
}

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

  while (i > 0) {
    i = (i - 1) / 2;
    tree[i] = min(tree[i * 2 + 1], tree[i * 2 + 2]);
  }
}

int find(int x, int y, int now, int left, int right) {
  if (right < x || y < left)
    return INF;

  if (x <= left && right <= y)
    return tree[now];

  int find_left = find(x, y, now * 2 + 1, left, (left + right) / 2);
  int find_right = find(x, y, now * 2 + 2, (left + right) / 2 + 1, right);

  return min(find_left, find_right);
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <limits.h>
using namespace std;

#define INF (2147483647)

int n, q;
int com;
int pos, num;
int b, e;

int tree[555555];

void init();
int find(int x, int y, int now, int left, int right);
void update(int i, int x);

int main() {

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

  init();

  for (int i = 0; i < q; i++) {
    scanf(" %d", &com);

    if (com) {
      scanf(" %d %d", &b, &e);
      printf("%d\n", find(b, e, 0, 0, n - 1));
    } else {
      scanf(" %d %d", &pos, &num);
      update(pos, num);
    }
  }
}

void init() {
  int m = n;
  n = 1;

  while (m > n)
    n = (n << 1);

  fill(tree, tree + n * 2, INF);
  return;
}

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

  while (i > 0) {
    i = (i - 1) / 2;
    tree[i] = min(tree[i * 2 + 1], tree[i * 2 + 2]);
  }
}

int find(int x, int y, int now, int left, int right) {
  if (right < x || y < left)
    return INF;

  if (x <= left && right <= y)
    return tree[now];

  int find_left = find(x, y, now * 2 + 1, left, (left + right) / 2);
  int find_right = find(x, y, now * 2 + 2, (left + right) / 2 + 1, right);

  return min(find_left, find_right);
}
Problem p02345 (C++)

Buggy submission (status: Runtime Error):
// Segment Tree
//  RMQ
#include <climits>
#include <cstdio>
#include <iostream>
using namespace std;
// const int MAX_N = 1 << 17;
const int MAX_N = 100022;

int n, dat[2 * MAX_N - 1];

int init(int n_) {
  n = 1;

  while (n < n_)
    n *= 2;

  for (int i = 0; i < 2 * n - 1; i++)
    dat[i] = INT_MAX;

  return n;
}

void update(int k, int a) {
  k += n - 1;
  dat[k] = a;
  while (k > 0) {
    k = (k - 1) / 2;
    dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
  }
}

int query(int a, int b, int k, int l, int r) {
  // cout<<a<<" ----- "<<b<<" "<<k<<" "<<l<<" "<<r<<endl;
  if (r <= a || b <= l) {
    // cout<<a<<" DD "<<b<<" "<<k<<" "<<l<<" "<<r<<endl;
    return INT_MAX;
  }

  if (a <= l && r <= b) {
    // cout<<a<<" c "<<b<<" "<<k<<" "<<l<<" "<<r<<endl;
    // cout<<dat[k]<<endl;
    return dat[k];
  } else {
    int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
    // cout<<a<<" b "<<b<<" "<<k<<" "<<l<<" "<<r<<endl;
    // cout<<vl<<" b "<<vr<<endl;
    return min(vl, vr);
  }
}

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

  cin >> n >> q;

  n = init(n);

  for (int i = 0; i < q; i++) {
    scanf("%d %d %d", &com, &x, &y);
    if (com) {
      printf("%d\n", query(x, y + 1, 0, 0, n));
    } else {
      update(x, y);
    }
    // for(int i = 0; i < n*2; i++)
    // cout<<i<<" "<<dat[i]<<endl;
  }

  return 0;
}


Fixed submission:
// Segment Tree
//  RMQ
#include <climits>
#include <cstdio>
#include <iostream>
using namespace std;
// const int MAX_N = 1 << 17;
const int MAX_N = 200022;

int n, dat[2 * MAX_N - 1];

int init(int n_) {
  n = 1;

  while (n < n_)
    n *= 2;

  for (int i = 0; i < 2 * n - 1; i++)
    dat[i] = INT_MAX;

  return n;
}

void update(int k, int a) {
  k += n - 1;
  dat[k] = a;
  while (k > 0) {
    k = (k - 1) / 2;
    dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
  }
}

int query(int a, int b, int k, int l, int r) {
  // cout<<a<<" ----- "<<b<<" "<<k<<" "<<l<<" "<<r<<endl;
  if (r <= a || b <= l) {
    // cout<<a<<" DD "<<b<<" "<<k<<" "<<l<<" "<<r<<endl;
    return INT_MAX;
  }

  if (a <= l && r <= b) {
    // cout<<a<<" c "<<b<<" "<<k<<" "<<l<<" "<<r<<endl;
    // cout<<dat[k]<<endl;
    return dat[k];
  } else {
    int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
    // cout<<a<<" b "<<b<<" "<<k<<" "<<l<<" "<<r<<endl;
    // cout<<vl<<" b "<<vr<<endl;
    return min(vl, vr);
  }
}

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

  cin >> n >> q;

  n = init(n);

  for (int i = 0; i < q; i++) {
    scanf("%d %d %d", &com, &x, &y);
    if (com) {
      printf("%d\n", query(x, y + 1, 0, 0, n));
    } else {
      update(x, y);
    }
    // for(int i = 0; i < n*2; i++)
    // cout<<i<<" "<<dat[i]<<endl;
  }

  return 0;
}
Problem p02345 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;
int dp[200010], n, q, r, x, y, i;
int m(int l, int r, int k) {
  if (x <= l && r <= y)
    return dp[k];
  if (y <= l || r <= x)
    return 2147483647;
  return min(m(l, (l + r) / 2, k * 2 + 1), m((l + r) / 2, r, k * 2 + 2));
}
int main() {
  cin >> n >> q;
  r = 2;
  while (r < n)
    r *= 2;
  n = r, r *= 2;
  while (r--)
    dp[r] = 2147483647;
  while (q--) {
    cin >> r >> x >> y;
    if (r) {
      y++;
      cout << m(0, n, 0) << endl;
    } else {
      i = x + n - 1, dp[i] = y;
      while (i)
        i = (i - 1) / 2, dp[i] = min(dp[i * 2 + 1], dp[i * 2 + 2]);
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
int dp[270000], n, q, r, x, y, i;
int m(int l, int r, int k) {
  if (x <= l && r <= y)
    return dp[k];
  if (y <= l || r <= x)
    return 2147483647;
  return min(m(l, (l + r) / 2, k * 2 + 1), m((l + r) / 2, r, k * 2 + 2));
}
int main() {
  cin >> n >> q;
  r = 2;
  while (r < n)
    r *= 2;
  n = r, r *= 2;
  while (r--)
    dp[r] = 2147483647;
  while (q--) {
    cin >> r >> x >> y;
    if (r) {
      y++;
      cout << m(0, n, 0) << endl;
    } else {
      i = x + n - 1, dp[i] = y;
      while (i)
        i = (i - 1) / 2, dp[i] = min(dp[i * 2 + 1], dp[i * 2 + 2]);
    }
  }
  return 0;
}
Problem p02345 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

const int INF = 2147483647;

class Backet {
  int n, sqrtN, B;
  vector<int> val, backet;

public:
  Backet(vector<int> a) : val(a) {
    n = a.size();
    sqrtN = ceil(sqrt(n));
    B = (n + sqrtN - 1) / sqrtN;

    backet.resize(B, INF);
    for (int i = 0; i < B; i++) {
      for (int j = sqrtN * i; j < sqrtN * (i + 1); j++) {
        if (j >= n)
          break;
        backet[i] = min(backet[i], val[j]);
      }
    }
  }

  int RMQ(int l, int r) {
    int L = (l + sqrtN - 1) / sqrtN + 1, R = r / sqrtN;

    int res = INF;
    if (L <= R) {
      for (int i = l; i < sqrtN * L; i++)
        res = min(res, val[i]);
      for (int i = L; i < R; i++)
        res = min(res, backet[i]);
      for (int i = sqrtN * R; i < r; i++)
        res = min(res, val[i]);
    } else {
      for (int i = l; i < r; i++)
        res = min(res, val[i]);
    }

    return res;
  }

  void update(int k, int v) {
    val[k] = v;
    int x = k / sqrtN;
    backet[x] = INF;
    for (int i = sqrtN * x; i < sqrtN * (x + 1); i++) {
      backet[x] = min(backet[x], val[i]);
    }
  }
};

int main() {
  int n, q;
  cin >> n >> q;
  vector<int> a(n, INF);
  Backet b(a);

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

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

const int INF = 2147483647;

class Backet {
  int n, sqrtN, B;
  vector<int> val, backet;

public:
  Backet(vector<int> a) : val(a) {
    n = a.size();
    sqrtN = ceil(sqrt(n));
    B = (n + sqrtN - 1) / sqrtN;

    backet.resize(B, INF);
    for (int i = 0; i < B; i++) {
      for (int j = sqrtN * i; j < sqrtN * (i + 1); j++) {
        if (j >= n)
          break;
        backet[i] = min(backet[i], val[j]);
      }
    }
  }

  int RMQ(int l, int r) {
    int L = (l + sqrtN - 1) / sqrtN + 1, R = r / sqrtN;

    int res = INF;
    if (L <= R) {
      for (int i = l; i < sqrtN * L; i++)
        res = min(res, val[i]);
      for (int i = L; i < R; i++)
        res = min(res, backet[i]);
      for (int i = sqrtN * R; i < r; i++)
        res = min(res, val[i]);
    } else {
      for (int i = l; i < r; i++)
        res = min(res, val[i]);
    }

    return res;
  }

  void update(int k, int v) {
    val[k] = v;
    int x = k / sqrtN;
    backet[x] = INF;
    for (int i = sqrtN * x; i < sqrtN * (x + 1); i++) {
      if (i >= n)
        break;
      backet[x] = min(backet[x], val[i]);
    }
  }
};

int main() {
  int n, q;
  cin >> n >> q;
  vector<int> a(n, INF);
  Backet b(a);

  for (int i = 0; i < q; i++) {
    int c, x, y;
    cin >> c >> x >> y;
    if (c == 0)
      b.update(x, y);
    else
      cout << b.RMQ(x, y + 1) << endl;
  }
}
Problem p02345 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <iostream>
#include <vector>

#define max_n (1 << 17)

using namespace std;

int dat[max_n], n;

int get(int k) { return dat[k + n]; }

void update(int k, int a) {
  for (k += n, dat[k] = a; k > 1; k >>= 1)
    dat[k >> 1] = min(dat[k], dat[k ^ 1]);
}

int rmq(int a, int b) {
  int res = INT_MAX;
  for (a += n, b += n; a <= b; a = (a + 1) >> 1, b = (b - 1) >> 1) {
    if (a & 1)
      res = min(res, dat[a]);
    if ((b & 1) == 0)
      res = min(res, dat[b]);
  }
  return res;
}

int main(void) {

  fill(dat, dat + max_n, INT_MAX);
  int q;
  cin >> n >> q;

  while (q--) {
    int com, x, y;
    cin >> com >> x >> y;
    if (com)
      cout << rmq(x, y) << endl;
    else
      update(x, y);
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <iostream>
#include <vector>

#define max_n (1 << 18)

using namespace std;

int dat[max_n], n;

int get(int k) { return dat[k + n]; }

void update(int k, int a) {
  for (k += n, dat[k] = a; k > 1; k >>= 1)
    dat[k >> 1] = min(dat[k], dat[k ^ 1]);
}

int rmq(int a, int b) {
  int res = INT_MAX;
  for (a += n, b += n; a <= b; a = (a + 1) >> 1, b = (b - 1) >> 1) {
    if (a & 1)
      res = min(res, dat[a]);
    if ((b & 1) == 0)
      res = min(res, dat[b]);
  }
  return res;
}

int main(void) {

  fill(dat, dat + max_n, INT_MAX);
  int q;
  cin >> n >> q;

  while (q--) {
    int com, x, y;
    cin >> com >> x >> y;
    if (com)
      cout << rmq(x, y) << endl;
    else
      update(x, y);
  }
  return 0;
}
Problem p02345 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <iostream>
#include <vector>

#define MAX_V 100002
#define INF INT_MAX

using namespace std;

int dat[MAX_V * 2], sz;

void update(int k, int x) {
  k += sz - 1;
  dat[k] = x;
  while (k > 0) {
    k = (k - 1) / 2;
    dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
  }
}

int query(int a, int b, int k, int l, int r) {
  if (r <= a || b <= l)
    return INF;
  if (a <= l && r <= b)
    return dat[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 min(vl, vr);
}

int main(void) {

  fill(dat, dat + MAX_V * 2, INF);

  int n, q;
  cin >> n >> q;
  sz = 1;
  while (sz < n)
    sz *= 2;

  while (q--) {
    int com, x, y;
    cin >> com >> x >> y;
    if (com)
      cout << query(x, y + 1, 0, 0, sz) << endl;
    else
      update(x, y);
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <iostream>
#include <vector>

#define MAX_V (1 << 17)
#define INF INT_MAX

using namespace std;

int dat[MAX_V * 2], sz;

void update(int k, int x) {
  k += sz - 1;
  dat[k] = x;
  while (k > 0) {
    k = (k - 1) / 2;
    dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
  }
}

int query(int a, int b, int k, int l, int r) {
  if (r <= a || b <= l)
    return INF;
  if (a <= l && r <= b)
    return dat[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 min(vl, vr);
}

int main(void) {

  fill(dat, dat + MAX_V * 2, INF);

  int n, q;
  cin >> n >> q;
  sz = 1;
  while (sz < n)
    sz *= 2;

  while (q--) {
    int com, x, y;
    cin >> com >> x >> y;
    if (com)
      cout << query(x, y + 1, 0, 0, sz) << endl;
    else
      update(x, y);
  }

  return 0;
}
Problem p02345 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
int A[(1 << 17)];
int n, Q;
int N;
void init() {
  N = 1;
  while (N < n)
    N *= 2;
  for (int i = 0; i < 2 * N - 1; i++)
    A[i] = 2147483647;
}
int find(int a, int b, int k, int l, int r) {
  if (a <= l && r <= b)
    return A[k];
  if (r <= a || b <= l)
    return 2147483647;
  int vl = find(a, b, k * 2 + 1, l, (l + r) / 2);
  int vr = find(a, b, k * 2 + 2, (l + r) / 2, r);
  // printf("find %d %d %d %d\n", l, r, vl,vr);
  return min(vl, vr);
}
void update(int i, int x) {
  i += N - 1;
  A[i] = x;
  // printf("update %d %d\n", i, A[i]);
  while (i > 0) {
    i = (i - 1) / 2;
    A[i] = min(A[2 * i + 1], A[2 * i + 2]);
    // printf("update %d %d\n", i, A[i]);
  }
}
int main() {
  cin >> n >> Q;
  init();
  for (int i = 0; i < Q; i++) {
    int com, x, y;
    cin >> com >> x >> y;
    if (com == 0)
      update(x, y);
    else
      printf("%d\n", find(x, y + 1, 0, 0, N));
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
int A[(1 << 20)];
int n, Q;
int N;
void init() {
  N = 1;
  while (N < n)
    N *= 2;
  for (int i = 0; i < 2 * N - 1; i++)
    A[i] = 2147483647;
}
int find(int a, int b, int k, int l, int r) {
  if (a <= l && r <= b)
    return A[k];
  if (r <= a || b <= l)
    return 2147483647;
  int vl = find(a, b, k * 2 + 1, l, (l + r) / 2);
  int vr = find(a, b, k * 2 + 2, (l + r) / 2, r);
  // printf("find %d %d %d %d\n", l, r, vl,vr);
  return min(vl, vr);
}
void update(int i, int x) {
  i += N - 1;
  A[i] = x;
  // printf("update %d %d\n", i, A[i]);
  while (i > 0) {
    i = (i - 1) / 2;
    A[i] = min(A[2 * i + 1], A[2 * i + 2]);
    // printf("update %d %d\n", i, A[i]);
  }
}
int main() {
  cin >> n >> Q;
  init();
  for (int i = 0; i < Q; i++) {
    int com, x, y;
    cin >> com >> x >> y;
    if (com == 0)
      update(x, y);
    else
      printf("%d\n", find(x, y + 1, 0, 0, N));
  }
  return 0;
}
Problem p02345 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
int n, q, data[200010];
int INF = (1LL << 31) - 1;

void init(int &n_) {
  int n = 1;
  while (n < n_)
    n *= 2;
  n_ = n;
  for (int i = 0; i < 2 * n - 1; i++)
    data[i] = INF;
}
void update(int k, int x) {
  k += n - 1;
  data[k] = x;
  while (k > 0) {
    k = (k - 1) / 2;
    data[k] = std::min(data[k * 2 + 1], data[k * 2 + 2]);
  }
}
int find(int a, int b, int k, int l, int r) {
  if (r <= a)
    return INF;
  if (b <= l)
    return INF;
  if (a <= l && r <= b)
    return data[k];
  int val1 = find(a, b, k * 2 + 1, l, (r + l) / 2);
  int val2 = find(a, b, k * 2 + 2, (r + l) / 2, r);
  return std::min(val1, val2);
}

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


Fixed submission:
#include <algorithm>
#include <iostream>
int n, q, data[300010];
int INF = (1LL << 31) - 1;

void init(int &n_) {
  int n = 1;
  while (n < n_)
    n *= 2;
  n_ = n;
  for (int i = 0; i < 2 * n - 1; i++)
    data[i] = INF;
}
void update(int k, int x) {
  k += n - 1;
  data[k] = x;
  while (k > 0) {
    k = (k - 1) / 2;
    data[k] = std::min(data[k * 2 + 1], data[k * 2 + 2]);
  }
}
int find(int a, int b, int k, int l, int r) {
  if (r <= a)
    return INF;
  if (b <= l)
    return INF;
  if (a <= l && r <= b)
    return data[k];
  int val1 = find(a, b, k * 2 + 1, l, (r + l) / 2);
  int val2 = find(a, b, k * 2 + 2, (r + l) / 2, r);
  return std::min(val1, val2);
}

int main() {
  std::cin >> n >> q;
  init(n);
  for (int i = 0; i < q; i++) {
    int a, b, c;
    std::cin >> a >> b >> c;
    if (a == 0)
      update(b, c);
    else
      std::cout << find(b, c + 1, 0, 0, n) << std::endl;
  }
  return 0;
}
Problem p02345 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define MAX_N 1 << 17
int n, dat[2 * MAX_N - 1];

// INT＿MAXで初期化
void init(int n_) {
  n = 1;
  while (n < n_)
    n *= 2;
  for (int i = 0; i < 2 * n - 1; i++)
    dat[i] = INT_MAX;
}

void update(int k, int a) {
  k += n - 1;
  dat[k] = a;
  while (k > 0) {
    k = (k - 1) / 2;
    dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
  }
}

int query(int a, int b, int k = 0, int l = 0, int r = n) {
  if (r <= a || b <= l)
    return INT_MAX;
  if (a <= l && r <= b)
    return dat[k];
  else {
    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 min(vl, vr);
  }
}

int main() {
  int q;
  cin >> n >> q;
  init(n);
  for (int i = 0; i < q; i++) {
    int com, x, y;
    cin >> com >> x >> y;
    if (com)
      cout << query(x, y + 1) << endl;
    else
      update(x, y);
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define MAX_N 1 << 17
int n, dat[2 * (MAX_N)-1];

// INT＿MAXで初期化
void init(int n_) {
  n = 1;
  while (n < n_)
    n *= 2;
  for (int i = 0; i < 2 * n - 1; i++)
    dat[i] = INT_MAX;
}

void update(int k, int a) {
  k += n - 1;
  dat[k] = a;
  while (k > 0) {
    k = (k - 1) / 2;
    dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
  }
}

int query(int a, int b, int k = 0, int l = 0, int r = n) {
  if (r <= a || b <= l)
    return INT_MAX;
  if (a <= l && r <= b)
    return dat[k];
  else {
    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 min(vl, vr);
  }
}

int main() {
  int q;
  cin >> n >> q;
  init(n);
  for (int i = 0; i < q; i++) {
    int com, x, y;
    cin >> com >> x >> y;
    if (com)
      cout << query(x, y + 1) << endl;
    else
      update(x, y);
  }
  return 0;
}
Problem p02345 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
using namespace std;
typedef long long ll;
static const int INF = 2147483647;
static const int MAX_N = 100000;

int n, dat[2 * MAX_N - 1];

void init(int n_) {
  n = 1;
  while (n < n_)
    n *= 2;

  for (int i = 0; i < 2 * n - 1; i++)
    dat[i] = INF;
}

void update(int k, int a) {
  k += n - 1;
  dat[k] = a;
  while (k > 0) {
    k = (k - 1) / 2;
    dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
  }
}

int query(int a, int b, int k, int l, int r) {
  if (r <= a || b <= l)
    return INF;

  if (a <= l && r <= b)
    return dat[k];
  else {
    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 min(vl, vr);
  }
}

int main() {
  int n_, q;
  scanf("%d %d", &n_, &q);
  init(n_);
  for (int i = 0; i < q; i++) {
    int com, x, y;
    scanf("%d %d %d", &com, &x, &y);
    if (com == 0)
      update(x, y);
    if (com == 1)
      printf("%d\n", query(x, y + 1, 0, 0, n));
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
using namespace std;
typedef long long ll;
static const int INF = 2147483647;
static const int MAX_N = 1 << 18;

int n, dat[2 * MAX_N - 1];

void init(int n_) {
  n = 1;
  while (n < n_)
    n *= 2;

  for (int i = 0; i < 2 * n - 1; i++)
    dat[i] = INF;
}

void update(int k, int a) {
  k += n - 1;
  dat[k] = a;
  while (k > 0) {
    k = (k - 1) / 2;
    dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
  }
}

int query(int a, int b, int k, int l, int r) {
  if (r <= a || b <= l)
    return INF;

  if (a <= l && r <= b)
    return dat[k];
  else {
    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 min(vl, vr);
  }
}

int main() {
  int n_, q;
  scanf("%d %d", &n_, &q);
  init(n_);
  for (int i = 0; i < q; i++) {
    int com, x, y;
    scanf("%d %d %d", &com, &x, &y);
    if (com == 0)
      update(x, y);
    if (com == 1)
      printf("%d\n", query(x, y + 1, 0, 0, n));
  }
  return 0;
}
Problem p02345 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define P pair<int, int>
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
const int MAX_N = 100001;
const int INF = 2147483647;

// 要素の数、区間などを何からの値を覚えておく配列
int n, q, v[2 * MAX_N - 1];

// 初期化
void init(int _n) {
  n = 1;
  while (n < _n)
    n *= 2;
  REP(i, 2 * n - 1) v[i] = INF;
}

// 添字がkのものをaに更新
void update(int k, int a) {
  k += n - 1;
  v[k] = a;
  while (k > 0) {
    k = (k - 1) / 2;
    v[k] = min(v[k * 2 + 1], v[k * 2 + 2]);
  }
}

// [a, b)の最小値を求める
// kは今見ている添字、l, rはそれに対応する[l, r)
// 呼び出す時はquery(a, b, 0, 0, n)として呼ぶ
int query(int a, int b, int k, int l, int r) {
  if (b <= l || a >= r)
    return INF;
  if (a <= l && b >= r)
    return v[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 min(vl, vr);
}

int main() {
  cin >> n >> q;
  init(n);
  REP(i, q) {
    int Q, x, y;
    cin >> Q >> x >> y;
    if (Q)
      cout << query(x, y + 1, 0, 0, n) << endl;
    else
      update(x, y);
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define P pair<int, int>
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
const int MAX_N = 200001;
const int INF = 2147483647;

// 要素の数、区間などを何からの値を覚えておく配列
int n, q, v[2 * MAX_N - 1];

// 初期化
void init(int _n) {
  n = 1;
  while (n < _n)
    n *= 2;
  REP(i, 2 * n - 1) v[i] = INF;
}

// 添字がkのものをaに更新
void update(int k, int a) {
  k += n - 1;
  v[k] = a;
  while (k > 0) {
    k = (k - 1) / 2;
    v[k] = min(v[k * 2 + 1], v[k * 2 + 2]);
  }
}

// [a, b)の最小値を求める
// kは今見ている添字、l, rはそれに対応する[l, r)
// 呼び出す時はquery(a, b, 0, 0, n)として呼ぶ
int query(int a, int b, int k, int l, int r) {
  if (b <= l || a >= r)
    return INF;
  if (a <= l && b >= r)
    return v[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 min(vl, vr);
}

int main() {
  cin >> n >> q;
  init(n);
  REP(i, q) {
    int Q, x, y;
    cin >> Q >> x >> y;
    if (Q)
      cout << query(x, y + 1, 0, 0, n) << endl;
    else
      update(x, y);
  }
  return 0;
}
Problem p02346 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, a, b) for (int i = (a); i < (b); i++)
#define RREP(i, a, b) for (int i = (a); i >= (b); i--)
#define pq priority_queue
#define P pair<int, int>
#define P2 pair<int, P>
#define P3 pair<int, P2>
typedef long long ll;
typedef long double ld;
using namespace std;
const int INF = 1e9, MOD = 1e9 + 7,
          around[] = {0, 1, 1, -1, -1, 0, -1, 1, 0, 0};
const int vx[] = {1, 0, -1, 0}, vy[] = {0, 1, 0, -1};
const ll LINF = 1e18;
const ld PI = abs(acos(-1));
const int sqrtN = 512;

struct SqrtDecomposition {
  vector<int> data;
  vector<int> bucketSum;
  int N, K;

  SqrtDecomposition(int n) : N(n) {
    K = (N + sqrtN - 1) / sqrtN;
    data.assign(n, 0);
    bucketSum.assign(K, 0);
  }

  void add(int k, int x) {
    data[k] += x;
    k = k / sqrtN;

    int sum = 0;
    for (int i = k * sqrtN; i < (k + 1) * sqrtN; i++) {
      sum += data[i];
    }

    bucketSum[k] = sum;
  }

  int get(int a, int b) {
    int sum = 0;
    for (int k = 0; k < K; k++) {
      int l = k * sqrtN, r = (k + 1) * sqrtN;

      if (b <= l || r <= a)
        continue;
      if (a <= l && r <= b)
        sum += bucketSum[k];
      else {
        for (int i = max(a, l); i < min(b, r); i++) {
          sum += data[i];
        }
      }
    }

    return sum;
  }
};

int main() {
  int n, q;
  cin >> n >> q;
  SqrtDecomposition data(n);
  REP(i, 0, q) {
    int a, b, c;
    cin >> a >> b >> c;

    if (a == 0)
      data.add(b, c);
    else if (a == 1)
      cout << data.get(b, c + 1) << endl;
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define REP(i, a, b) for (int i = (a); i < (b); i++)
#define RREP(i, a, b) for (int i = (a); i >= (b); i--)
#define pq priority_queue
#define P pair<int, int>
#define P2 pair<int, P>
#define P3 pair<int, P2>
typedef long long ll;
typedef long double ld;
using namespace std;
const int INF = 1e9, MOD = 1e9 + 7,
          around[] = {0, 1, 1, -1, -1, 0, -1, 1, 0, 0};
const int vx[] = {1, 0, -1, 0}, vy[] = {0, 1, 0, -1};
const ll LINF = 1e18;
const ld PI = abs(acos(-1));
const int sqrtN = 512;

struct SqrtDecomposition {
  vector<int> data;
  vector<int> bucketSum;
  int N, K;

  SqrtDecomposition(int n) : N(n) {
    K = (N + sqrtN - 1) / sqrtN;
    data.assign(K * sqrtN, 0);
    bucketSum.assign(K, 0);
  }

  void add(int k, int x) {
    data[k] += x;
    k = k / sqrtN;

    int sum = 0;
    for (int i = k * sqrtN; i < (k + 1) * sqrtN; i++) {
      sum += data[i];
    }

    bucketSum[k] = sum;
  }

  int get(int a, int b) {
    int sum = 0;
    for (int k = 0; k < K; k++) {
      int l = k * sqrtN, r = (k + 1) * sqrtN;

      if (b <= l || r <= a)
        continue;
      if (a <= l && r <= b)
        sum += bucketSum[k];
      else {
        for (int i = max(a, l); i < min(b, r); i++) {
          sum += data[i];
        }
      }
    }

    return sum;
  }
};

int main() {
  int n, q;
  cin >> n >> q;
  SqrtDecomposition data(n);
  REP(i, 0, q) {
    int a, b, c;
    cin >> a >> b >> c;

    if (a == 0)
      data.add(b, c);
    else if (a == 1)
      cout << data.get(b, c + 1) << endl;
  }

  return 0;
}
Problem p02346 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;
#define N 131072
int tree[N * 2];
int getsum(int a, int b, int k, int l, int r) {
  if (r <= a || b <= l)
    return 0;
  if (a <= l && r <= b)
    return tree[k];
  int m = (r) / 2;
  return getsum(a, b, k * 2 + 1, l, m) + getsum(a, b, k * 2 + 2, m, r);
}
void add(int a, int b) {
  a += N - 1;
  tree[a] += b;
  while (a) {
    a = (a - 1) / 2;
    tree[a] += b;
  }
}
int main() {
  int n, q;
  int c, a, b;
  cin >> n >> q;
  for (int i = 0; i < q; i++) {
    cin >> c >> a >> b;
    a--;
    if (c == 1)
      cout << getsum(a, b, 0, 0, N) << endl;
    else
      add(a, b);
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
#define N 131072
int tree[N * 2];
int getsum(int a, int b, int k, int l, int r) {
  if (r <= a || b <= l)
    return 0;
  if (a <= l && r <= b)
    return tree[k];
  int m = (l + r) / 2;
  return getsum(a, b, k * 2 + 1, l, m) + getsum(a, b, k * 2 + 2, m, r);
}
void add(int a, int b) {
  a += N - 1;
  tree[a] += b;
  while (a) {
    a = (a - 1) / 2;
    tree[a] += b;
  }
}
int main() {
  int n, q;
  int c, a, b;
  cin >> n >> q;
  for (int i = 0; i < q; i++) {
    cin >> c >> a >> b;
    a--;
    if (c == 1)
      cout << getsum(a, b, 0, 0, N) << endl;
    else
      add(a, b);
  }
  return 0;
}
Problem p02346 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm> // require sort next_permutation count __gcd reverse etc.
#include <cctype>    // require tolower, toupper
#include <cfloat>
#include <climits>
#include <cmath>   // require fabs
#include <cstdio>  // require scanf printf
#include <cstdlib> // require abs exit atof atoi
#include <cstring> // require memset
#include <ctime>   // require srand
#include <deque>
#include <fstream> // require freopen
#include <functional>
#include <iomanip> // require setw
#include <iostream>
#include <limits>
#include <map>
#include <numeric> // require accumulate
#include <queue>
#include <set>
#include <sstream> // require stringstream
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(A) A.begin(), A.end()

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

const int MAX_N = 10005;
int bit[MAX_N], n;

void add(int i, int x) {

  while (i <= n) {
    bit[i] += x;
    i += i & (-i);
  } // end while
}

int sum(int i) {

  int s = 0;
  while (i > 0) {
    s += bit[i];
    i -= i & (-i);
  } // end while

  return s;
}

void init_bit(void) {
  n = 0;
  memset(bit, 0, sizeof(bit));
}

int main() {
  init_bit();
  int q;
  scanf("%d %d", &n, &q);

  rep(i, q) {
    int com, x, y;
    scanf("%d %d %d", &com, &x, &y);
    if (com == 0) {
      add(x, y);
    } else {
      int s = sum(y);
      int t = sum(x - 1);
      int res = s - t;
      printf("%d\n", res);
    } // end if
  }   // end rep

  return 0;
}

Fixed submission:
#include <algorithm> // require sort next_permutation count __gcd reverse etc.
#include <cctype>    // require tolower, toupper
#include <cfloat>
#include <climits>
#include <cmath>   // require fabs
#include <cstdio>  // require scanf printf
#include <cstdlib> // require abs exit atof atoi
#include <cstring> // require memset
#include <ctime>   // require srand
#include <deque>
#include <fstream> // require freopen
#include <functional>
#include <iomanip> // require setw
#include <iostream>
#include <limits>
#include <map>
#include <numeric> // require accumulate
#include <queue>
#include <set>
#include <sstream> // require stringstream
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(A) A.begin(), A.end()

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

const int MAX_N = 100005;
int bit[MAX_N], n;

void add(int i, int x) {

  while (i <= n) {
    bit[i] += x;
    i += i & (-i);
  } // end while
}

int sum(int i) {

  int s = 0;
  while (i > 0) {
    s += bit[i];
    i -= i & (-i);
  } // end while

  return s;
}

void init_bit(void) {
  n = 0;
  memset(bit, 0, sizeof(bit));
}

int main() {
  init_bit();
  int q;
  scanf("%d %d", &n, &q);

  rep(i, q) {
    int com, x, y;
    scanf("%d %d %d", &com, &x, &y);
    if (com == 0) {
      add(x, y);
    } else {
      int s = sum(y);
      int t = sum(x - 1);
      int res = s - t;
      printf("%d\n", res);
    } // end if
  }   // end rep

  return 0;
}
Problem p02346 (C++)

Buggy submission (status: Runtime Error):
#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;
}


Fixed submission:
#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;
}
Problem p02346 (C++)

Buggy submission (status: 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, 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;
}


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 * 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;
}
Problem p02346 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;

int n;
long long int dat[300000];

void init(int nn) {
  n = 1;
  while (n < nn)
    n *= 2;
  for (int i = 0; i < n * 2 - 1; i++) {
    dat[i] = 0ll;
  }
}

void add(int x, int i) {
  x += n - 1;
  dat[x] += i;
  while (x) {
    x = (x - 1) / 2;
    dat[x] = dat[x * 2 + 1] + dat[x * 2 + 2];
  }
}

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

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

Fixed submission:
#include <iostream>
using namespace std;

int n;
long long int dat[300000];

void init(int nn) {
  n = 1;
  while (n < nn)
    n *= 2;
  for (int i = 0; i < n * 2 - 1; i++) {
    dat[i] = 0ll;
  }
}

void add(int x, int i) {
  x += n - 1;
  dat[x] += i;
  while (x) {
    x = (x - 1) / 2;
    dat[x] = dat[x * 2 + 1] + dat[x * 2 + 2];
  }
}

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

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