alexjercan/bugnet huggingface dataset:
Problem p02345 (C++) — buggy submission, verdict: 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++) {
      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;
  }
}

Error: 0

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++) {
      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, verdict: 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[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;
}


Error: 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[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;
}
Problem p02345 (C++) — buggy submission, verdict: 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));
  }
}

Error: -11

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, verdict: Time Limit Exceeded

#include <cmath>
#include <cstdio>
#define mid ((left + right) >> 1)
#define lson rt << 1, left, mid
#define rson rt << 1 | 1, mid + 1, right
#define INF 2147483647

int min(int x, int y) { return x > y ? y : x; }

int n, q;
int node[100000 << 2];

void init(int n_) {
  for (int i = 0; i <= (n_ << 2); i++)
    node[i] = INF;
}

void pushup(int rt) { node[rt] = min(node[rt << 1 | 1], node[rt << 1]); }

void update1(int rt, int left, int right, int target, int value) {
  if (left == right) {
    node[rt] = value;
    return;
  }
  if (target <= mid)
    update1(lson, target, value);
  else if (target > mid)
    update1(rson, target, value);
  pushup(rt);
}

void update2(int target, int value) {
  int v = target + n;
  node[v] = value;
  v /= 2;
  while (v > 0) {
    node[v] = min(node[2 * v], node[2 * v + 1]);
    v /= 2;
  }
}

int query(int rt, int left, int right, int L, int R) {
  // printf("%d %d %d %d %d\\n",rt,left,right,L,R);
  if (left == L && R == right)
    return node[rt];
  if (R < left && right < L)
    return INF;
  if (mid >= R)
    query(lson, L, R);
  else if (mid < L)
    query(rson, L, R);
  else
    return min(query(lson, L, mid), query(rson, mid + 1, R));
}

int main() {
  init(100000);
  while (~scanf("%d%d", &n, &q)) {
    for (int i = 0; i < q; i++) {
      int a, b, c;
      scanf("%d%d%d", &a, &b, &c);
      if (a == 1)
        printf("%d\\n", query(1, 1, n, b + 1, c + 1));
      else
        update1(1, 1, n, b + 1, c);
    }
  }
  return 0;
}
/*

3 7
0 0 1
0 1 2
0 2 3
1 0 0
1 0 2
1 1 2
1 0 1

*/

Error: TLE

Fixed submission:

#include <cmath>
#include <cstdio>
#define mid ((left + right) >> 1)
#define lson rt << 1, left, mid
#define rson rt << 1 | 1, mid + 1, right
#define INF 2147483647

int min(int x, int y) { return x > y ? y : x; }

int n, q;
int node[100000 << 2];

void init(int n_) {
  for (int i = 0; i <= (n_ << 2); i++)
    node[i] = INF;
}

void pushup(int rt) { node[rt] = min(node[rt << 1 | 1], node[rt << 1]); }

void update(int rt, int left, int right, int target, int value) {
  if (left == right) {
    node[rt] = value;
    return;
  }
  if (target <= mid)
    update(lson, target, value);
  else if (target > mid)
    update(rson, target, value);
  pushup(rt);
}

int query(int rt, int left, int right, int L, int R) {
  // printf("%d %d %d %d %d\\n",rt,left,right,L,R);
  if (left == L && R == right)
    return node[rt];
  if (R < left && right < L)
    return INF;
  if (mid >= R)
    query(lson, L, R);
  else if (mid < L)
    query(rson, L, R);
  else
    return min(query(lson, L, mid), query(rson, mid + 1, R));
}

int main() {
  while (~scanf("%d%d", &n, &q)) {
    init(n);
    for (int i = 0; i < q; i++) {
      int a, b, c;
      scanf("%d%d%d", &a, &b, &c);
      if (a == 1)
        printf("%d\\n", query(1, 1, n, b + 1, c + 1));
      else
        update(1, 1, n, b + 1, c);
    }
  }
  return 0;
}
/*

3 7
0 0 1
0 1 2
0 2 3
1 0 0
1 0 2
1 1 2
1 0 1

*/
Problem p02345 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
#include <climits>
#include <stdio.h>

// Binary heap structure.
int *H; // H[1], H[2], ... , H[n], H[n+1], H[n+2], ... , H[2n].

// keep on condition: H[x] = min(H[2x], H[2x+1]). O(log(n)).
void update(int x, int y, int n) {
  x++;
  for (x += n, H[x] = y; x > 1; x >>= 1) // x >>= 1 means x /= 2.
    H[x >> 1] =
        min(H[x], H[x ^ 1]); // x ^ 1: x = 2m then x + 1, x = 2m + 1 then x - 1.
  return;
}

// binary search. O(log(n)). very fast.
int find(int x, int y, int n) {
  int least = INT_MAX; // x, y starts from leaf, usually.
  x++;
  y++;
  for (x += n, y += n; x <= y; x = ((x + 1) >> 1), y = ((y - 1) >> 1)) {
    if (x & 1) {
      least = min(least, H[x]);
    }
    if (!(y & 1)) {
      least = min(least, H[y]);
    }
  }
  return least;
}

int main() {
  H = new int[1 << 18];
  int n, q;
  scanf("%d %d", &n, &q);
  int cmd, x, y;

  fill(H, H + 2 * n + 1, INT_MAX);

  while (q) {
    scanf("%d %d %d", &cmd, &x, &y);
    if (cmd) {
      printf("%d\\n", find(x, y, n));
    } else {
      update(x, y, n);
    }
    q--;
  };
  delete[] H;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
#include <stdio.h>

int main() {
  int i, n, q;
  int *A;
  scanf("%d %d", &n, &q);
  A = new int[n];
  fill(A, A + n, (1 << 31) - 1);

  int cmd, x, y, min;
  while (q) {
    scanf("%d %d %d", &cmd, &x, &y);
    if (cmd) {
      min = A[x];
      for (i = x + 1; i <= y; i++) {
        if (min > A[i])
          min = A[i];
      }
      printf("%d\\n", min);
    } else {
      A[x] = y;
    }
    q--;
  };
  delete[] A;

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

#include <cmath>
#include <iostream>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define INF pow(2, 31) - 1
#define B 300
using namespace std;

long long n, q, com, x, y;
vector<long long> dt;
vector<long long> backet;

void find(long long a, long long b) {
  long long res = INF;
  for (long long i = a; i < b + 1;) {
    if (i % B == 0 && i + B - 1 <= b) {
      res = min(res, backet[i / B]);
      i += B;
    } else {
      res = min(res, dt[i]);
      i++;
    }
  }
  cout << res << endl;
}

void update(long long i, long long x) {
  dt[i] = x;
  long long res = INF;
  for (long long j = i - i % B; j < min(n, i - i % B + B); j++) {
    res = min(res, dt[j]);
  }
  backet[i / B] = res;
}

int main() {
  cin >> n >> q;
  rep(i, n) dt.push_back(INF);
  rep(i, n / B + 1) backet.push_back(INF);
  rep(i, q) {
    cin >> com >> x >> y;
    if (com == 0)
      update(x, y);
    else
      find(x, y);
  }
  return 0;
}

Error: -11

Fixed submission:

#include <cmath>
#include <iostream>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define INF pow(2, 31) - 1
#define B 300
using namespace std;

int n, q, com, x, y;
vector<int> dt;
vector<int> backet;

void find(int a, int b) {
  int res = INF;
  for (long long i = a; i < b + 1;) {
    if (i % B == 0 && i + B - 1 <= b) {
      res = min(res, backet[i / B]);
      i += B;
    } else {
      res = min(res, dt[i]);
      i++;
    }
  }
  cout << res << endl;
}

void update(int i, int x) {
  dt[i] = x;
  int res = INF;
  for (long long j = i - i % B; j < min(n, i + B); j++) {
    res = min(res, dt[j]);
  }
  backet[i / B] = res;
}

int main() {
  cin >> n >> q;
  rep(i, n) dt.push_back(INF);
  rep(i, n / B) backet.push_back(INF);
  rep(i, q) {
    cin >> com >> x >> y;
    if (com == 0)
      update(x, y);
    else
      find(x, y);
  }
  return 0;
}
Problem p02345 (C++) — buggy submission, verdict: 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[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);
}

Error: 0

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[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);
}
Problem p02345 (C++) — buggy submission, verdict: Runtime Error

#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);
}

Error: 0

Fixed submission:

#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);
}
Problem p02345 (C++) — buggy submission, verdict: Runtime Error

#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);
}

Error: 0

Fixed submission:

#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);
}