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

#include <bits/stdc++.h>
using namespace std;

#define FOR(i, l, r) for (int i = (int)(l); i < (int)(r); i++)
#define ALL(x) x.begin(), x.end()
template <typename T> bool chmax(T &a, const T &b) {
  return a < b ? (a = b, true) : false;
}
template <typename T> bool chmin(T &a, const T &b) {
  return b < a ? (a = b, true) : false;
}
typedef long long ll;

const int BUCKET = 400, SIZE = 300;
int N, Q;
bool updateFlag[BUCKET];
ll lazyUpdate[BUCKET];
ll bucketMin[BUCKET];
ll A[100000];
const ll INF = (1ll << 31) - 1;

void update(int l, int r, ll val) {
  FOR(i, 0, BUCKET) {
    int bucketL = i * SIZE, bucketR = (i + 1) * SIZE;
    if (r <= bucketL || bucketR <= l)
      continue;
    if (l <= bucketL && bucketR <= r) {
      lazyUpdate[i] = val;
      updateFlag[i] = true;
      continue;
    }
    if (updateFlag[i]) {
      FOR(j, bucketL, bucketR) { A[j] = lazyUpdate[i]; }
      updateFlag[i] = false;
    }
    bucketMin[i] = INF;
    FOR(j, bucketL, bucketR) {
      if (l <= j && j < r) {
        A[j] = val;
      }
      chmin(bucketMin[i], A[j]);
    }
  }
}

ll getMin(int l, int r) {
  ll res = INF;
  FOR(i, 0, BUCKET) {
    int bucketL = i * SIZE, bucketR = (i + 1) * SIZE;
    if (r <= bucketL || bucketR <= l)
      continue;
    if (l <= bucketL && bucketR <= r) {
      if (updateFlag[i]) {
        chmin(res, lazyUpdate[i]);
      } else {
        chmin(res, bucketMin[i]);
      }
      continue;
    }
    if (updateFlag[i]) {
      FOR(j, bucketL, bucketR) { A[j] = lazyUpdate[i]; }
      updateFlag[i] = false;
    }
    bucketMin[i] = INF;
    FOR(j, bucketL, bucketR) {
      if (l <= j && j < r) {
        chmin(res, A[j]);
      }
      chmin(bucketMin[i], A[j]);
    }
  }
  return res;
}

int main() {
  scanf("%d%d", &N, &Q);
  vector<ll> ans;
  fill(A, A + N, INF);
  fill(bucketMin, bucketMin + BUCKET, INF);
  FOR(i, 0, Q) {
    int com;
    scanf("%d", &com);
    if (com == 0) {
      int s, t, x;
      scanf("%d%d%d", &s, &t, &x);
      update(s, t + 1, x);
    } else {
      int s, t;
      scanf("%d%d", &s, &t);
      ans.push_back(getMin(s, t + 1));
    }
  }

  FOR(i, 0, ans.size()) { printf("%lld\\n", ans[i]); }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define FOR(i, l, r) for (int i = (int)(l); i < (int)(r); i++)
#define ALL(x) x.begin(), x.end()
template <typename T> bool chmax(T &a, const T &b) {
  return a < b ? (a = b, true) : false;
}
template <typename T> bool chmin(T &a, const T &b) {
  return b < a ? (a = b, true) : false;
}
typedef long long ll;

const int BUCKET = 400, SIZE = 300;
int N, Q;
bool updateFlag[BUCKET];
ll lazyUpdate[BUCKET];
ll bucketMin[BUCKET];
ll A[100000];
const ll INF = (1ll << 31) - 1;

void update(int l, int r, ll val) {
  FOR(i, 0, BUCKET) {
    int bucketL = i * SIZE, bucketR = (i + 1) * SIZE;
    if (r <= bucketL || bucketR <= l)
      continue;
    if (l <= bucketL && bucketR <= r) {
      lazyUpdate[i] = val;
      updateFlag[i] = true;
      continue;
    }
    if (updateFlag[i]) {
      FOR(j, bucketL, bucketR) { A[j] = lazyUpdate[i]; }
      updateFlag[i] = false;
    }
    bucketMin[i] = INF;
    FOR(j, bucketL, bucketR) {
      if (l <= j && j < r) {
        A[j] = val;
      }
      chmin(bucketMin[i], A[j]);
    }
  }
}

ll getMin(int l, int r) {
  ll res = INF;
  FOR(i, 0, BUCKET) {
    int bucketL = i * SIZE, bucketR = (i + 1) * SIZE;
    if (r <= bucketL || bucketR <= l)
      continue;
    if (l <= bucketL && bucketR <= r) {
      if (updateFlag[i]) {
        chmin(res, lazyUpdate[i]);
      } else {
        chmin(res, bucketMin[i]);
      }
    }
    if (updateFlag[i]) {
      FOR(j, bucketL, bucketR) { A[j] = lazyUpdate[i]; }
      updateFlag[i] = false;
    }
    bucketMin[i] = INF;
    FOR(j, bucketL, bucketR) {
      if (l <= j && j < r) {
        chmin(res, A[j]);
      }
      chmin(bucketMin[i], A[j]);
    }
  }
  return res;
}

int main() {
  scanf("%d%d", &N, &Q);
  vector<ll> ans;
  fill(A, A + N, INF);
  fill(bucketMin, bucketMin + BUCKET, INF);
  FOR(i, 0, Q) {
    int com;
    scanf("%d", &com);
    if (com == 0) {
      int s, t, x;
      scanf("%d%d%d", &s, &t, &x);
      update(s, t + 1, x);
    } else {
      int s, t;
      scanf("%d%d", &s, &t);
      ans.push_back(getMin(s, t + 1));
    }
  }

  FOR(i, 0, ans.size()) { printf("%lld\\n", ans[i]); }

  return 0;
}