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

#include <bits/stdc++.h>
#define int long long int
#define MOD(x) ((x % MOD_N) + MOD_N) % MOD_N
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORE(i, a, b) for (int i = (a); i <= (b); ++i)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define RFORE(i, a, b) for (int i = (b); i >= (a); --i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, 0, n)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define SZ(c) (int)((c).size())
#define EACH(i, v) for (auto i = v.begin(); i != v.end(); ++i)
#define REACH(i, v) for (auto i = v.rbegin(); i != v.rend(); ++i)
#define LB(c, x) distance((c).begin(), lower_bound(ALL(c), x))
#define UB(c, x) distance((c).begin(), upper_bound(ALL(c), x))
#define COUNT(c, x) (lower_bound(ALL(c), x) - upper_bound(ALL(c), x))
#define UNIQUE(c)                                                              \\
  SORT(c);                                                                     \\
  (c).erase(unique(ALL(c)), (c).end());
#define COPY(c1, c2) copy(ALL(c1), (c2).begin())
#define EXIST(s, e) (bool)((s).find(e) != (s).end())
#define PB push_back
#define MP make_pair
#define DUMP(x) cerr << #x << " = " << (x) << endl;
#define NL cerr << endl;
using namespace std;
template <typename T, typename U> using P = pair<T, U>;
template <typename T> using V = vector<T>;
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> T sum(vector<T> &v) { return accumulate(ALL(v), T()); }
template <typename T> T sum(vector<T> &v, int a, int b) {
  return accumulate(v.begin() + a, v.begin() + b, T());
}
template <typename T> T max(vector<T> &v) { return *max_element(ALL(v)); }
template <typename T> T min(vector<T> &v) { return *max_element(ALL(v)); }
template <typename T> T max_index(vector<T> &v) {
  return distance((v).begin(), max_element(ALL(v)));
}
template <typename T> T min_index(vector<T> &v) {
  return distance((v).begin(), min_element(ALL(v)));
}

struct edge {
  int to, cost;
};

template <typename T> auto &operator<<(ostream &s, const vector<T> &v) {
  s << "[";
  bool a = 1;
  for (auto e : v) {
    s << (a ? "" : " ") << e;
    a = 0;
  }
  s << "]";
  return s;
}
template <typename T, typename U>
auto &operator<<(ostream &s, const pair<T, U> &p) {
  s << "(" << p.first << "," << p.second << ")";
  return s;
}
template <typename T> auto &operator<<(ostream &s, const set<T> &st) {
  s << "{";
  bool a = 1;
  for (auto e : st) {
    s << (a ? "" : " ") << e;
    a = 0;
  }
  s << "}";
  return s;
}
template <typename T, typename U>
auto &operator<<(ostream &s, const map<T, U> &m) {
  s << "{";
  bool a = 1;
  for (auto e : m) {
    s << (a ? "" : " ") << e.first << ":" << e.second;
    a = 0;
  }
  s << "}";
  return s;
}

const int INF = 1e18;
const int MOD_N = 1e9 + 7;

template <typename Monoid> struct SegmentTree {
  using F = function<Monoid(Monoid, Monoid)>;
  int sz;
  vector<Monoid> seg;
  const F f;
  const Monoid M1;
  SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1) {
    sz = 1;
    while (sz < n)
      sz <<= 1;
    seg.assign(2 * sz, M1);
  }
  void set(int k, const Monoid &x) { seg[k + sz] = x; }
  void build() {
    for (int k = sz - 1; k > 0; k--) {
      seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
    }
  }
  void update(int k, const Monoid &x) {
    k += sz;
    seg[k] = x;
    while (k >>= 1) {
      seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
    }
  }
  Monoid query(int a, int b) {
    Monoid L = M1, R = M1;
    for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
      if (a & 1)
        L = f(L, seg[a++]);
      if (b & 1)
        R = f(seg[--b], R);
    }
    return f(L, R);
  }
  Monoid operator[](const int &k) const { return seg[k + sz]; }
};

signed main() {
  int n, q;
  cin >> n >> q;
  V<int> c(q), x(q), y(q);
  REP(i, q) { cin >> c[i] >> x[i] >> y[i]; }

  SegmentTree<int> segt(
      n, [](int a, int b) { return min(a, b); }, (1LL << 31) - 1);
  segt.build();
  REP(i, q) {
    if (c[i] == 0) {
      segt.update(x[i], y[i]);
    } else {
      cout << segt.query(x[i], y[i] + 1) << endl;
    }
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define int long long int
#define MOD(x) ((x % MOD_N) + MOD_N) % MOD_N
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORE(i, a, b) for (int i = (a); i <= (b); ++i)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define RFORE(i, a, b) for (int i = (b); i >= (a); --i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, 0, n)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define SZ(c) (int)((c).size())
#define EACH(i, v) for (auto i = v.begin(); i != v.end(); ++i)
#define REACH(i, v) for (auto i = v.rbegin(); i != v.rend(); ++i)
#define LB(c, x) distance((c).begin(), lower_bound(ALL(c), x))
#define UB(c, x) distance((c).begin(), upper_bound(ALL(c), x))
#define COUNT(c, x) (lower_bound(ALL(c), x) - upper_bound(ALL(c), x))
#define UNIQUE(c)                                                              \\
  SORT(c);                                                                     \\
  (c).erase(unique(ALL(c)), (c).end());
#define COPY(c1, c2) copy(ALL(c1), (c2).begin())
#define EXIST(s, e) (bool)((s).find(e) != (s).end())
#define PB push_back
#define MP make_pair
#define DUMP(x) cerr << #x << " = " << (x) << endl;
#define NL cerr << endl;
using namespace std;
template <typename T, typename U> using P = pair<T, U>;
template <typename T> using V = vector<T>;
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> T sum(vector<T> &v) { return accumulate(ALL(v), T()); }
template <typename T> T sum(vector<T> &v, int a, int b) {
  return accumulate(v.begin() + a, v.begin() + b, T());
}
template <typename T> T max(vector<T> &v) { return *max_element(ALL(v)); }
template <typename T> T min(vector<T> &v) { return *max_element(ALL(v)); }
template <typename T> T max_index(vector<T> &v) {
  return distance((v).begin(), max_element(ALL(v)));
}
template <typename T> T min_index(vector<T> &v) {
  return distance((v).begin(), min_element(ALL(v)));
}

struct edge {
  int to, cost;
};

template <typename T> auto &operator<<(ostream &s, const vector<T> &v) {
  s << "[";
  bool a = 1;
  for (auto e : v) {
    s << (a ? "" : " ") << e;
    a = 0;
  }
  s << "]";
  return s;
}
template <typename T, typename U>
auto &operator<<(ostream &s, const pair<T, U> &p) {
  s << "(" << p.first << "," << p.second << ")";
  return s;
}
template <typename T> auto &operator<<(ostream &s, const set<T> &st) {
  s << "{";
  bool a = 1;
  for (auto e : st) {
    s << (a ? "" : " ") << e;
    a = 0;
  }
  s << "}";
  return s;
}
template <typename T, typename U>
auto &operator<<(ostream &s, const map<T, U> &m) {
  s << "{";
  bool a = 1;
  for (auto e : m) {
    s << (a ? "" : " ") << e.first << ":" << e.second;
    a = 0;
  }
  s << "}";
  return s;
}

const int INF = 1e18;
const int MOD_N = 1e9 + 7;

template <typename T, typename F> class SegmentTree {
private:
  int n;
  vector<T> node;
  F f;

public:
  SegmentTree(int sz, const F &func, T inf) : f(func) {
    n = 1;
    while (n < sz)
      n *= 2;
    node.resize(2 * n - 1, inf);
  }
  SegmentTree(const vector<T> &v, const F &func) : f(func) {
    int sz = v.size();
    n = 1;
    while (n < sz)
      n *= 2;
    node.resize(2 * n - 1);
    for (int i = 0; i < sz; i++)
      node[i + n - 1] = v[i];
    for (int i = n - 2; i >= 0; i--)
      node[i] = f(node[2 * i + 1], node[2 * i + 2]);
  }
  void update(int a, int b, T val, int k = 0, int l = 0, int r = -1) {
    // update [a, b)
    if (r < 0)
      r = n;
    if (r <= a || b <= l)
      return;
    if (a <= l && r <= b) {
      node[k] = val;
      return;
    }
    if (node[k] != -1) {
      node[2 * k + 1] = node[2 * k + 2] = node[k];
      node[k] = -1;
    }
    update(a, b, val, 2 * k + 1, l, (l + r) / 2);
    update(a, b, val, 2 * k + 2, (l + r) / 2, r);
  }
  T find(int a, int b, int k = 0, int l = 0, int r = -1) {
    // return f[a, b)
    if (r < 0)
      r = n;
    if (r <= a || b <= l)
      return INF;
    if (node[k] != -1)
      return node[k];
    // if(a <= l && r <= b) return node[k];
    int vl = find(a, b, 2 * k + 1, l, (l + r) / 2);
    int vr = find(a, b, 2 * k + 2, (l + r) / 2, r);
    return f(vl, vr);
  }
  void update(int i, T val) { update(i, i + 1, val); }
  T find(int i) { return find(i, i + 1); }
};
template <typename T> class RMQ {
private:
  SegmentTree<T, function<T(T, T)>> segt;

public:
  RMQ(int n, T inf)
      : segt(
            n, [](T a, T b) { return min(a, b); }, inf) {}
  void update(int i, T val) { segt.update(i, val); }
  T find(int a, int b) {
    // return min [a,b)
    return segt.find(a, b);
  }
};
template <typename T> class RUQ {
private:
  SegmentTree<T, function<T(T, T)>> segt;

public:
  RUQ(int n, T inf)
      : segt(
            n, [](T a, T b) { return min(a, b); }, inf) {}
  void update(int a, int b, T val) { segt.update(a, b, val); }
  T find(int i) { return segt.find(i); }
};
template <typename T> class RMQ_RUQ {
private:
  SegmentTree<T, function<T(T, T)>> segt;

public:
  RMQ_RUQ(int n, T inf)
      : segt(
            n, [](T a, T b) { return min(a, b); }, inf) {}
  void update(int a, int b, T val) { segt.update(a, b, val); }
  T find(int a, int b) {
    // return min [a,b)
    return segt.find(a, b);
  }
};

signed main() {
  int n, q;
  cin >> n >> q;
  V<int> c(q), x(q), y(q);
  REP(i, q) { cin >> c[i] >> x[i] >> y[i]; }

  RMQ<int> rmq(n, (1LL << 31) - 1);
  REP(i, q) {
    if (c[i] == 0) {
      rmq.update(x[i], y[i]);
    } else {
      cout << rmq.find(x[i], y[i] + 1) << endl;
    }
  }

  return 0;
}