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

#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <cctype>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>

using namespace std;
typedef long long ll;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> T gcd(T a, T b) {
  a = abs(a), b = abs(b);
  while (b > 0) {
    tie(a, b) = make_pair(b, a % b);
  }
  return a;
}

constexpr long long INF = 1LL << 60;
constexpr int inf = 1000000007;
constexpr long long mod = 1000000007LL;
// constexpr long long mod = 998244353LL;

int main() {

  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int H, W;
  cin >> H >> W;
  vector<int> L(H), R(H);
  for (int i = 0; i < H; i++)
    cin >> L[i] >> R[i], L[i]--, R[i]--;
  map<int, int> mp;
  for (int i = 0; i < W; i++)
    mp[i] = i;
  vector<int> cnt(W + 1);
  cnt[0] = W;
  int res = 0;
  for (int i = 0; i < H; i++) {
    auto itr = mp.lower_bound(L[i]);
    int r = -1;
    while (itr != mp.end() and itr->first <= R[i] + 1) {
      cnt[itr->first - itr->second]--;
      chmax(r, itr->second);
      itr = mp.erase(itr);
    }
    if (r >= 0 and R[i] + 1 < W) {
      mp[R[i] + 1] = r;
      cnt[R[i] + 1 - r]++;
    }
    while (res < W and cnt[res] == 0)
      res++;
    if (res == W)
      cout << -1 << "\\n";
    else
      cout << i + 1 + res << "\\n";
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <cctype>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>

using namespace std;
typedef long long ll;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> T gcd(T a, T b) {
  a = abs(a), b = abs(b);
  while (b > 0) {
    tie(a, b) = make_pair(b, a % b);
  }
  return a;
}

constexpr long long INF = 1LL << 60;
constexpr int inf = 1000000007;
constexpr long long mod = 1000000007LL;
// constexpr long long mod = 998244353LL;

int main() {

  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int H, W;
  cin >> H >> W;
  vector<int> L(H), R(H);
  for (int i = 0; i < H; i++)
    cin >> L[i] >> R[i], L[i]--, R[i]--;
  map<int, int> mp;
  for (int i = 0; i < W; i++)
    mp[i] = i;
  vector<int> cnt(W + 1);
  cnt[0] = W;
  int res = 0;
  for (int i = 0; i < H; i++) {
    int s = L[i] - 1;
    auto itr = mp.lower_bound(L[i]);
    while (itr != mp.end() and itr->first <= R[i]) {
      cnt[itr->first - itr->second]--;
      if (s >= 0 and mp.find(s) != mp.end()) {
        itr->second = mp[s];
        cnt[itr->first - itr->second]++;
        itr++;
      } else
        itr = mp.erase(itr);
    }
    while (res < W and cnt[res] == 0)
      res++;
    if (res == W)
      cout << -1 << "\\n";
    else
      cout << i + 1 + res << "\\n";
  }
  return 0;
}