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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <chrono>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <thread>
#include <utility>
#include <vector>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pi;
const ll E = 1e18 + 7;
#define F first
#define S second
#define MK make_pair
const ll MOD = 1000000007;

vector<vector<ll>> c;

void mk_c() {
  c.resize(3100);
  for (int i = 1; i < 2100; i++) {
    c[i].resize(3100);
    c[i][0] = 1;
    c[i][i] = 1;
    for (int t = 1; t < i; t++) {
      c[i][t] = (c[i - 1][t - 1] + c[i - 1][t]) % MOD;
    }
  }
}

int main() {
  ll n, k;
  cin >> n >> k;
  n -= k;
  if (n < 0) {
    cout << 0 << endl;
    return 0;
  }
  mk_c();
  cout << c[n + k - 1][k - 1] << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <chrono>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <thread>
#include <utility>
#include <vector>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pi;
const ll E = 1e18 + 7;
#define F first
#define S second
#define MK make_pair
const ll MOD = 1000000007;

vector<vector<ll>> c;

void mk_c() {
  c.resize(3100);
  for (int i = 1; i < 2100; i++) {
    c[i].resize(3100);
    c[i][0] = 1;
    c[i][i] = 1;
    for (int t = 1; t < i; t++) {
      c[i][t] = (c[i - 1][t - 1] + c[i - 1][t]) % MOD;
    }
  }
}

int main() {
  ll n, k;
  cin >> n >> k;
  n -= k;
  if (n < 0) {
    cout << 0 << endl;
    return 0;
  }
  if (n == 0) {
    cout << 1 << endl;
    return 0;
  }
  mk_c();
  cout << c[n + k - 1][k - 1] << endl;

  return 0;
}
Problem p02343 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <iostream>
#include <vector>
using namespace std;
const int num = 5000;
vector<int> v[num];
int cnt = -1;
inline void merge(int a, int b) {
  int m = -1, n = -1;
  for (int i = 0; i <= cnt; i++) {
    for (int j = 0; j < v[i].size(); j++) {
      if (m != -1 && n != -1)
        break;
      if (v[i][j] == a)
        m = i;
      if (v[i][j] == b)
        n = i;
    }
    if (m != -1 && n != -1)
      break;
  }
  if (m != -1 && n != -1 && m != n) {
    int x = v[n].size();
    for (int i = 0; i < x; i++) {
      v[m].push_back(v[n][i]);
      v[n].erase(v[n].begin() + i);
    }
  } else if (m != -1 && n != -1 && m == n)
    return;
  else if (m != -1 && n == -1)
    v[m].push_back(b);
  else if (m == -1 && n != -1)
    v[n].push_back(a);
  else {
    cnt++;
    v[cnt].push_back(a);
    v[cnt].push_back(b);
  }
}
inline void same(int a, int b) {
  int m = -1, n = -1;
  for (int i = 0; i <= cnt; i++) {
    for (int j = 0; j < v[i].size(); j++) {
      if (m != -1 && n != -1)
        break;
      if (v[i][j] == a)
        m = i;
      if (v[i][j] == b)
        n = i;
      if (m != -1 && n != -1)
        break;
    }
  }
  if (m != -1 && n != -1 && m == n)
    cout << '1' << endl;
  else
    cout << '0' << endl;
}

int main() {
  int n, q, com, fi, se;
  cin >> n >> q;
  for (int i = 0; i < q; i++) {
    cin >> com >> fi >> se;
    if (com == 0)
      merge(fi, se);
    else
      same(fi, se);
  }
  return 0;
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <iostream>
#include <vector>
using namespace std;
const int num = 5000;
vector<int> v[num];
int cnt = -1;
inline void merge(int a, int b) {
  int m = -1, n = -1;
  for (int i = 0; i <= cnt; i++) {
    for (int j = 0; j < v[i].size(); j++) {
      if (m != -1 && n != -1)
        break;
      if (v[i][j] == a)
        m = i;
      if (v[i][j] == b)
        n = i;
    }
    if (m != -1 && n != -1)
      break;
  }
  if (m != -1 && n != -1 && m != n) {
    int x = v[n].size();
    for (int i = 0; i < x; i++) {
      v[m].push_back(v[n][0]);
      v[n].erase(v[n].begin());
    }
  } else if (m != -1 && n != -1 && m == n)
    return;
  else if (m != -1 && n == -1)
    v[m].push_back(b);
  else if (m == -1 && n != -1)
    v[n].push_back(a);
  else {
    cnt++;
    v[cnt].push_back(a);
    v[cnt].push_back(b);
  }
}
inline void same(int a, int b) {
  int m = -1, n = -1;
  for (int i = 0; i <= cnt; i++) {
    for (int j = 0; j < v[i].size(); j++) {
      if (m != -1 && n != -1)
        break;
      if (v[i][j] == a)
        m = i;
      if (v[i][j] == b)
        n = i;
      if (m != -1 && n != -1)
        break;
    }
  }
  if (m != -1 && n != -1 && m == n)
    cout << '1' << endl;
  else
    cout << '0' << endl;
}

int main() {
  int n, q, com, fi, se;
  cin >> n >> q;
  for (int i = 0; i < q; i++) {
    cin >> com >> fi >> se;
    if (com == 0)
      merge(fi, se);
    else
      same(fi, se);
  }
  return 0;
}
Problem p02343 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>

using namespace std;

struct edge {
  int dest;
  int weight; // = dest minus curr
};

class UF {
public:
  vector<edge> v;
  UF(int n) { v = vector<edge>(n, {-1, 0}); }
  void unite(int x, int y, int y_minus_x) {
    int loc_x = find(x);
    int loc_y = find(y);
    if (v[loc_x].dest <
        v[loc_y].dest) { // x's set has more elements -- join y to x
      v[loc_x].dest += v[loc_y].dest;
      v[loc_y] = {loc_x, -y_minus_x};
    } else {
      v[loc_y].dest += v[loc_x].dest;
      v[loc_x] = {loc_y, y_minus_x};
    }
  }
  void unite(int x, int y) { unite(x, y, 0); }

  // if weighted = true, returns the difference of the root and the current node
  // if weighted = false, returns the id of the root
  int find(int x, bool weighted = false) {
    int tmp = 0;
    while (v[x].dest >= 0) {
      tmp += v[x].weight;
      x = v[x].dest;
    }
    return (weighted ? tmp : x);
  }
  // pre: x and y are in the same set
  // post: returns y - x
  int get_difference(int x, int y) {
    int root_minus_x = find(x, true);
    int root_minus_y = find(y, true);
    return root_minus_x - root_minus_y;
  }
};

int main() {
  int n, q;
  cin >> n >> q;
  UF uf(n);
  int c, x, y;
  while (cin >> c >> x >> y) {
    if (c == 0)
      uf.unite(x, y);
    if (c == 1)
      cout << (uf.find(x) == uf.find(y)) << endl;
  }
}


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

using namespace std;

struct edge {
  int dest;
  int weight; // = dest minus curr
};

class UF {
public:
  vector<edge> v;
  UF(int n) { v = vector<edge>(n, {-1, 0}); }
  void unite(int x, int y, int y_minus_x) {
    int loc_x = find(x);
    int loc_y = find(y);
    if (loc_x == loc_y)
      return;
    if (v[loc_x].dest <
        v[loc_y].dest) { // x's set has more elements -- join y to x
      v[loc_x].dest += v[loc_y].dest;
      v[loc_y] = {loc_x, -y_minus_x};
    } else {
      v[loc_y].dest += v[loc_x].dest;
      v[loc_x] = {loc_y, y_minus_x};
    }
  }
  void unite(int x, int y) { unite(x, y, 0); }

  // if weighted = true, returns the difference of the root and the current node
  // if weighted = false, returns the id of the root
  int find(int x, bool weighted = false) {
    int tmp = 0;
    while (v[x].dest >= 0) {
      tmp += v[x].weight;
      x = v[x].dest;
    }
    return (weighted ? tmp : x);
  }
  // pre: x and y are in the same set
  // post: returns y - x
  int get_difference(int x, int y) {
    int root_minus_x = find(x, true);
    int root_minus_y = find(y, true);
    return root_minus_x - root_minus_y;
  }
};

int main() {
  int n, q;
  cin >> n >> q;
  UF uf(n);
  int c, x, y;
  while (cin >> c >> x >> y) {
    if (c == 0)
      uf.unite(x, y);
    if (c == 1)
      cout << (uf.find(x) == uf.find(y)) << endl;
  }
}
Problem p02343 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <vector>

typedef struct node {
  int number;
  node *parent;
} node;

node *root;

node *find(node *x) {

  if (x->parent == root)
    return x;
  else {
    x->parent = find(x->parent);
    return x->parent;
  }
}

int main(void) {
  int n, q;
  scanf("%d %d", &n, &q);
  std::vector<node> nodes(n);
  for (int i = 0; i < n; i++) {
    nodes[i].number = i;
    nodes[i].parent = root;
  }
  for (int i = 0; i < q; i++) {
    int op, x, y;
    node *x_parent, *y_parent;
    scanf("%d %d %d", &op, &x, &y);
    x_parent = find(&nodes[x]);
    y_parent = find(&nodes[y]);
    if (op == 0) {
      if (x_parent->number > y_parent->number)
        x_parent->parent = y_parent;
      else
        y_parent->parent = x_parent;
    } else {
      if (x_parent->number == y_parent->number)
        printf("1\n");
      else
        printf("0\n");
    }
  }
  return 0;
}

Fixed submission:
#include <cstdio>
#include <vector>

typedef struct node {
  int number;
  node *parent;
} node;

node *root;

node *find(node *x) {

  if (x->parent == root)
    return x;
  else {
    x->parent = find(x->parent);
    return x->parent;
  }
}

int main(void) {
  int n, q;
  scanf("%d %d", &n, &q);
  std::vector<node> nodes(n);
  for (int i = 0; i < n; i++) {
    nodes[i].number = i;
    nodes[i].parent = root;
  }
  for (int i = 0; i < q; i++) {
    int op, x, y;
    node *x_parent, *y_parent;
    scanf("%d %d %d", &op, &x, &y);
    x_parent = find(&nodes[x]);
    y_parent = find(&nodes[y]);
    if (op == 0) {
      if (x_parent->number > y_parent->number)
        x_parent->parent = y_parent;
      else if (x_parent->number < y_parent->number)
        y_parent->parent = x_parent;
    } else {
      if (x_parent->number == y_parent->number)
        printf("1\n");
      else
        printf("0\n");
    }
  }
  return 0;
}
Problem p02343 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>

using namespace std;

struct UF {
  vector<int> a;
  vector<int> size;

  UF(int n) {
    a.assign(n, 0);
    size.assign(n, 1);
    for (int i = 0; i < n; i++) {
      a[i] = i;
    }
  }

  int root(int u) {
    int parent = u;
    while (a[parent] != parent) {
      parent = a[u];
    }
    return parent;
  }

  bool same(int u, int v) { return root(u) == root(v); }

  void unite(int u, int v) {
    int i = root(u);
    int j = root(v);
    if (i != j) {
      if (size[i] > size[j]) {
        a[j] = i;
        size[i] += size[j];
      } else {
        a[i] = j;
        size[j] += size[i];
      }
    }
  }
};

int main() {
  int n, q;
  cin >> n >> q;
  UF uf = UF(n + 1);
  for (int i = 0; i < q; i++) {
    int cmd, u, v;
    cin >> cmd >> u >> v;
    if (cmd == 0) {
      uf.unite(u, v);
    } else {
      cout << (uf.same(u, v) ? 1 : 0) << endl;
    }
  }
  return 0;
}

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

using namespace std;

struct UF {
  vector<int> a;
  vector<int> size;

  UF(int n) {
    a.assign(n, 0);
    size.assign(n, 1);
    for (int i = 0; i < n; i++) {
      a[i] = i;
    }
  }

  int root(int u) {
    int parent = u;
    while (a[parent] != parent) {
      parent = a[parent];
    }
    return parent;
  }

  bool same(int u, int v) { return root(u) == root(v); }

  void unite(int u, int v) {
    int i = root(u);
    int j = root(v);
    if (i != j) {
      if (size[i] > size[j]) {
        a[j] = i;
        size[i] += size[j];
      } else {
        a[i] = j;
        size[j] += size[i];
      }
    }
  }
};

int main() {
  int n, q;
  cin >> n >> q;
  UF uf = UF(n + 1);
  for (int i = 0; i < q; i++) {
    int cmd, u, v;
    cin >> cmd >> u >> v;
    if (cmd == 0) {
      uf.unite(u, v);
    } else {
      cout << (uf.same(u, v) ? 1 : 0) << endl;
    }
  }
  return 0;
}
Problem p02343 (C++)

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

struct union_find_tree {

  vector<ll> par;

  union_find_tree(ll n) {
    par.resize(n);
    for (ll i = 0; i < n; i++) {
      par[i] = i;
    }
  }

  ll find(ll a) {
    if (par[a] == a)
      return a;
    return par[a] = find(par[a]);
  }

  void unite(ll a, ll b) { par[find(a)] = b; }

  bool same(ll a, ll b) { return find(a) == find(b); }
};

int main() {
  ll n, q;
  cin >> n >> q;
  union_find_tree uni(n);
  for (ll i = 0; i < q; i++) {
    ll c, x, y;
    cin >> c >> x >> y;
    if (c == 0) {
      uni.unite(x, y);
    } else {
      if (uni.same(x, y)) {
        cout << 1 << endl;
      } else {
        cout << 0 << endl;
      }
    }
  }
}


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

struct union_find_tree {

  vector<ll> par;

  union_find_tree(ll n) {
    par.resize(n);
    for (ll i = 0; i < n; i++) {
      par[i] = i;
    }
  }

  ll find(ll a) {
    if (par[a] == a)
      return a;
    return par[a] = find(par[a]);
  }

  void unite(ll a, ll b) { par[find(a)] = find(b); }

  bool same(ll a, ll b) { return find(a) == find(b); }
};

int main() {
  ll n, q;
  cin >> n >> q;
  union_find_tree uni(n);
  for (ll i = 0; i < q; i++) {
    ll c, x, y;
    cin >> c >> x >> y;
    if (c == 0) {
      uni.unite(x, y);
    } else {
      if (uni.same(x, y)) {
        cout << 1 << endl;
      } else {
        cout << 0 << endl;
      }
    }
  }
}
Problem p02343 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <cstdio>
#include <functional>
#include <iostream>
#include <list>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define _USE_MATH_DEFINES

#include <map>
#include <math.h>

#define SENTINEL 1000000001

#define min(a, b) (a) > (b) ? (b) : (a)
#define max(a, b) (a) > (b) ? (a) : (b)

using namespace std;

typedef pair<int, int> pii;

using namespace std;

struct UnionFind {
  vector<int> parent;
  vector<int> rank;

  UnionFind(int n) : parent(n), rank(n, 0) {
    for (int i = 0; i < n; i++) {
      parent[i] = i;
    }
  }

  int find(int x) {
    if (x == parent[x])
      return x;
    else
      return parent[x] = find(parent[x]);
  }

  bool same(int x, int y) { return find(x) == find(y); }

  void unite(int x, int y) {
    int a = parent[x];
    int b = parent[y];

    if (a == b) {
      return;
    }

    if (rank[a] < rank[b]) {
      parent[a] = b;
    } else {
      parent[b] = a;
      if (rank[a] == rank[b])
        rank[b]++;
    }
  }
};

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

  UnionFind uf(n);

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

    switch (c) {
    case 0:
      uf.unite(x, y);
      break;
    case 1:
      cout << (uf.same(x, y) ? 1 : 0) << endl;
      break;
    }
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <cstdio>
#include <functional>
#include <iostream>
#include <list>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define _USE_MATH_DEFINES

#include <map>
#include <math.h>

#define SENTINEL 1000000001

#define min(a, b) (a) > (b) ? (b) : (a)
#define max(a, b) (a) > (b) ? (a) : (b)

using namespace std;

typedef pair<int, int> pii;

using namespace std;

struct UnionFind {
  vector<int> parent;
  vector<int> rank;

  UnionFind(int n) : parent(n), rank(n, 0) {
    for (int i = 0; i < n; i++) {
      parent[i] = i;
    }
  }

  int find(int x) {
    if (x == parent[x])
      return x;
    else
      return parent[x] = find(parent[x]);
  }

  bool same(int x, int y) { return find(x) == find(y); }

  void unite(int x, int y) {
    int a = find(x);
    int b = find(y);

    if (a == b) {
      return;
    }

    if (rank[a] < rank[b]) {
      parent[a] = b;
    } else {
      parent[b] = a;
      if (rank[a] == rank[b])
        rank[b]++;
    }
  }
};

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

  UnionFind uf(n);

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

    switch (c) {
    case 0:
      uf.unite(x, y);
      break;
    case 1:
      cout << (uf.same(x, y) ? 1 : 0) << endl;
      break;
    }
  }

  return 0;
}
Problem p02343 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define INT_MAX 2147483647
#define INT_MIN -2147483646
#define Loop(i, n) for (int i = 0; i < (int)n; i++)
#define Loop1(i, n) for (int i = 1; i <= (int)n; i++)
#define Loopr(i, n) for (int i = (int)n - 1; i >= 0; i--)
#define Loopr1(i, n) for (int i = (int)n; i >= 1; i--)
using namespace std;
typedef long long int ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
void fastios() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
}

static const int N = 10005;
vi p(N, -1);
vi d(N, 1);

void unite(int x, int y) {
  while (p[x] != -1) {
    x = p[x];
  }
  while (p[y] != -1) {
    y = p[y];
  }
  if (d[x] > d[y])
    p[y] = x;
  else if (d[x] < d[y])
    p[x] = y;
  else {
    p[y] = x;
    d[x]++;
  }
}

bool same(int x, int y) {
  while (p[x] != -1) {
    x = p[x];
  }
  while (p[y] != -1) {
    y = p[y];
  }
  if (x == y)
    return 1;
  else
    return 0;
}

int main() {
  fastios();
  int n, q;
  cin >> n >> q;
  Loop(i, q) {
    int com, x, y;
    cin >> com >> x >> y;
    if (com == 0)
      unite(x, y);
    else
      cout << same(x, y) << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define INT_MAX 2147483647
#define INT_MIN -2147483646
#define Loop(i, n) for (int i = 0; i < (int)n; i++)
#define Loop1(i, n) for (int i = 1; i <= (int)n; i++)
#define Loopr(i, n) for (int i = (int)n - 1; i >= 0; i--)
#define Loopr1(i, n) for (int i = (int)n; i >= 1; i--)
using namespace std;
typedef long long int ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
void fastios() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
}

static const int N = 10005;
vi p(N, -1);
vi d(N, 1);

void unite(int x, int y) {
  while (p[x] != -1) {
    x = p[x];
  }
  while (p[y] != -1) {
    y = p[y];
  }
  if (x == y)
    return;
  if (d[x] > d[y])
    p[y] = x;
  else if (d[x] < d[y])
    p[x] = y;
  else {
    p[y] = x;
    d[x]++;
  }
}

bool same(int x, int y) {
  while (p[x] != -1) {
    x = p[x];
  }
  while (p[y] != -1) {
    y = p[y];
  }
  if (x == y)
    return 1;
  else
    return 0;
}

int main() {
  fastios();
  int n, q;
  cin >> n >> q;
  Loop(i, q) {
    int com, x, y;
    cin >> com >> x >> y;
    if (com == 0)
      unite(x, y);
    else
      cout << same(x, y) << endl;
  }
  return 0;
}
Problem p02343 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <list>
#include <vector>
using namespace std;

void makeset(int n, vector<list<int>> &set) {
  set.resize(n);
  for (int i = 0; i < n; i++) {
    set[i].push_back(i);
  }
}

void unite(vector<list<int>> &set, int x, int y, int n) {
  list<int>::iterator itr;
  int idx1 = -1, idx2 = -1;
  for (int i = 0; i < n; i++) {
    if (set[i].empty())
      continue;
    for (itr = set[i].begin(); itr != set[i].end(); itr++) {
      if (*itr == x)
        idx1 = i;
      if (*itr == y)
        idx2 = i;
    }
    if (idx1 != -1 && idx2 != -1)
      break;
  }
  set[idx1].merge(set[idx2]);
  set[idx2].clear();
}

void same(vector<list<int>> &set, int x, int y, int n) {
  list<int>::iterator itr;
  int idx1 = -1, idx2 = -1;
  for (int i = 0; i < n; i++) {
    if (set[i].empty())
      continue;
    for (itr = set[i].begin(); itr != set[i].end(); itr++) {
      if (*itr == x)
        idx1 = i;
      if (*itr == y)
        idx2 = i;
    }
    if (idx1 != -1 && idx2 != -1)
      break;
  }
  if (idx1 == idx2)
    cout << '1' << endl;
  else
    cout << '0' << endl;
}

int main(void) {
  int n, q, x, y, c;
  cin >> n >> q;
  vector<list<int>> set;
  makeset(n, set);
  for (int i = 0; i < q; i++) {
    cin >> c >> x >> y;
    if (c == 0)
      unite(set, x, y, n);
    else
      same(set, x, y, n);
  }
}


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

void makeset(int n, vector<list<int>> &set) {
  set.resize(n);
  for (int i = 0; i < n; i++) {
    set[i].push_back(i);
  }
}

void unite(vector<list<int>> &set, int x, int y, int n) {
  list<int>::iterator itr;
  int idx1 = -1, idx2 = -1;
  for (int i = 0; i < n; i++) {
    if (set[i].empty())
      continue;
    for (itr = set[i].begin(); itr != set[i].end(); itr++) {
      if (*itr == x)
        idx1 = i;
      if (*itr == y)
        idx2 = i;
    }
    if (idx1 != -1 && idx2 != -1)
      break;
  }
  if (idx1 == idx2)
    return;
  set[idx1].merge(set[idx2]);
  set[idx2].clear();
}

void same(vector<list<int>> &set, int x, int y, int n) {
  list<int>::iterator itr;
  int idx1 = -1, idx2 = -1;
  for (int i = 0; i < n; i++) {
    if (set[i].empty())
      continue;
    for (itr = set[i].begin(); itr != set[i].end(); itr++) {
      if (*itr == x)
        idx1 = i;
      if (*itr == y)
        idx2 = i;
    }
    if (idx1 != -1 && idx2 != -1)
      break;
  }
  if (idx1 == idx2)
    cout << '1' << endl;
  else
    cout << '0' << endl;
}

int main(void) {
  int n, q, x, y, c;
  cin >> n >> q;
  vector<list<int>> set;
  makeset(n, set);
  for (int i = 0; i < q; i++) {
    cin >> c >> x >> y;
    if (c == 0)
      unite(set, x, y, n);
    else
      same(set, x, y, n);
  }
}
Problem p02343 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <stdio.h>
#define N 10010
int n;
int father[N];
int findfather(int x) { return x == father[x] ? x : findfather(father[x]); }
void init() {
  for (int i = 0; i < n; i++)
    father[i] = i;
}
int main() {
  int m;
  scanf("%d %d", &n, &m);
  init();
  for (int i = 0; i < m; i++) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    if (a == 0) {
      father[findfather(c)] = b;
    } else {
      printf("%s\n", findfather(b) == findfather(c) ? "1" : "0");
    }
  }
  return 0;
}


Fixed submission:
#include <stdio.h>
#define N 10010
int n;
int father[N];
int findfather(int x) { return x == father[x] ? x : findfather(father[x]); }
void init() {
  for (int i = 0; i < n; i++)
    father[i] = i;
}
int main() {
  int m;
  scanf("%d %d", &n, &m);
  init();
  for (int i = 0; i < m; i++) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    if (a == 0) {
      father[findfather(c)] = findfather(b);
    } else {
      printf("%s\n", findfather(b) == findfather(c) ? "1" : "0");
    }
  }
  return 0;
}
Problem p02343 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, -1, sizeof(mat))
#define N 101
struct edge {
  int to, cost;
};
int par[N];
int q, n;

void init(int n) { FOR(i, 0, n) par[i] = i; }

int root(int x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = root(par[x]);
  }
}

void unite(int x, int y) {
  x = root(x);
  y = root(y);
  if (x == y)
    return;
  par[x] = y;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> q;
  init(n);
  int com, x, y;
  FOR(i, 0, q) {
    cin >> com >> x >> y;
    if (com) {
      if (root(x) == root(y)) {
        cout << "1" << endl;
      } else {
        cout << "0" << endl;
      }
    } else {
      unite(x, y);
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, -1, sizeof(mat))
#define N 10000
struct edge {
  int to, cost;
};
int par[N];
int q, n;

void init(int n) { FOR(i, 0, n) par[i] = i; }

int root(int x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = root(par[x]);
  }
}

void unite(int x, int y) {
  x = root(x);
  y = root(y);
  if (x == y)
    return;
  par[x] = y;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> q;
  init(n);
  int com, x, y;
  FOR(i, 0, q) {
    cin >> com >> x >> y;
    if (com) {
      if (root(x) == root(y)) {
        cout << "1" << endl;
      } else {
        cout << "0" << endl;
      }
    } else {
      unite(x, y);
    }
  }
  return 0;
}
Problem p02343 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;
#define DEBUG(x) cout << #x << ": " << x << endl

#define PB push_back
#define MP make_pair
#define F first
#define S second
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define INT(a) int((a) + 1e-9)
int DX[4] = {1, 0, -1, 0};
int DY[4] = {0, 1, 0, -1};

typedef pair<int, int> Pii;

#define SUPnl 10002

int nl;
int p[SUPnl];

int init() {
  REP(ni, nl) { p[ni] = -1; }
}

int getRoot(int a) {
  while (true) {
    if (p[a] == -1)
      return a;
    a = p[a];
  }
}

bool same(int a, int b) {
  int pa = getRoot(a);
  int pb = getRoot(b);
  if (pa == pb) {
    p[b] = pa;
    return true;
  } else
    return false;
}

void unite(int a, int b) {
  a = getRoot(a);
  b = getRoot(b);
  if (a == b)
    return;
  p[b] = a;
}

int main() {
  int ol;
  cin >> nl >> ol;
  init();
  int ot, a, b;
  REP(oi, ol) {
    cin >> ot >> a >> b;
    if (ot == 0)
      unite(a, b);
    if (ot == 1)
      cout << same(a, b) << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;
#define DEBUG(x) cout << #x << ": " << x << endl

#define PB push_back
#define MP make_pair
#define F first
#define S second
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define INT(a) int((a) + 1e-9)
int DX[4] = {1, 0, -1, 0};
int DY[4] = {0, 1, 0, -1};

typedef pair<int, int> Pii;

#define SUPnl 10002

int nl;
int p[SUPnl];

int init() {
  REP(ni, nl) { p[ni] = -1; }
}

int getRoot(int a) {
  while (true) {
    if (p[a] == -1)
      return a;
    a = p[a];
  }
}

bool same(int a, int b) {
  int ra = getRoot(a);
  int rb = getRoot(b);
  if (ra != a)
    p[a] = ra;
  if (rb != b)
    p[b] = rb;
  return ra == rb;
}

void unite(int a, int b) {
  a = getRoot(a);
  b = getRoot(b);
  if (a == b)
    return;
  p[b] = a;
}

int main() {
  int ol;
  cin >> nl >> ol;
  init();
  int ot, a, b;
  REP(oi, ol) {
    cin >> ot >> a >> b;
    if (ot == 0)
      unite(a, b);
    if (ot == 1)
      cout << same(a, b) << endl;
  }
}
Problem p02343 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

typedef long long signed int ll;

constexpr ll NIL = -20000000000;

int u[10010];

int main() {
  for (int i = 0; i < 10010; i++)
    u[i] = -1;
  int n, q, x, y, com;
  cin >> n >> q;
  for (int i = 0; i < q; i++) {
    cin >> com >> x >> y;
    if (com) {
      int c[2] = {x, y};
      while (u[c[0]] != -1)
        c[0] = u[c[0]];
      while (u[c[1]] != -1)
        c[1] = u[c[1]];
      if (c[0] == c[1])
        cout << 1 << endl;
      else
        cout << 0 << endl;
    } else {
      int c[2] = {x, y};
      while (u[c[0]] != -1)
        c[0] = u[c[0]];
      while (u[c[1]] != -1)
        c[1] = u[c[1]];
      u[c[1]] = c[0];
    }
  }
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

typedef long long signed int ll;

constexpr ll NIL = -20000000000;

int u[10010];

int main() {
  for (int i = 0; i < 10010; i++)
    u[i] = -1;
  int n, q, x, y, com;
  cin >> n >> q;
  for (int i = 0; i < q; i++) {
    cin >> com >> x >> y;
    if (com) {
      int c[2] = {x, y};
      while (u[c[0]] != -1)
        c[0] = u[c[0]];
      while (u[c[1]] != -1)
        c[1] = u[c[1]];
      if (c[0] == c[1])
        cout << 1 << endl;
      else
        cout << 0 << endl;
    } else {
      int c[2] = {x, y};
      while (u[c[0]] != -1)
        c[0] = u[c[0]];
      while (u[c[1]] != -1)
        c[1] = u[c[1]];
      if (c[0] != c[1])
        u[c[1]] = c[0];
    }
  }
}
Problem p02343 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;
#define MAX_N 114514
vector<int> X[MAX_N];
int Group[MAX_N], N, Q, A, B, C;
int main() {
  cin >> N >> Q;
  for (int i = 0; i < MAX_N; i++) {
    X[i].push_back(i);
    Group[i] = i;
  }
  for (int i = 0; i < Q; i++) {
    cin >> A >> B >> C;
    int D = Group[B], E = Group[C];
    if (A == 0) {
      if (X[D].size() >= X[E].size()) {
        for (int j = 0; j < X[E].size(); j++) {
          Group[X[E][j]] = D;
          X[D].push_back(X[E][j]);
        }
        X[E].clear();
      } else if (X[D].size() < X[E].size()) {
        for (int j = 0; j < X[D].size(); j++) {
          Group[X[D][j]] = E;
          X[E].push_back(X[D][j]);
        }
        X[D].clear();
      }
    }
    if (A == 1) {
      if (D == E) {
        cout << "1" << endl;
      } else {
        cout << "0" << endl;
      }
    }
  }
  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
#define MAX_N 114514
vector<int> X[MAX_N];
int Group[MAX_N], N, Q, A, B, C;
int main() {
  cin >> N >> Q;
  for (int i = 0; i < MAX_N; i++) {
    X[i].push_back(i);
    Group[i] = i;
  }
  for (int i = 0; i < Q; i++) {
    cin >> A >> B >> C;
    int D = Group[B], E = Group[C];
    if (A == 0 && D != E) {
      if (X[D].size() >= X[E].size()) {
        for (int j = 0; j < X[E].size(); j++) {
          Group[X[E][j]] = D;
          X[D].push_back(X[E][j]);
        }
        X[E].clear();
      } else if (X[D].size() < X[E].size()) {
        for (int j = 0; j < X[D].size(); j++) {
          Group[X[D][j]] = E;
          X[E].push_back(X[D][j]);
        }
        X[D].clear();
      }
    }
    if (A == 1) {
      if (D == E) {
        cout << "1" << endl;
      } else {
        cout << "0" << endl;
      }
    }
  }
  return 0;
}
Problem p02344 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <stack>
#include <stdio.h>
using namespace std;
const int N = 1e5 + 10;
int p[N], val[N];
stack<int> s[N];
int find(int n) {
  if (p[n] == n)
    return n;
  return p[n] = find(p[n]);
}
int main() {
  int n, q, l, r, temp, a, b;
  scanf("%d%d", &n, &q);
  for (int i = 0; i < n; i++) {
    p[i] = i;
    val[i] = 0;
    s[i].push(i);
  }
  while (q--) {
    scanf("%d%d%d", &temp, &l, &r);
    if (temp == 1) {
      if (find(l) != find(r))
        printf("?\n");
      else
        printf("%d\n", val[r] - val[l]);
    } else {
      scanf("%d", &temp);
      a = find(l);
      b = find(r);
      if (s[a].size() > s[b].size()) {
        p[b] = a;
        temp += val[l] - val[r];
        while (!s[b].empty()) {
          val[s[b].top()] += temp;
          s[a].push(s[b].top());
          s[b].pop();
        }
      } else {
        p[a] = b;
        temp = val[r] - temp - val[l];
        while (!s[a].empty()) {
          val[s[a].top()] += temp;
          s[b].push(s[a].top());
          s[a].pop();
        }
      }
    }
  }
}


Fixed submission:
#include <stack>
#include <stdio.h>
using namespace std;
const int N = 1e5 + 10;
int p[N], val[N];
stack<int> s[N];
int find(int n) {
  if (p[n] == n)
    return n;
  return p[n] = find(p[n]);
}
int main() {
  int n, q, l, r, temp, a, b;
  scanf("%d%d", &n, &q);
  for (int i = 0; i < n; i++) {
    p[i] = i;
    val[i] = 0;
    s[i].push(i);
  }
  while (q--) {
    scanf("%d%d%d", &temp, &l, &r);
    if (temp == 1) {
      if (find(l) != find(r))
        printf("?\n");
      else
        printf("%d\n", val[r] - val[l]);
    } else {
      scanf("%d", &temp);
      a = find(l);
      b = find(r);
      if (a == b)
        continue;
      if (s[a].size() > s[b].size()) {
        p[b] = a;
        temp += val[l] - val[r];
        while (!s[b].empty()) {
          val[s[b].top()] += temp;
          s[a].push(s[b].top());
          s[b].pop();
        }
      } else {
        p[a] = b;
        temp = val[r] - temp - val[l];
        while (!s[a].empty()) {
          val[s[a].top()] += temp;
          s[b].push(s[a].top());
          s[a].pop();
        }
      }
    }
  }
}
Problem p02345 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, a) range(i, 0, a)
using namespace std;

int n, dat[(1LL << 17) - 1];

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

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

long long query(int a, int b, int k = 0, int l = 0, int r = n) {
  if (r <= a || b <= l)
    return (1LL << 31) - 1;

  if (a <= l && r <= b)
    return dat[k];

  long long vl = query(a, b, 2 * k + 1, l, (l + r) / 2),
            vr = query(a, b, 2 * k + 2, (l + r) / 2, r);
  return min(vl, vr);
}

int main() {
  int q;
  cin >> n >> q;
  init(n);
  while (q--) {
    long long a, b, c;
    cin >> a >> b >> c;
    if (a) {
      cout << query(b, c + 1) << endl;
    } else {
      updata(b, c);
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, a) range(i, 0, a)
using namespace std;

int n, dat[10000000];

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

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

long long query(int a, int b, int k = 0, int l = 0, int r = n) {
  if (r <= a || b <= l)
    return (1LL << 31) - 1;

  if (a <= l && r <= b)
    return dat[k];

  long long vl = query(a, b, 2 * k + 1, l, (l + r) / 2),
            vr = query(a, b, 2 * k + 2, (l + r) / 2, r);
  return min(vl, vr);
}

int main() {
  int q;
  cin >> n >> q;
  init(n);
  while (q--) {
    long long a, b, c;
    cin >> a >> b >> c;
    if (a) {
      cout << query(b, c + 1) << endl;
    } else {
      updata(b, c);
    }
  }
  return 0;
}
Problem p02345 (C++)

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

const int INF = (1LL << 31) - 1;

struct SegmentTree {
  int n;
  vector<int> v;

  SegmentTree(int n_, int x) : v(n_ * 4) {
    for (int i = 0; (1 << i) <= n_; ++i, n = 1 << i)
      ;
    for (int i = 0; i < n * 4; ++i)
      v[i] = x;
  }

  void set(int i, int x) {
    i += n - 1;
    v[i] = x;
    for (; i > 0;) {
      i = (i - 1) / 2;
      v[i] = min(v[2 * i + 1], v[2 * i + 2]);
    }
  }

  int find(int l, int r) { return find(l, r, 0, 0, n); }

  int find(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return INF;
    if (a <= l && r <= b) {
      return v[k];
    } else {
      int vl = find(a, b, k * 2 + 1, l, (l + r) / 2);
      int vr = find(a, b, k * 2 + 2, (l + r) / 2, r);
      return min(vl, vr);
    }
  }
};

int main() {
  int n, q;
  scanf("%d %d", &n, &q);
  SegmentTree st(n, INF);

  for (int i = 0; i < q; ++i) {
    // for (int i = 0; i < st.n * 2 - 1; ++i) printf(" %d", st.v[i]); puts("");
    int c, x, y;
    scanf("%d %d %d", &c, &x, &y);
    if (c == 0) {
      st.set(x, y);
    } else /*if (c == 1)*/ {
      printf("%d\n", st.find(x, y + 1));
    }
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;

const int INF = (1LL << 31) - 1;

struct SegmentTree {
  int n;
  vector<int> v;

  SegmentTree(int n_, int x) : v(n_ * 4) {
    for (int i = 0; (1 << i) <= n_; ++i, n = 1 << i)
      ;
    for (int i = 0; i < n_ * 4; ++i)
      v[i] = x;
  }

  void set(int i, int x) {
    i += n - 1;
    v[i] = x;
    for (; i > 0;) {
      i = (i - 1) / 2;
      v[i] = min(v[2 * i + 1], v[2 * i + 2]);
    }
  }

  int find(int l, int r) { return find(l, r, 0, 0, n); }

  int find(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return INF;
    if (a <= l && r <= b) {
      return v[k];
    } else {
      int vl = find(a, b, k * 2 + 1, l, (l + r) / 2);
      int vr = find(a, b, k * 2 + 2, (l + r) / 2, r);
      return min(vl, vr);
    }
  }
};

int main() {
  int n, q;
  scanf("%d %d", &n, &q);
  SegmentTree st(n, INF);

  for (int i = 0; i < q; ++i) {
    // for (int i = 0; i < st.n * 2 - 1; ++i) printf(" %d", st.v[i]); puts("");
    int c, x, y;
    scanf("%d %d %d", &c, &x, &y);
    if (c == 0) {
      st.set(x, y);
    } else /*if (c == 1)*/ {
      printf("%d\n", st.find(x, y + 1));
    }
  }

  return 0;
}
Problem p02345 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
#define INF 2147483647
int n, q, rmq[200000];
void update(int a, int b) {
  a += n - 1;
  rmq[a] = b;
  while (a > 0) {
    a = (a - 1) / 2;
    rmq[a] = min(rmq[2 * a + 1], rmq[2 * a + 2]);
  }
}
int query(int a, int b, int k, int s, int t) {
  if (a <= s && t <= b)
    return rmq[k];
  if (t < a || b < s)
    return INF;

  return min(query(a, b, k * 2 + 1, s, (t + s - 1) / 2),
             query(a, b, k * 2 + 2, (t + s + 1) / 2, t));
}
int main(void) {
  int n1;
  cin >> n1 >> q;

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

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

Fixed submission:
#include <iostream>
using namespace std;
#define INF 2147483647
int n, q, rmq[262144];
void update(int a, int b) {
  a += n - 1;
  rmq[a] = b;
  while (a > 0) {
    a = (a - 1) / 2;
    rmq[a] = min(rmq[2 * a + 1], rmq[2 * a + 2]);
  }
}
int query(int a, int b, int k, int s, int t) {
  if (a <= s && t <= b)
    return rmq[k];
  if (t < a || b < s)
    return INF;

  return min(query(a, b, k * 2 + 1, s, (t + s - 1) / 2),
             query(a, b, k * 2 + 2, (t + s + 1) / 2, t));
}
int main(void) {
  int n1;
  cin >> n1 >> q;

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

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

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

int main() {
  long n, q;
  scanf("%d %d", &n, &q);
  long A[100000];
  for (long i = 0; i < n; i++)
    A[i] = pow(2, 31) - 1;
  for (long i = 0; i < q; i++) {
    long c, x, y;
    scanf("%ld %ld %ld", &c, &x, &y);
    if (c == 0)
      A[x] = y;
    else
      printf("%ld\n", *min_element(A + x, A + y + 1));
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
using namespace std;

int main() {
  int n, q;
  scanf("%d %d", &n, &q);
  long A[100000];
  for (long i = 0; i < n; i++)
    A[i] = pow(2, 31) - 1;
  for (long i = 0; i < q; i++) {
    long c, x, y;
    scanf("%ld %ld %ld", &c, &x, &y);
    if (c == 0)
      A[x] = y;
    else
      printf("%ld\n", *min_element(A + x, A + y + 1));
  }
  return 0;
}
Problem p02345 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

int N, dat[200000];
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, int l, int r) {
  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 n, q;
  cin >> n >> q;
  init(n);
  rep(i, q) {
    int com, x, y;
    cin >> com >> x >> y;
    if (com)
      printf("%d\n", query(x, y + 1, 0, 0, N));
    else
      update(x, y);
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

int N, dat[300000];
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, int l, int r) {
  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 n, q;
  cin >> n >> q;
  init(n);
  rep(i, q) {
    int com, x, y;
    cin >> com >> x >> y;
    if (com)
      printf("%d\n", query(x, y + 1, 0, 0, N));
    else
      update(x, y);
  }
}
Problem p02345 (C++)

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

using namespace std;

const int max_n = 1 << 16;
const int max_array = max_n * 2 - 1;

const int INF = 2147483647;

int dat[max_array];

void init() {
  for (int i = 0; i < max_array; i++) {
    dat[i] = INF;
  }
}

void update(int k, int x) {
  k += max_n - 1;
  dat[k] = x;

  while (k > 0) {
    k = (k - 1) / 2;

    dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
  }
}

int find(int a, int b, int k, int l, int m) {

  if (m <= a || b <= l) {
    return INF;
  }
  if (a <= l && m <= b) {
    return dat[k];
  }
  int lv = find(a, b, k * 2 + 1, l, (l + m) / 2);
  int mv = find(a, b, k * 2 + 2, (l + m) / 2, m);
  return min(lv, mv);
}

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

  init();

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

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

using namespace std;

const int max_n = 1 << 17;
const int max_array = max_n * 2 - 1;

const int INF = 2147483647;

int dat[max_array];

void init() {
  for (int i = 0; i < max_array; i++) {
    dat[i] = INF;
  }
}

void update(int k, int x) {
  k += max_n - 1;
  dat[k] = x;

  while (k > 0) {
    k = (k - 1) / 2;

    dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
  }
}

int find(int a, int b, int k, int l, int m) {

  if (m <= a || b <= l) {
    return INF;
  }
  if (a <= l && m <= b) {
    return dat[k];
  }
  int lv = find(a, b, k * 2 + 1, l, (l + m) / 2);
  int mv = find(a, b, k * 2 + 2, (l + m) / 2, m);
  return min(lv, mv);
}

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

  init();

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define int long long
using namespace std;
typedef pair<int, int> P;

const int inf = 2147483647;
const int MAX = 1e5 + 10;

// RMQ
struct segtree {
  int N, dat[2 * MAX];
  segtree() {}
  segtree(int n) {
    N = 1;
    while (N < n)
      N *= 2;
    for (int i = 0; i < 2 * N - 1; i++)
      dat[i] = inf;
  }
  // update k th element
  void update(int k, int a) {
    k += N - 1; // leaf
    dat[k] = a;
    while (k > 0) {
      k = (k - 1) / 2;
      dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
  }
  // min [a, b)
  int query(int a, int b) { return query(a, b, 0, 0, N); }
  int query(int a, int b, int k, int l, int r) {
    if (r <= a or b <= l)
      return inf;
    if (a <= l and r <= b)
      return dat[k];
    int m = (l + r) / 2;
    return min(query(a, b, k * 2 + 1, l, m), query(a, b, k * 2 + 2, m, r));
  }
};

signed main() {
  int n, q;
  cin >> n >> q;
  segtree sg(n);
  rep(i, 0, q) {
    int c, x, y;
    cin >> c >> x >> y;
    if (c == 0) {
      sg.update(x, y);
    } else {
      cout << sg.query(x, y + 1) << endl;
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define int long long
using namespace std;
typedef pair<int, int> P;

const int inf = 2147483647;
const int MAX = 2e5 + 10;

// RMQ
struct segtree {
  int N, dat[2 * MAX];
  segtree() {}
  segtree(int n) {
    N = 1;
    while (N < n)
      N *= 2;
    for (int i = 0; i < 2 * N - 1; i++)
      dat[i] = inf;
  }
  // update k th element
  void update(int k, int a) {
    k += N - 1; // leaf
    dat[k] = a;
    while (k > 0) {
      k = (k - 1) / 2;
      dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
  }
  // min [a, b)
  int query(int a, int b) { return query(a, b, 0, 0, N); }
  int query(int a, int b, int k, int l, int r) {
    if (r <= a or b <= l)
      return inf;
    if (a <= l and r <= b)
      return dat[k];
    int m = (l + r) / 2;
    return min(query(a, b, k * 2 + 1, l, m), query(a, b, k * 2 + 2, m, r));
  }
};

signed main() {
  int n, q;
  cin >> n >> q;
  segtree sg(n);
  rep(i, 0, q) {
    int c, x, y;
    cin >> c >> x >> y;
    if (c == 0) {
      sg.update(x, y);
    } else {
      cout << sg.query(x, y + 1) << endl;
    }
  }
}