Problem p02345 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define mind(a, b) (a > b ? b : a)
#define maxd(a, b) (a > b ? a : b)
#define absd(x) (x < 0 ? -(x) : x)
#define pow2(x) ((x) * (x))
#define rep(i, n) for (int i = 0; i < n; ++i)
#define repr(i, n) for (int i = n - 1; i >= 0; --i)
#define repl(i, s, n) for (int i = s; i <= n; ++i)
#define replr(i, s, n) for (int i = n; i >= s; --i)
#define repf(i, s, n, j) for (int i = s; i <= n; i += j)
#define repe(e, obj) for (auto e : obj)

#define SP << " " <<
#define COL << " : " <<
#define COM << ", " <<
#define ARR << " -> " <<
#define PNT(STR) cout << STR << endl
#define POS(X, Y) "(" << X << ", " << Y << ")"
#define DEB(A) " (" << #A << ") " << A
#define DEBREP(i, n, val)                                                      \
  for (int i = 0; i < n; ++i)                                                  \
    cout << val << " ";                                                        \
  cout << endl
#define ALL(V) (V).begin(), (V).end()
#define INF 1000000007
#define INFLL 1000000000000000007LL
#define EPS 1e-9

typedef unsigned int uint;
typedef unsigned long ulong;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
#define P_TYPE int
typedef pair<P_TYPE, P_TYPE> P;
typedef pair<P, P_TYPE> PI;
typedef pair<P_TYPE, P> IP;
typedef pair<P, P> PP;
typedef priority_queue<P, vector<P>, greater<P>> pvqueue;

#define N 100007

class SegmentTree {
  const static ll inf = (1LL << 31) - 1;
  int n, n0;
  ll data[N];

public:
  SegmentTree(int n) {
    n0 = 1;
    while (n0 < n)
      n0 <<= 1;
    rep(i, 2 * n0) data[i] = inf;
  }

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

  ll query(int l, int r) {
    int l0 = l + n0, r0 = r + n0;
    ll s = inf;
    while (l0 < r0) {
      if (r0 & 1) {
        --r0;
        s = min(s, data[r0 - 1]);
      }
      if (l0 & 1) {
        s = min(s, data[l0 - 1]);
        ++l0;
      }
      l0 >>= 1;
      r0 >>= 1;
    }
    return s;
  }
};

int n, q;

int main() {
  cin >> n >> q;
  SegmentTree st(n);
  rep(i, q) {
    int t, x, y;
    cin >> t >> x >> y;
    switch (t) {
    case 0:
      st.update(x, y);
      break;
    case 1:
      cout << st.query(x, y + 1) << endl;
      break;
    }
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define mind(a, b) (a > b ? b : a)
#define maxd(a, b) (a > b ? a : b)
#define absd(x) (x < 0 ? -(x) : x)
#define pow2(x) ((x) * (x))
#define rep(i, n) for (int i = 0; i < n; ++i)
#define repr(i, n) for (int i = n - 1; i >= 0; --i)
#define repl(i, s, n) for (int i = s; i <= n; ++i)
#define replr(i, s, n) for (int i = n; i >= s; --i)
#define repf(i, s, n, j) for (int i = s; i <= n; i += j)
#define repe(e, obj) for (auto e : obj)

#define SP << " " <<
#define COL << " : " <<
#define COM << ", " <<
#define ARR << " -> " <<
#define PNT(STR) cout << STR << endl
#define POS(X, Y) "(" << X << ", " << Y << ")"
#define DEB(A) " (" << #A << ") " << A
#define DEBREP(i, n, val)                                                      \
  for (int i = 0; i < n; ++i)                                                  \
    cout << val << " ";                                                        \
  cout << endl
#define ALL(V) (V).begin(), (V).end()
#define INF 1000000007
#define INFLL 1000000000000000007LL
#define EPS 1e-9

typedef unsigned int uint;
typedef unsigned long ulong;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
#define P_TYPE int
typedef pair<P_TYPE, P_TYPE> P;
typedef pair<P, P_TYPE> PI;
typedef pair<P_TYPE, P> IP;
typedef pair<P, P> PP;
typedef priority_queue<P, vector<P>, greater<P>> pvqueue;

#define N 100007

class SegmentTree {
  const static ll inf = (1LL << 31) - 1;
  int n, n0;
  ll data[4 * N];

public:
  SegmentTree(int n) {
    n0 = 1;
    while (n0 < n)
      n0 <<= 1;
    rep(i, 2 * n0) data[i] = inf;
  }

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

  ll query(int l, int r) {
    int l0 = l + n0, r0 = r + n0;
    ll s = inf;
    while (l0 < r0) {
      if (r0 & 1) {
        --r0;
        s = min(s, data[r0 - 1]);
      }
      if (l0 & 1) {
        s = min(s, data[l0 - 1]);
        ++l0;
      }
      l0 >>= 1;
      r0 >>= 1;
    }
    return s;
  }
};

int n, q;

int main() {
  cin >> n >> q;
  SegmentTree st(n);
  rep(i, q) {
    int t, x, y;
    cin >> t >> x >> y;
    switch (t) {
    case 0:
      st.update(x, y);
      break;
    case 1:
      cout << st.query(x, y + 1) << endl;
      break;
    }
  }
  return 0;
}