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

#include "algorithm"
#include "bitset"
#include "cassert"
#include "climits"
#include "cmath"
#include "cstdio"
#include "ctime"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "numeric"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "unordered_set"

using namespace std;

constexpr long long int MOD = 1000000007;
// constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
// constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-12;

// int N, M, K, T, H, W, L, R;
long long int N, M, K, T, H, W, L, R;

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

  cin >> H >> W;
  vector<int> l(H);
  vector<int> r(H);
  for (int i = 0; i < H; i++) {
    cin >> l[i] >> r[i];
  }
  multiset<int> ms;
  set<pair<int, int>> s;
  for (int i = 0; i < W; i++) {
    ms.insert(0);
    s.emplace(i + 1, i + 1);
  }
  vector<int> ans;
  for (int i = 0; i < H; i++) {
    while (1) {
      auto it = s.lower_bound({l[i], 0});
      if (it == s.end())
        break;
      if (it->first <= r[i]) {
        ms.erase(ms.find(it->first - it->second));
        if ((next(it) == s.end() || next(it)->first > r[i]) && r[i] != W) {
          //			cout << "it " << it->first << " " << it->second <<
          //endl;
          s.insert({r[i] + 1, it->second});
          ms.insert(r[i] + 1 - it->second);
        }
        //		cout << "erase " << it->first << " " << it->second <<
        //endl;
        s.erase(s.lower_bound({l[i], 0}));
      } else {
        break;
      }
    }
    //	for (auto j : s) {
    //		cout << j.first << " " << j.second << endl;
    //	}
    if (ms.size())
      cout << *ms.begin() + i + 1 << endl;
    else
      cout << -1 << endl;
  }
}

Error: TLE

Fixed submission:

#include "algorithm"
#include "bitset"
#include "cassert"
#include "climits"
#include "cmath"
#include "cstdio"
#include "ctime"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "numeric"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "unordered_set"

using namespace std;

constexpr long long int MOD = 1000000007;
// constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
// constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-12;

// int N, M, K, T, H, W, L, R;
long long int N, M, K, T, H, W, L, R;

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

  cin >> H >> W;
  vector<int> l(H);
  vector<int> r(H);
  for (int i = 0; i < H; i++) {
    cin >> l[i] >> r[i];
    if (i) {
      if (l[i] >= l[i - 1] && r[i] <= r[i - 1]) {
        l[i] = l[i - 1];
        r[i] = r[i - 1];
      }
    }
  }
  vector<int> ans;
  queue<pair<int, int>> Q;
  queue<int> numQ;
  if (l[0] >= 2) {
    Q.push({1, l[0] - 1});
    numQ.push(1);
  }
  if (r[0] < W) {
    Q.push({r[0] + 1, W});
    numQ.push(1);
  }
  if (Q.size())
    ans.push_back(1);
  else
    ans.push_back(MOD);
  for (int i = 1; i < H; i++) {
    int nx = MOD;
    int cnt = Q.size();
    while (cnt--) {
      int cl, cr;
      tie(cl, cr) = Q.front();
      Q.pop();
      int nl, nr;
      int num = numQ.front();
      numQ.pop();
      int nnum = 0;
      if (l[i] <= cl && cl <= r[i] && r[i] != W) {
        nl = r[i] + 1;
        nr = max(cr, nl);
        nnum = num + nr - cr + 1;
        nx = min(nx, nnum);
        Q.push({nl, nr});
        numQ.push(nnum);
      } else if (cl < l[i]) {
        nl = cl;
        nr = min(l[i] - 1, cr);
        nnum = num + 1;
        nx = min(nx, nnum);
        Q.push({nl, nr});
        numQ.push(nnum);
        nl = nr = r[i] + 1;
        nnum = num + nr - cr + 1;
        nx = min(nx, nnum);
        Q.push({nl, nr});
        numQ.push(nnum);
      } else if (r[i] < cl) {
        nl = cl, nr = cr;
        nnum = num + 1;
        nx = min(nx, nnum);
        Q.push({nl, nr});
        numQ.push(nnum);
      }
    }
    ans.push_back(nx);
  }
  for (auto i : ans) {
    if (i == MOD)
      i = -1;
    cout << i << endl;
  }
}