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

#include <algorithm>
#include <bits/stdc++.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
int INF = 1e9;
void chmax(int &a, int b) { a = max(a, b); }
void chmin(int &a, int b) { a = min(a, b); }

int main() {
  int h, w;
  cin >> h >> w;
  map<int, int> mp;
  multiset<int> m;
  rep(i, w) mp[i] = i;
  rep(i, w) m.insert(0);
  rep(i, h) {
    int l, r;
    cin >> l >> r;
    --l;
    map<int, int>::iterator it = mp.lower_bound(l);
    int mr = -1;
    while (it != mp.end() && it->first <= r) {
      mr = it->second;
      m.erase(m.find(it->first - it->second));
      mp.erase(it++);
    }
    if (mr != -1 && r < w) {
      mp[r] = mr;
      m.insert(r - mr);
    }
    int ans = -1;
    if (m.size() > 0) {
      ans = *m.begin() + (i + 1);
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define chmax(x, y) x = max(x, y)
int INF = 1e9;
struct Linear_Sieve {
  int n;
  vector<int> lp, primes;
  // lp[i]...the least prime of i. primes...prime numbers
  Linear_Sieve(int n = 1) : n(n), lp(n + 1) {
    for (int i = 2; i < n + 1; i++) {
      if (lp[i] == 0) {
        primes.push_back(i);
        lp[i] = i;
      }
      for (int j = 0;
           j < primes.size() && primes[j] <= lp[i] && i * primes[j] < n + 1;
           ++j)
        lp[i * primes[j]] = primes[j];
    }
  }
  bool isPrime(int x) { return lp[x] == x; }

  vector<int> factorList(int x) {
    // make list which contains prime factors of x. Ans it toralates multipled
    // components.
    vector<int> res;
    while (x != 1) {
      res.push_back(lp[x]);
      x /= lp[x];
    }
    return res;
  }

  vector<pair<int, int>> factor(int x) {
    // make list of pairs which couple prime factor and multiplicity.
    vector<int> fl = factorList(x);
    if (fl.size() == 0)
      return {};
    vector<pair<int, int>> res(1, pair<int, int>(fl[0], 0));
    for (int p : fl) {
      if (res.back().first == p) {
        res.back().second++;
      } else {
        res.emplace_back(p, 1);
      }
    }
    return res;
  }
};

int main() {
  int h, w;
  cin >> h >> w;
  vector<int> a(h), b(h);
  rep(i, h) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
  }
  using tup = tuple<int, int, int>;
  priority_queue<tup, vector<tup>, greater<tup>> q;
  int ind = INF;
  rep(i, w) {
    if (i < a[0] || i > b[0]) {
      ind = min(ind, i);
      q.emplace(0, i, 0);
    }
  }
  int hmax = 0;
  rep(i, h) {
    if (ind < a[i] || ind > b[i])
      continue;
    else
      ind = b[i] + 1;
    if (ind == w)
      hmax = i;
  }
  // cout << hmax << endl;

  vector<int> ans(200005, INF);
  while (!q.empty()) {
    tup temp = q.top();
    q.pop();
    int &hi = get<2>(temp);
    int &i = get<1>(temp);
    int &len = get<0>(temp);
    // printf("%d %d %d\\n",len,i,hi);
    // printf("%d %d %d\\n",a[hi],b[hi],12345);
    ans[hi] = min(ans[hi], len);
    if (hi == h)
      break;
    if (i < a[hi] || i > b[hi])
      q.emplace(len + 1, i, hi + 1);
    else {
      if (b[hi] + 1 < w)
        q.emplace(len + b[hi] - i + 2, b[hi] + 1, hi + 1);
    }
  }
  rep(i, h) {
    if (ans[i + 1] != INF)
      cout << ans[i + 1] << endl;
    else
      cout << -1 << endl;
  }
}