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

#include <bits/stdc++.h>
using namespace std;
using pint = pair<int, int>;
using ll = long long;
using ull = unsigned long long;
using vll = vector<long long>;
using pll = pair<ll, ll>;
#define FOR(i, begin, end)                                                     \\
  for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end)                                                    \\
  for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define VREP(s, ite) for (auto ite = s.begin(); ite != s.end(); ++ite)
#define FI first
#define SE second
#define ALL(v) v.begin(), v.end()
#define endl "\\n"
#define ciosup                                                                 \\
  cin.tie(0);                                                                  \\
  ios::sync_with_stdio(false);
#define eb emplace_back
#define vint vector<int>
constexpr ll INF = 1e15 + 7LL;
constexpr ll MOD = 1e9 + 7LL;
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (int i = 0; i < v.size(); ++i) {
    is >> v[i];
  }
  return is;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  if (v.size() == 0)
    return os;
  for (int i = 0; i < v.size() - 1; ++i) {
    os << v[i] << " ";
  }
  os << v[v.size() - 1];
  return os;
}

void solve() {
  int h, w;
  cin >> h >> w;
  map<int, int> idma;
  map<int, int> ma;
  REP(i, w) {
    idma.insert(make_pair(i + 1, i + 1));
    ma[0]++;
  }
  REP(i, h) {
    int a, b;
    cin >> a >> b;
    if (b == w)
      b = 1000000000;
    auto nowite = idma.lower_bound(a);
    while (true) {
      if (nowite == idma.end())
        break;
      auto nextite = next(nowite);
      pint now = *nowite;
      if (now.FI > b)
        break;
      idma.erase(nowite);
      if (--ma[now.FI - now.SE] == 0)
        ma.erase(now.FI - now.SE);
      now.FI = b + 1;
      if (idma.find(now.FI) == idma.end()) {
        idma[now.FI] = now.SE;
        ++ma[now.FI - now.SE];
      } else {
        int nowval = idma[now.FI];
        if (nowval < now.SE) {
          if (--ma[now.FI - nowval] == 0)
            ma.erase(now.FI - nowval);
          ++ma[now.FI - now.SE];
          idma[now.FI] = now.SE;
        }
      }
      nowite = nextite;
    }
    auto ite = ma.begin();
    if (ite->first >= 100000000)
      cout << -1 << endl;
    else
      cout << i + 1 + ite->first << endl;
  }
}

int main() {
  solve();
  char tmp;
  while (cin >> tmp) {
    cin.putback(tmp);
    solve();
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using pint = pair<int, int>;
using ll = long long;
using ull = unsigned long long;
using vll = vector<long long>;
using pll = pair<ll, ll>;
#define FOR(i, begin, end)                                                     \\
  for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end)                                                    \\
  for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define VREP(s, ite) for (auto ite = s.begin(); ite != s.end(); ++ite)
#define FI first
#define SE second
#define ALL(v) v.begin(), v.end()
#define endl "\\n"
#define ciosup                                                                 \\
  cin.tie(0);                                                                  \\
  ios::sync_with_stdio(false);
#define eb emplace_back
#define vint vector<int>
constexpr ll INF = 1e15 + 7LL;
constexpr ll MOD = 1e9 + 7LL;
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (int i = 0; i < v.size(); ++i) {
    is >> v[i];
  }
  return is;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  if (v.size() == 0)
    return os;
  for (int i = 0; i < v.size() - 1; ++i) {
    os << v[i] << " ";
  }
  os << v[v.size() - 1];
  return os;
}

void solve() {
  int h, w;
  cin >> h >> w;
  multimap<int, int> idma;
  map<int, int> ma;
  REP(i, w) {
    idma.insert(make_pair(i + 1, i + 1));
    ma[0]++;
  }
  REP(i, h) {
    int a, b;
    cin >> a >> b;
    if (b == w)
      b = 1000000000;
    auto nowite = idma.lower_bound(a);
    while (true) {
      if (nowite == idma.end())
        break;
      auto nextite = next(nowite);
      pint now = *nowite;
      if (now.FI > b)
        break;
      idma.erase(nowite);
      if (--ma[now.FI - now.SE] == 0)
        ma.erase(now.FI - now.SE);
      now.FI = b + 1;
      ++ma[now.FI - now.SE];
      idma.insert(now);
      nowite = nextite;
    }
    auto ite = ma.begin();
    if (ite->first >= 100000000)
      cout << -1 << endl;
    else
      cout << i + 1 + ite->first << endl;
  }
}

int main() {
  solve();
  char tmp;
  while (cin >> tmp) {
    cin.putback(tmp);
    solve();
  }
}