Problem p02345 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _CRT_SECURE_NO_WARNINGS
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// #define int ll
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
#define all(c) begin(c), end(c)
#define range(i, a, b) for (ll i = a; i < ll(b); i++)
#define rep(i, b) range(i, 0, b)
#define rangei(i, a, b) for (ll a = a; i <= ll(b); i++)
#define repi(i, b) rangei(i, 1, b)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
template <class T> ostream &operator<<(ostream &os, vector<T> const &);
template <int n, class... T>
typename enable_if<(n >= sizeof...(T))>::type _ot(ostream &,
                                                  tuple<T...> const &) {}
template <int n, class... T>
typename enable_if<(n < sizeof...(T))>::type _ot(ostream &os,
                                                 tuple<T...> const &t) {
  os << (n == 0 ? "" : " ") << get<n>(t);
  _ot<n + 1>(os, t);
}
template <class... T> ostream &operator<<(ostream &os, tuple<T...> const &t) {
  _ot<0>(os, t);
  return os;
}
template <class T, class U>
ostream &operator<<(ostream &os, pair<T, U> const &p) {
  return os << "(" << p.first << ", " << p.second << ") ";
}
template <class T> ostream &operator<<(ostream &os, vector<T> const &v) {
  rep(i, v.size()) os << v[i] << (i + 1 == (int)v.size() ? "" : " ");
  return os;
}
#ifdef DEBUG
#define dump(...)                                                              \
  (cerr << #__VA_ARGS__ << " = " << mt(__VA_ARGS__) << " [" << __LINE__ << "]" \
        << endl)
#else
#define dump(...)
#endif
void fastios() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
#define endl '\n'
}
template <class T> size_t uniq(vector<T> &v) {
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
  return v.size();
}
template <class T> size_t uniq(T *l, size_t n) {
  sort(l, l + n);
  return unique(l, l + n) - l;
}
#define mems(arr, val) memset(arr, val, sizeof(arr));
int const mod = 1000000007;
int const inf = numeric_limits<int>::max();

// RMQ
template <class T> struct SegmentTree {
  int n;
  vector<T> dat;
  SegmentTree(int n_ = 0) : n(n_) {
    n = 1;
    while (n < n_)
      n <<= 1;
    dat.resize(n * 2, inf);
  }
  T query(int v, int w, int l, int r) {
    if (v >= (int)dat.size())
      return inf;
    if (r - l == w)
      return dat[v];
    T res = inf;
    res = std::min(res, query(v * 2, w / 2, l, min(r, w / 2)));
    res = std::min(res, query(v * 2 + 1, w / 2, max(0, l - w / 2), r - w / 2));
    return res;
  }
  void update(int i, T x) {
    i += n;
    dat[i] = x;
    while (i != 1) {
      dat[i >> 1] = min(dat[i], dat[i ^ 1]);
      i /= 2;
    }
  }
  T query(int l, int r) { return query(1, n, l, r); }
  size_t size() const { return n; }
};

int main() {
  fastios();
  int n, q;
  while (cin >> n >> q) {
    SegmentTree<int> st(n);
    rep(i, q) {
      char c;
      int x, y;
      cin >> c >> x >> y;
      if (c == '0') {
        st.update(x, y);
      } else {
        cout << st.query(x, y + 1) << endl;
      }
    }
  }
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// #define int ll
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
#define all(c) begin(c), end(c)
#define range(i, a, b) for (ll i = a; i < ll(b); i++)
#define rep(i, b) range(i, 0, b)
#define rangei(i, a, b) for (ll a = a; i <= ll(b); i++)
#define repi(i, b) rangei(i, 1, b)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
template <class T> ostream &operator<<(ostream &os, vector<T> const &);
template <int n, class... T>
typename enable_if<(n >= sizeof...(T))>::type _ot(ostream &,
                                                  tuple<T...> const &) {}
template <int n, class... T>
typename enable_if<(n < sizeof...(T))>::type _ot(ostream &os,
                                                 tuple<T...> const &t) {
  os << (n == 0 ? "" : " ") << get<n>(t);
  _ot<n + 1>(os, t);
}
template <class... T> ostream &operator<<(ostream &os, tuple<T...> const &t) {
  _ot<0>(os, t);
  return os;
}
template <class T, class U>
ostream &operator<<(ostream &os, pair<T, U> const &p) {
  return os << "(" << p.first << ", " << p.second << ") ";
}
template <class T> ostream &operator<<(ostream &os, vector<T> const &v) {
  rep(i, v.size()) os << v[i] << (i + 1 == (int)v.size() ? "" : " ");
  return os;
}
#ifdef DEBUG
#define dump(...)                                                              \
  (cerr << #__VA_ARGS__ << " = " << mt(__VA_ARGS__) << " [" << __LINE__ << "]" \
        << endl)
#else
#define dump(...)
#endif
void fastios() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
#define endl '\n'
}
template <class T> size_t uniq(vector<T> &v) {
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
  return v.size();
}
template <class T> size_t uniq(T *l, size_t n) {
  sort(l, l + n);
  return unique(l, l + n) - l;
}
#define mems(arr, val) memset(arr, val, sizeof(arr));
int const mod = 1000000007;
int const inf = numeric_limits<int>::max();

// RMQ
template <class T> struct SegmentTree {
  int n;
  vector<T> dat;
  SegmentTree(int n_ = 0) : n(n_) {
    n = 1;
    while (n < n_)
      n <<= 1;
    dat.resize(n * 2, inf);
  }
  T query(int v, int w, int l, int r) {
    if (v >= (int)dat.size())
      return inf;
    if (r - l == w)
      return dat[v];
    if (r <= l)
      return inf;
    T res = inf;
    res = std::min(res, query(v * 2, w / 2, l, min(r, w / 2)));
    res = std::min(res, query(v * 2 + 1, w / 2, max(0, l - w / 2), r - w / 2));
    return res;
  }
  void update(int i, T x) {
    i += n;
    dat[i] = x;
    while (i != 1) {
      dat[i >> 1] = min(dat[i], dat[i ^ 1]);
      i /= 2;
    }
  }
  T query(int l, int r) { return query(1, n, l, r); }
  size_t size() const { return n; }
};

int main() {
  fastios();
  int n, q;
  while (cin >> n >> q) {
    SegmentTree<int> st(n);
    rep(i, q) {
      char c;
      int x, y;
      cin >> c >> x >> y;
      if (c == '0') {
        st.update(x, y);
      } else {
        cout << st.query(x, y + 1) << endl;
      }
    }
  }
}