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

#include <algorithm>
#include <complex>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define REP(i, m, n) for (int i = int(m); i < int(n); i++)
#define RREP(i, m, n) for (int i = int(n) - 1; i >= int(m); --i)
#define EACH(i, c) for (auto &(i) : c)
#define all(c) begin(c), end(c)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(begin(c), end(c))
#define pb emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
// #define int long long

#ifdef LOCAL
#define DEBUG(s) cout << (s) << endl
#define dump(x) cerr << #x << " = " << (x) << endl
#define BR cout << endl;
#else
#define DEBUG(s)                                                               \\
  do {                                                                         \\
  } while (0)
#define dump(x)                                                                \\
  do {                                                                         \\
  } while (0)
#define BR
#endif
using namespace std;

using UI = unsigned int;
using UL = unsigned long;
using LL = long long;
using ULL = unsigned long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VLL = vector<LL>;
using VVLL = vector<VLL>;
using VS = vector<string>;
using PII = pair<int, int>;
using VP = vector<PII>;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

void solve() {
  int h, w;
  cin >> h >> w;
  int a, b;
  set<PII> s;
  multiset<int> m;
  REP(i, 0, w) s.emplace(i, i);
  REP(i, 0, w) m.insert(0);
  s.emplace(1 << 30, 1 << 30);
  m.insert(1 << 30);
  REP(i, 0, h) {
    // dump("---");
    cin >> a >> b;
    --a;
    --b;
    // dump("dump");
    auto it = s.lower_bound(MP(a, 0));
    auto bt = it;
    // dump("dump");
    int lv = -1;
    while (it != s.end() && it->first <= b) {
      // dump(it->first);
      chmax(lv, it->second);
      int diff = it->first - it->second;
      // dump((diff));
      m.erase(m.lower_bound(diff));
      ++it;
    }
    // dump("dump");
    // if (bt != s.end()) {
    //     dump(bt->first);
    //     dump(bt->second);
    // }

    if (it != bt) {
      // --it;
      s.erase(bt, it);
    }
    // dump(a);
    // dump(b);
    // dump(lv);
    if (lv > -1 && b + 1 < w && s.lower_bound(MP(b + 1, 0))->first != b + 1) {
      // dump(lv);
      s.emplace(b + 1, lv);
      m.insert(b + 1 - lv);
    }
    // dump("dump");
    // dump(*m.begin());
    if ((*m.begin()) == 1 << 30)
      cout << -1 << endl;
    else
      cout << (*m.begin()) + i + 1 << endl;
    // for (auto &sv: s) dump(sv.first);
    // for (auto &v: m) dump(v);
  }
}

signed main() {
  solve();

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <complex>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define REP(i, m, n) for (int i = int(m); i < int(n); i++)
#define RREP(i, m, n) for (int i = int(n) - 1; i >= int(m); --i)
#define EACH(i, c) for (auto &(i) : c)
#define all(c) begin(c), end(c)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(begin(c), end(c))
#define pb emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
// #define int long long

#ifdef LOCAL
#define DEBUG(s) cout << (s) << endl
#define dump(x) cerr << #x << " = " << (x) << endl
#define BR cout << endl;
#else
#define DEBUG(s)                                                               \\
  do {                                                                         \\
  } while (0)
#define dump(x)                                                                \\
  do {                                                                         \\
  } while (0)
#define BR
#endif
using namespace std;

using UI = unsigned int;
using UL = unsigned long;
using LL = long long;
using ULL = unsigned long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VLL = vector<LL>;
using VVLL = vector<VLL>;
using VS = vector<string>;
using PII = pair<int, int>;
using VP = vector<PII>;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

// constexpr double EPS = 1e-10;
// constexpr double PI  = acos(-1.0);
// constexpr int INF = INT_MAX;
// constexpr int MOD = 1'000'000'007;
// inline void modAdd(LL &l, LL &r) {l = (l + r) % MOD;}

template <class T> inline T sqr(T x) { return x * x; }

void solve() {
  int h, w;
  cin >> h >> w;
  VI a(h), b(h);
  REP(i, 0, h) cin >> a[i] >> b[i];
  REP(i, 0, h) {
    a[i]--;
    b[i]--;
  }
  VI ans(h + 1, 1 << 30);
  VI l(h + 1, w);
  vector<bool> done(h + 1, false);
  RREP(i, 0, w) {
    int idx = i;
    int cnt = 0;
    if (idx >= a[0] && idx <= b[0])
      continue;
    REP(j, 0, h) {
      ++cnt;
      if (l[j + 1] <= idx)
        continue;
      if (j == h - 1 || idx < a[j + 1] || idx > b[j + 1]) {
        chmin(ans[j + 1], cnt);
        l[j + 1] = idx;
        continue;
      } else {
        int diff = b[j + 1] + 1 - idx;
        chmin(ans[j + 1], cnt);
        cnt += diff;
        idx = b[j + 1] + 1;
        l[j + 1] = idx;
      }
      if (idx >= w)
        break;
    }
    if (ans[h] == h)
      break;
  }

  REP(i, 1, h + 1) {
    if (ans[i] == 1 << 30)
      cout << -1 << endl;
    else
      cout << ans[i] << endl;
  }
}

signed main() {
  solve();

  return 0;
}