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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define FOR(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define RREP(i, a) for (int(i) = (a)-1; (i) >= 0; (i)--)
#define FORR(i, a, b) for (int(i) = (a)-1; (i) >= (b); (i)--)
#define DEBUG(C) cerr << #C << " = " << C << endl;
using LL = long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<LL>;
using VVL = vector<VL>;
using VD = vector<double>;
using VVD = vector<VD>;
using PII = pair<int, int>;
using PDD = pair<double, double>;
using PLL = pair<LL, LL>;
using VPII = vector<PII>;
template <typename T> using VT = vector<T>;
#define ALL(a) begin((a)), end((a))
#define RALL(a) rbegin((a)), rend((a))
#define SORT(a) sort(ALL((a)))
#define RSORT(a) sort(RALL((a)))
#define REVERSE(a) reverse(ALL((a)))
#define MP make_pair
#define FORE(a, b) for (auto &&a : (b))
#define FIND(s, e) ((s).find(e) != (s).end())
#define EB emplace_back
template <typename T> inline bool chmax(T &a, T b) {
  return (a < b ? a = b, true : false);
}
template <typename T> inline bool chmin(T &a, T b) {
  return (a > b ? a = b, true : false);
}

const int INF = 1e9;
const int MOD = INF + 7;
const LL LLINF = 1e18;
const long double EPS = 1e-9;

template <typename T = int, typename ctype = std::less<T>,
          T LIM = (std::is_same<ctype, std::less<T>>::value
                       ? std::numeric_limits<T>::max()
                       : std::numeric_limits<T>::min())>
class RangeUpdateQueryOfMinMax {
private:
  vector<T> data, lazy;
  vector<bool> valid, lazyFlag;
  ctype comp;
  const T initVal;
  int sz;
  void push(int k) {
    if (this->lazyFlag[k]) {
      this->valid[k] = true;
      this->data[k] = this->lazy[k];
      if ((k << 1) + 2 < (sz << 1) - 1) {
        this->lazy[(k << 1) + 1] = this->lazy[(k << 1) + 2] = this->lazy[k];
        this->lazyFlag[(k << 1) + 1] = this->lazyFlag[(k << 1) + 2] = true;
      }
      this->lazyFlag[k] = false;
    }
  }
  void update(int l, int r, int L, int R, int k, T val) {
    this->push(k);
    if (L <= l && r <= R) {
      this->lazy[k] = val;
      this->lazyFlag[k] = true;
      this->push(k);
    } else if (r < L || R < l) {
      return;
    } else if (r - l > 1) {
      const int mid = (l + r) >> 1;
      this->update(l, mid, L, R, (k << 1) + 1, val);
      this->update(mid, r, L, R, (k << 1) + 2, val);
      data[k] = std::min(data[(k << 1) + 1], data[(k << 1) + 2], comp);
    }
  }
  T query(int l, int r, int L, int R, int k) {
    this->push(k);
    if (L <= l && r <= R) {
      return this->data[k];
    } else if (r <= L || R <= l) {
      return this->initVal;
    } else {
      const int mid = (l + r) >> 1;
      T left = this->query(l, mid, L, R, (k << 1) + 1);
      T right = this->query(mid, r, L, R, (k << 1) + 2);
      return std::min(left, right, comp);
    }
  }

public:
  RangeUpdateQueryOfMinMax(const int n, T initval = LIM) : initVal(initval) {
    this->sz = 1;
    while (this->sz < n)
      this->sz <<= 1;
    this->data.resize((this->sz << 1) - 1, this->initVal);
    this->lazy.resize((this->sz << 1) - 1);
    this->valid.resize((this->sz << 1) - 1, false);
    this->lazyFlag.resize((this->sz << 1) - 1, false);
  }
  void update(int l, int r, T val) { this->update(0, sz, l, r, 0, val); }
  T query(int l, int r) { return this->query(0, sz, l, r, 0); }
  int &operator[](const int k) { return data[k]; }
  int size() { return this->sz; }
};

int main() {
  int n, q;
  cin >> n >> q;
  RangeUpdateQueryOfMinMax<int> ruq(n);
  while (q--) {
    // REP(i, ruq.size() * 2 - 1) cout<<ruq[i]<<" ";cout<<endl;
    int num;
    cin >> num;
    if (num) {
      int s, t;
      cin >> s >> t;
      cout << ruq.query(s, t + 1) << endl;
    } else {
      int s, t, x;
      cin >> s >> t >> x;
      ruq.update(s, t + 1, x);
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define FOR(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define RREP(i, a) for (int(i) = (a)-1; (i) >= 0; (i)--)
#define FORR(i, a, b) for (int(i) = (a)-1; (i) >= (b); (i)--)
#define DEBUG(C) cerr << #C << " = " << C << endl;
using LL = long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<LL>;
using VVL = vector<VL>;
using VD = vector<double>;
using VVD = vector<VD>;
using PII = pair<int, int>;
using PDD = pair<double, double>;
using PLL = pair<LL, LL>;
using VPII = vector<PII>;
template <typename T> using VT = vector<T>;
#define ALL(a) begin((a)), end((a))
#define RALL(a) rbegin((a)), rend((a))
#define SORT(a) sort(ALL((a)))
#define RSORT(a) sort(RALL((a)))
#define REVERSE(a) reverse(ALL((a)))
#define MP make_pair
#define FORE(a, b) for (auto &&a : (b))
#define FIND(s, e) ((s).find(e) != (s).end())
#define EB emplace_back
template <typename T> inline bool chmax(T &a, T b) {
  return (a < b ? a = b, true : false);
}
template <typename T> inline bool chmin(T &a, T b) {
  return (a > b ? a = b, true : false);
}

const int INF = 1e9;
const int MOD = INF + 7;
const LL LLINF = 1e18;
const long double EPS = 1e-9;

template <typename T = int, typename ctype = std::less<T>,
          T LIM = (std::is_same<ctype, std::less<T>>::value
                       ? std::numeric_limits<T>::max()
                       : std::numeric_limits<T>::min())>
class RangeUpdateQueryOfMinMax {
private:
  vector<T> data, lazy;
  vector<bool> valid, lazyFlag;
  ctype comp;
  const T initVal;
  int sz;
  void push(int k) {
    if (this->lazyFlag[k]) {
      this->valid[k] = true;
      this->data[k] = this->lazy[k];
      if (k * 2 + 2 < sz * 2 - 1) {
        this->lazy[k * 2 + 1] = this->lazy[k * 2 + 2] = this->lazy[k];
        this->lazyFlag[k * 2 + 1] = this->lazyFlag[k * 2 + 2] = true;
      }
      this->lazyFlag[k] = false;
    }
  }
  void update(int l, int r, int L, int R, int k, T val) {
    this->push(k);
    if (L <= l && r <= R) {
      this->lazy[k] = val;
      this->lazyFlag[k] = true;
      this->push(k);
    } else if (r < L || R < l) {
      return;
    } else if (r - l > 1) {
      const int mid = (l + r) / 2;
      this->update(l, mid, L, R, k * 2 + 1, val);
      this->update(mid, r, L, R, k * 2 + 2, val);
    }
  }
  T query(int l, int r, int L, int R, int k) {
    this->push(k);
    if (r < L || R < l) {
      return this->initVal;
    } else if (r - l == 1) {
      if (L <= l && r <= R) {
        return this->data[k];
      } else {
        return this->initVal;
      }
    } else {
      const int mid = (l + r) / 2;
      T left = this->query(l, mid, L, R, k * 2 + 1);
      T right = this->query(mid, r, L, R, k * 2 + 2);
      return std::min(left, right, comp);
    }
  }

public:
  RangeUpdateQueryOfMinMax(const int n, T initval = LIM) : initVal(initval) {
    this->sz = 1;
    while (this->sz < n)
      this->sz <<= 1;
    this->data.resize(this->sz * 2 - 1, LIM);
    this->lazy.resize(this->sz * 2 - 1);
    this->valid.resize(this->sz * 2 - 1, false);
    this->lazyFlag.resize(this->sz * 2 - 1, false);
  }
  void update(int l, int r, T val) { this->update(0, sz, l, r, 0, val); }
  T query(int l, int r) { return this->query(0, sz, l, r, 0); }
  int &operator[](const int k) { return data[k]; }
  int size() { return this->sz; }
};

int main() {
  int n, q;
  cin >> n >> q;
  RangeUpdateQueryOfMinMax<int> ruq(n);
  while (q--) {
    // REP(i, ruq.size() * 2 - 1) cout<<ruq[i]<<" ";cout<<endl;
    int num;
    cin >> num;
    if (num) {
      int s, t;
      cin >> s >> t;
      cout << ruq.query(s, t + 1) << endl;
    } else {
      int s, t, x;
      cin >> s >> t >> x;
      ruq.update(s, t + 1, x);
    }
  }
}