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

#include <algorithm>
#include <iostream>
#include <numeric>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

#define REP(i, a, b) for (int i = int(a); i < int(b); i++)
using namespace std;
using ll = long long int;
using P = pair<ll, ll>;

// clang-format off
#ifdef _DEBUG_
#define dump(...) do{ cerr << __LINE__ << ":\\t" << #__VA_ARGS__ << " = "; PPPPP(__VA_ARGS__); cerr << endl; } while(false)
template<typename T> void PPPPP(T t) { cerr << t; }
template<typename T, typename... S> void PPPPP(T t, S... s) { cerr << t << ", "; PPPPP(s...); }
#else
#define dump(...) do{ } while(false)
#endif
template<typename T> vector<T> make_v(size_t a, T b) { return vector<T>(a, b); }
template<typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v(ts...))>(a, make_v(ts...)); }
template<typename T> bool chmin(T &a, T b) { if (a > b) {a = b; return true; } return false; }
template<typename T> bool chmax(T &a, T b) { if (a < b) {a = b; return true; } return false; }
template<typename T> void print(T a) { cout << a << endl; }
template<typename T, typename... Ts> void print(T a, Ts... ts) { cout << a << ' '; print(ts...); }
template<typename T> istream &operator,(istream &in, T &t) { return in >> t; }
// clang-format on

#include <set>

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int h, w;
  cin, h, w;
  set<P> s1, s2;
  REP(i, 0, w) {
    s1.emplace(i, 0);
    s2.emplace(0, i);
  }
  const int inf = 1 << 29;
  vector<int> m(w, 0);
  REP(i, 0, h) {
    int a, b;
    cin, a, b;
    a--;
    b--;
    auto it1 = s1.lower_bound(P(a, -1));
    auto it2 = s1.lower_bound(P(b, inf));
    P maxP(-1, -1);
    while (it1 != it2) {
      chmax(maxP, *it1);
      auto [s, t] = *it1;
      it1 = s1.erase(it1);
      s2.erase(P(t, s));
      m[s] = inf;
    }
    if (maxP.first != -1 && b + 1 < w) {
      auto [s, t] = maxP;
      int cost = t + b + 1 - s;
      if (m[b + 1] > cost) {
        s1.emplace(b + 1, cost);
        s2.emplace(cost, b + 1);
        m[b + 1] = cost;
      }
    }

    if (s2.size() == 0) {
      print(-1);
    } else {
      auto [s, t] = *s2.begin();
      print(s + i + 1);
    }
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <numeric>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

#define REP(i, a, b) for (int i = int(a); i < int(b); i++)
using namespace std;
using ll = long long int;
using P = pair<ll, ll>;

// clang-format off
#ifdef _DEBUG_
#define dump(...) do{ cerr << __LINE__ << ":\\t" << #__VA_ARGS__ << " = "; PPPPP(__VA_ARGS__); cerr << endl; } while(false)
template<typename T> void PPPPP(T t) { cerr << t; }
template<typename T, typename... S> void PPPPP(T t, S... s) { cerr << t << ", "; PPPPP(s...); }
#else
#define dump(...) do{ } while(false)
#endif
template<typename T> vector<T> make_v(size_t a, T b) { return vector<T>(a, b); }
template<typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v(ts...))>(a, make_v(ts...)); }
template<typename T> bool chmin(T &a, T b) { if (a > b) {a = b; return true; } return false; }
template<typename T> bool chmax(T &a, T b) { if (a < b) {a = b; return true; } return false; }
template<typename T> void print(T a) { cout << a << endl; }
template<typename T, typename... Ts> void print(T a, Ts... ts) { cout << a << ' '; print(ts...); }
template<typename T> istream &operator,(istream &in, T &t) { return in >> t; }
// clang-format on

#include <set>

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int h, w;
  cin, h, w;
  set<P> s1, s2;
  REP(i, 0, w) {
    s1.emplace(i, 0);
    s2.emplace(0, i);
  }
  const int inf = 1 << 29;
  REP(i, 0, h) {
    int a, b;
    cin, a, b;
    a--;
    b--;
    auto it1 = s1.lower_bound(P(a, -1));
    auto it2 = s1.lower_bound(P(b, inf));
    P maxP(-1, -1);
    while (it1 != it2) {
      chmax(maxP, *it1);
      auto [s, t] = *it1;
      it1 = s1.erase(it1);
      s2.erase(P(t, s));
      if (maxP.first != -1 && b + 1 < w) {
        auto [s, t] = maxP;
        s1.emplace(b + 1, t + b + 1 - s);
        s2.emplace(t + b + 1 - s, b + 1);
      }
    }
    if (s2.size() == 0) {
      print(-1);
    } else {
      auto [s, t] = *s2.begin();
      print(s + i + 1);
    }
  }

  return 0;
}