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

#include <bits/stdc++.h>
using namespace std;
using ll = int;
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define all(c) begin(c), end(c)
#define PI acos(-1)
#define oo LLONG_MAX
template <typename T1, typename T2> bool chmax(T1 &a, T2 b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}
template <typename T1, typename T2> bool chmin(T1 &a, T2 b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}
/*

  1 2 3 4 5 //(now, start)
1 . # # # . (1, 1) (5, 5)
2 # . . . . (1, 1) (5, 5)
3 . # # . . (3, 1) (5, 5)
4 . # # # . (4, 1) (5, 5)

  1 2 3 4 5 //(now, start)
1 . # # . . (1,1) (4,4) (5,5)
2 # # # . . (4,4) (5,5)
3 . . . . # (4,4)
4 # # # # . (4,5)


*/
int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);

  ll H, W;
  cin >> H >> W;

  set<pair<ll, ll>> pos;
  multiset<ll> score;
  rep(i, H) {
    //    cout<<"i:"<<i<<endl;
    ll A, B;
    cin >> A >> B;
    // 初期入れ
    if (i == 0) {
      for (ll j = 1; j <= W; j++) {
        if (j >= A && j <= B)
          continue;
        pos.insert({j, j});
        score.insert(0);
      }
    } else {
      if (score.empty()) {
        cout << -1 << endl;
        continue;
      }
      auto itr1 = pos.lower_bound({A, 0});
      auto itr2 = pos.lower_bound({B, 200010});
      if (B == W || (itr2 != pos.end() && (*itr2).first == B + 1)) {
        while (itr1 != itr2) {
          score.erase(score.find((*itr1).first - (*itr1).second));
          pos.erase(itr1++);
        }
      } else {
        // B+1が存在するなら全部消す。
        // しないなら、B+1に最も近い1個だけ残して消す。
        while (itr1 != itr2) {
          auto judge = itr2;
          judge--;
          if (itr1 == judge) {
            ll v1 = (*itr1).first;
            ll v2 = (*itr1).second;
            score.erase(score.find(v1 - v2));
            score.insert(B + 1 - v2);
            pos.erase(itr1++);
            pos.insert({B + 1, v2});
          } else {
            score.erase(score.find((*itr1).first - (*itr1).second));
            pos.erase(itr1++);
          }
        }
      }
    }
    if (score.empty())
      cout << -1 << endl;
    else
      cout << *score.begin() + i + 1 << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = int;
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define all(c) begin(c), end(c)
#define PI acos(-1)
#define oo LLONG_MAX
template <typename T1, typename T2> bool chmax(T1 &a, T2 b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}
template <typename T1, typename T2> bool chmin(T1 &a, T2 b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}
/*

  1 2 3 4 5 //(now, start)
1 . # # # . (1, 1) (5, 5)
2 # . . . . (1, 1) (5, 5)
3 . # # . . (3, 1) (5, 5)
4 . # # # . (4, 1) (5, 5)

  1 2 3 4 5 //(now, start)
1 . # # .
2 # # # .
3 # # . .
4 # # # #


*/
int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);

  ll H, W;
  cin >> H >> W;

  vector<pair<ll, ll>> pos;
  multiset<ll> score;
  rep(i, H) {
    //    cout<<"i:"<<i<<endl;
    ll A, B;
    cin >> A >> B;
    // 初期入れ
    if (i == 0) {
      for (ll j = 1; j <= W; j++) {
        if (j >= A && j <= B)
          continue;
        pos.push_back({j, j});
        score.insert(0);
      }
    } else {
      ll psize = pos.size();
      if (psize == 0) {
        cout << -1 << endl;
        continue;
      }
      vector<pair<ll, ll>> work;
      vector<ll> used(200010, 0);
      for (ll j = psize - 1; j >= 0; j--) {
        ll v1 = pos[j].first;
        ll v2 = pos[j].second;
        if (v1 >= A && v1 <= B) {
          if (B == W) {
            score.erase(score.find(v1 - v2));
          } else {
            if (used[B + 1] == 0) {
              work.push_back({B + 1, v2});
              score.erase(score.find(v1 - v2));
              score.insert(B + 1 - v2);
            } else
              score.erase(score.find(v1 - v2));
            used[B + 1]++;
          }
        } else {
          work.push_back({v1, v2});
          used[v1]++;
        }
      }
      // rev
      ll wsize = work.size();
      for (ll j = 0; j < wsize / 2; j++) {
        auto tmp = work[j];
        work[j] = work[wsize - j - 1];
        work[wsize - j - 1] = tmp;
      }
      pos = work;
    }
    if (pos.size() == 0)
      cout << -1 << endl;
    else
      cout << *score.begin() + i + 1 << endl;
  }
}