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

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll, ll> P;
typedef pair<int, int> pii;
typedef vector<P> vpl;
typedef tuple<ll, ll, ll> tapu;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1 << 30;
const ll linf = 1LL << 62;
const int MAX = 1020000;
ll dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};
ll dx[8] = {0, 0, 1, -1, 1, -1, -1, 1};
const double pi = acos(-1);
const double eps = 1e-7;
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}
template <typename T> inline void print(T &a) {
  for (auto itr = a.begin(); itr != a.end(); itr++) {
    cout << *itr << " ";
  }
  cout << "\\n";
}
template <typename T1, typename T2> inline void print2(T1 a, T2 b) {
  cout << "debug: " << a << " " << b << "\\n";
}
template <typename T1, typename T2, typename T3>
inline void print3(T1 a, T2 b, T3 c) {
  cout << "debug: " << a << " " << b << " " << c << "\\n";
}
void mark() {
  cout << "#"
       << "\\n";
}
ll pcount(ll x) { return __builtin_popcountll(x); }
const int mod = 1e9 + 7;
// const int mod = 998244353;

int main() {
  int h, w;
  cin >> h >> w;
  vl a(h), b(h);
  rep(i, h) cin >> a[i] >> b[i], a[i]--, b[i]--;
  vector<int> val(w, 0);
  set<int> se;
  multiset<int> mse;
  rep(i, w) mse.insert(0);
  rep(i, w) se.insert(i);
  rep(i, h) {
    auto itr = se.lower_bound(a[i]);
    vector<int> era;
    for (auto it = itr; it != se.end(); it++) {
      if (*it > b[i])
        break;
      int tmp = val[*it] + b[i] + 1 - *it;
      if (b[i] + 1 != w) {
        if (val[b[i] + 1] > tmp) {
          mse.erase(mse.find(val[b[i] + 1]));
          mse.insert(tmp);
          chmin(val[b[i] + 1], tmp);
        }
      }
      mse.erase(mse.find(val[*it]));
      mse.insert(inf);
      val[*it] = inf;
      era.push_back(*it);
    }
    for (auto j : era)
      se.erase(j);
    if (b[i] + 1 < w)
      se.insert(b[i] + 1);
    int ans = *mse.begin();
    if (ans == inf)
      cout << -1 << "\\n";
    else
      cout << ans + i + 1 << "\\n";
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll, ll> P;
typedef pair<int, int> pii;
typedef vector<P> vpl;
typedef tuple<ll, ll, ll> tapu;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1 << 30;
const ll linf = 1LL << 62;
const int MAX = 1020000;
ll dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};
ll dx[8] = {0, 0, 1, -1, 1, -1, -1, 1};
const double pi = acos(-1);
const double eps = 1e-7;
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}
template <typename T> inline void print(T &a) {
  for (auto itr = a.begin(); itr != a.end(); itr++) {
    cout << *itr << " ";
  }
  cout << "\\n";
}
template <typename T1, typename T2> inline void print2(T1 a, T2 b) {
  cout << "debug: " << a << " " << b << "\\n";
}
template <typename T1, typename T2, typename T3>
inline void print3(T1 a, T2 b, T3 c) {
  cout << "debug: " << a << " " << b << " " << c << "\\n";
}
void mark() {
  cout << "#"
       << "\\n";
}
ll pcount(ll x) { return __builtin_popcountll(x); }
const int mod = 1e9 + 7;
// const int mod = 998244353;

int main() {
  int h, w;
  cin >> h >> w;
  vl a(h), b(h);
  rep(i, h) cin >> a[i] >> b[i], a[i]--, b[i]--;
  vector<int> val(w, 0);
  set<int> se;
  multiset<int> mse;
  rep(i, w) mse.insert(0);
  rep(i, w) se.insert(i);
  rep(i, h) {
    auto itr = se.lower_bound(a[i]);
    for (auto it = itr; it != se.end(); it++) {
      if (*it > b[i])
        break;
      int tmp = val[*it] + b[i] + 1 - *it;
      if (b[i] + 1 != w) {
        if (val[b[i] + 1] > tmp) {
          mse.erase(mse.find(val[b[i] + 1]));
          mse.insert(tmp);
          chmin(val[b[i] + 1], tmp);
        }
      }
      mse.erase(mse.find(val[*it]));
      mse.insert(inf);
      val[*it] = inf;
    }
    if (b[i] + 1 < w)
      se.insert(b[i] + 1);
    int ans = *mse.begin();
    if (ans == inf)
      cout << -1 << "\\n";
    else
      cout << ans + i + 1 << "\\n";
  }
}