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

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define each(a, b) for (auto &(a) : (b))
#define all(v) (v).begin(), (v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)), v.erase(unique(all(v)), v.end())
#define cmx(x, y) x = max(x, y)
#define cmn(x, y) x = min(x, y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout << #x << " = " << (x) << endl
#define spair(p) cout << #p << ": " << p.fi << " " << p.se << endl
#define sar(a, n)                                                              \\
  cout << #a << ":";                                                           \\
  rep(pachico, n) cout << " " << a[pachico];                                   \\
  cout << endl
#define svec(v)                                                                \\
  cout << #v << ":";                                                           \\
  rep(pachico, v.size()) cout << " " << v[pachico];                            \\
  cout << endl
#define svecp(v)                                                               \\
  cout << #v << ":";                                                           \\
  each(pachico, v) cout << " {" << pachico.first << ":" << pachico.second      \\
                        << "}";                                                \\
  cout << endl
#define sset(s)                                                                \\
  cout << #s << ":";                                                           \\
  each(pachico, s) cout << " " << pachico;                                     \\
  cout << endl
#define smap(m)                                                                \\
  cout << #m << ":";                                                           \\
  each(pachico, m) cout << " {" << pachico.first << ":" << pachico.second      \\
                        << "}";                                                \\
  cout << endl

using namespace std;

typedef pair<int, int> P;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 100005;

#define getchar getchar_unlocked
#define putchar putchar_unlocked

inline int in() {
  int n, c;
  while ((c = getchar()) < '0')
    if (c == EOF)
      return -1;
  n = c - '0';
  while ((c = getchar()) >= '0')
    n = n * 10 + c - '0';
  return n;
}

inline void out(int n) {
  short res[11], i = 0;
  do {
    res[i++] = n % 10, n /= 10;
  } while (n);
  while (i)
    putchar(res[--i] + '0');
  putchar('\\n');
}

#define T int

class segtree {
private:
  int n, sz, id;
  vector<T> node, lazy;
  vector<bool> lazyFlag;

public:
  segtree(int n_) : sz(n_), id(0) {
    n = 1;
    while (n < sz)
      n *= 2, id++;
    node.resize(2 * n, numeric_limits<T>::max());
    lazy.resize(2 * n);
    lazyFlag.resize(2 * n, false);
  }
  void eval(int k) {
    if (lazyFlag[k]) {
      node[k] = lazy[k];
      if (k < n) {
        lazy[k * 2] = lazy[k * 2 + 1] = lazy[k];
        lazyFlag[k * 2] = lazyFlag[k * 2 + 1] = true;
      }
      lazyFlag[k] = false;
    }
  }
  void range(int a, int b, T x, int k = 1, int l = 0, int r = -1) {
    if (r < 0)
      r = n;
    eval(k);
    if (b <= l || r <= a)
      return;
    if (a <= l && r <= b) {
      lazy[k] = x;
      lazyFlag[k] = true;
      eval(k);
    } else {
      range(a, b, x, 2 * k, l, (l + r) / 2);
      range(a, b, x, 2 * k + 1, (l + r) / 2, r);
      node[k] = min(node[2 * k], node[2 * k + 1]);
    }
  }
  T query(int a, int b) {
    a += n, b += n - 1;
    for (int i = id; i > 0; i--)
      eval(a >> i), eval(b >> i);
    b++;
    T res1 = numeric_limits<T>::max(), res2 = numeric_limits<T>::max();
    while (a < b) {
      if (a & 1)
        eval(a), res1 = min(res1, node[a]), a++;
      if (b & 1)
        b--, eval(b), res2 = min(res2, node[b]);
      a >>= 1, b >>= 1;
    }
    return min(res1, res2);
  }
  void print() {
    rep(i, sz) cout << query(i, i + 1) << " ";
    cout << endl;
  }
};

int main() {
  int n = in(), q = in();
  segtree sg(n);
  rep(i, q) {
    int a = in();
    if (a == 0) {
      int x = in(), y = in(), z = in();
      sg.range(x, y + 1, z);
    } else {
      int x = in(), y = in();
      out(sg.query(x, y + 1));
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define each(a, b) for (auto(a) : (b))
#define all(v) (v).begin(), (v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)), v.erase(unique(all(v)), v.end())
#define cmx(x, y) x = max(x, y)
#define cmn(x, y) x = min(x, y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout << #x << " = " << (x) << endl
#define spair(p) cout << #p << ": " << p.fi << " " << p.se << endl
#define svec(v)                                                                \\
  cout << #v << ":";                                                           \\
  rep(kbrni, v.size()) cout << " " << v[kbrni];                                \\
  cout << endl
#define sset(s)                                                                \\
  cout << #s << ":";                                                           \\
  each(kbrni, s) cout << " " << kbrni;                                         \\
  cout << endl
#define smap(m)                                                                \\
  cout << #m << ":";                                                           \\
  each(kbrni, m) cout << " {" << kbrni.first << ":" << kbrni.second << "}";    \\
  cout << endl

using namespace std;

typedef pair<int, int> P;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 100005;

template <typename T> class RBST {
public:
  struct node {
    T val, kind, lazy, al;
    int st_size; // ??¨?????¨????????????
    node *left;
    node *right;
    bool lazy_flag;
    node(){};
    node(T v, T k)
        : val(v), kind(k), al(v), left(nullptr), right(nullptr), st_size(1),
          lazy(0), lazy_flag(false) {}
    ~node() {
      delete left;
      delete right;
    }
  };
  using pnn = pair<node *, node *>;
  node *root;
  T id1, id2; // opr1,opr2????????????
  //?????¨?????????????????????(sum?????????)
  T opr1(T l, T r) { return min(l, r); }
  //?????¶???????????????(add?????????)
  T opr2(T l, T r) { return r; }
  RBST() : root(nullptr), id1(numeric_limits<T>::max()), id2(0) {}
  RBST(vector<T> &vec) : root(nullptr), id1(numeric_limits<T>::max()), id2(0) {
    rep(i, len(vec)) { insert(i, vec[i]); }
  }
  int size(node *t) { return t ? t->st_size : 0; }
  T que(node *t) {
    push(t);
    return t ? t->al : id1;
  }
  //?????¶??????
  node *push(node *t) {
    if (!t)
      return t;
    if (t->lazy_flag) {
      if (t->left) {
        t->left->lazy_flag = true;
        t->left->lazy = opr2(t->left->lazy, t->lazy);
      }
      if (t->right) {
        t->right->lazy_flag = true;
        t->right->lazy = opr2(t->right->lazy, t->lazy);
      }
      t->val = opr2(t->val, t->lazy);
      t->al = opr1(que(t->left), opr1(t->val, que(t->right)));
      t->lazy = id2;
      t->lazy_flag = false;
    }
    return t;
  }
  node *update(node *t) {
    push(t);
    node *l = t->left;
    node *r = t->right;
    t->st_size = size(l) + size(r) + 1;
    push(l), push(r);
    t->al = opr1(opr1(que(l), que(r)), t->val);
    return t;
  }
  unsigned rnd() {
    static unsigned x = 123456789, y = 362436069, z = 521288629, w = 86675123;
    unsigned t = (x ^ (x << 11));
    x = y, y = z, z = w;
    return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));
  }
  node *merge(node *l, node *r) {
    if (!l || !r)
      return (!l) ? r : l;
    update(l), update(r);
    if (rnd() % (size(l) + size(r)) < (unsigned)size(l)) {
      l->right = merge(l->right, r);
      return update(l);
    } else {
      r->left = merge(l, r->left);
      return update(r);
    }
  }
  pnn split(node *t,
            int k) { //??¨???????????????(k,n-k)??¨???????????????????????????
    if (!t)
      return pnn(nullptr, nullptr);
    update(t);
    if (k <= size(t->left)) {
      pnn s = split(t->left, k);
      t->left = s.second;
      return pnn(s.first, update(t));
    } else {
      pnn s = split(t->right, k - size(t->left) - 1);
      t->right = s.first;
      return pnn(update(t), s.second);
    }
  }
  node *insert(node *t, int k, node *u) {
    pnn s = split(t, k);
    return merge(merge(s.first, u), s.second);
  }
  // 0_indexed??§k???insert
  void insert(int k, T val, T num) {
    root = insert(root, k, new node(val, num));
  }
  pnn erase(node *t, int k) {
    pnn sr = split(t, k + 1);
    pnn sl = split(sr.first, k);
    return pnn(merge(sl.first, sr.second), sl.second);
  }
  // 0_indexed??§k??????erase
  void erase(int k) {
    node *p;
    tie(root, p) = erase(root, k);
    p->left = p->right = nullptr;
    delete p;
  }
  pair<T, T> get(int k) {
    auto sr = split(root, k + 1);
    auto sl = split(sr.first, k);
    auto lr = sl.second;
    pair<T, T> res = make_pair(lr->val, lr->kind);
    root = merge(merge(sl.first, lr), sr.second);
    return res;
  }
  void range(int l, int r, T val) {
    auto sr = split(root, r);
    auto sl = split(sr.first, l);
    auto lr = sl.second;
    lr->lazy = val;
    lr->lazy_flag = true;
    root = merge(merge(sl.first, lr), sr.second);
  }
  T query(int l, int r) {
    auto sr = split(root, r);
    auto sl = split(sr.first, l);
    auto lr = sl.second;
    T res = que(lr);
    root = merge(merge(sl.first, lr), sr.second);
    return res;
  }
};

int main() {
  int n, q;
  cin >> n >> q;
  RBST<int> rb;
  rep(i, n) { rb.insert(i, INT_MAX, 0); }
  rep(i, q) {
    // rep(i,rb.size(rb.root)){
    //     spair(rb.get(i));
    // }
    // cout << "\\n";
    int a;
    cin >> a;
    if (a == 0) {
      int u, v, w;
      cin >> u >> v >> w;
      rb.range(u, v + 1, w);
    } else {
      int u, v;
      cin >> u >> v;
      cout << rb.query(u, v + 1) << "\\n";
    }
  }
  return 0;
}