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

#pragma region Macro
#pragma region Basic
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define FOR(i, a, b) for (int i = (a), i##_len = (b); i < i##_len; i++)
#define FORD(i, a, b) for (int i = (a), i##_len = (b); i > i##_len; i--)
#define FORR(t, a) for (auto t : a)
#define ALL(x) (x).begin(), (x).end() // sortなどの引数を省略したい
#define GET(i, j) get<j>(i)
#define MT(x, ...) make_tuple(x, __VA_ARGS__)
#define SZ(x) ((int)(x).size())
#define UNUSED(x) (void(x));
#define PB push_back
#define EB emplace_back
#ifdef _DEBUG
#define PRE_COMMAND                                                            \\
  std::cin.rdbuf(in.rdbuf());                                                  \\
  cout << fixed << setprecision(15);
#else
#define PRE_COMMAND cout << fixed << setprecision(15);
#endif
const double EPS = 1e-10;
const double PI = acos(-1.0);
template <class T> auto MAX(T &seq) {
  return *max_element(seq.begin(), seq.end());
}
template <class T> auto MIN(T &seq) {
  return *min_element(seq.begin(), seq.end());
}
template <class T> auto SUM(T &seq) {
  T temp{0};
  auto &temp2 = temp[0];
  return accumulate(seq.begin(), seq.end(), temp2);
}
template <class T> void SORT(T &seq) { sort(seq.begin(), seq.end()); }
template <class T, class S> void SORT(T &seq, S &sort_order) {
  sort(seq.begin(), seq.end(), sort_order);
}
template <class T> void SORTR(vector<T> &seq) {
  sort(seq.begin(), seq.end(), greater<T>());
}
template <class T> void CHMAX(T &a, const T &b) {
  if (a < b) {
    a = b;
  }
}
template <class T> void CHMIN(T &a, const T &b) {
  if (b < a) {
    a = b;
  }
}
template <class T> set<T> VECTOR_2_SET(vector<T> &v) {
  set<T> s;
  for (auto ele : v) {
    s.insert(ele);
  }
  return s;
}
template <class T> vector<T> SET_2_VECTOR(set<T> &s) {
  vector<T> v;
  for (auto ele : s) {
    v.push_back(ele);
  }
  return v;
}
// pythonみたいなスライス
template <class T>
vector<T> SLICE(vector<T> &v, int first, int last, int step) {
  int n = (int)v.size(), now = first;
  vector<T> ret;
  if (first < 0 || n <= last) {
    return {};
  }
  if (step * (last - first) <= 0) {
    return {};
  }
  if (step > 0) {
    while (first <= now && now < last) {
      ret.push_back(v[now]);
      now += step;
    }
  } else {
    while (first >= now && now > last) {
      ret.push_back(v[now]);
      now += step;
    }
  }
  return ret;
}
template <class T, class S, class R> long long pow(T n_0, S k_0, R mod_0) {
  long long n = n_0;
  long long k = k_0;
  long long mod = mod_0;
  long long now = 1;
  while (true) {
    if (k & 1) {
      now = now * n % mod;
    }
    k >>= 1;
    if (k == 0) {
      return now;
    }
    n = (n * n) % mod;
  }
}
#pragma endregion Basic
#pragma region Input
template <class Head> void INPUT(vector<Head> &head);
template <size_t N, class Head> void INPUT(array<Head, N> &head);
template <class T1> void INPUT(tuple<T1> &t);
template <class T1, class T2> void INPUT(tuple<T1, T2> &t);
template <class T1, class T2, class T3> void INPUT(tuple<T1, T2, T3> &t);
template <class T1, class T2, class T3, class T4>
void INPUT(tuple<T1, T2, T3, T4> &t);
template <class T1, class T2, class T3, class T4, class T5>
void INPUT(tuple<T1, T2, T3, T4, T5> &t);
template <class Head> void INPUT(Head &head);
template <class Head, class... Tail> void INPUT(Head &head, Tail &...tail);
// 本体
template <class Head> void INPUT(vector<Head> &head) {
  for (size_t i = 0; i < head.size(); i++) {
    INPUT(head[i]);
  }
}
template <size_t N, class Head> void INPUT(array<Head, N> &head) {
  for (size_t i = 0; i < head.size(); i++) {
    INPUT(head[i]);
  }
}
template <class T1> void INPUT(tuple<T1> &t) { cin >> get<0>(t); }
template <class T1, class T2> void INPUT(tuple<T1, T2> &t) {
  cin >> get<0>(t) >> get<1>(t);
}
template <class T1, class T2, class T3> void INPUT(tuple<T1, T2, T3> &t) {
  cin >> get<0>(t) >> get<1>(t) >> get<2>(t);
}
template <class T1, class T2, class T3, class T4>
void INPUT(tuple<T1, T2, T3, T4> &t) {
  cin >> get<0>(t) >> get<1>(t) >> get<2>(t) >> get<3>(t);
}
template <class T1, class T2, class T3, class T4, class T5>
void INPUT(tuple<T1, T2, T3, T4, T5> &t) {
  cin >> get<0>(t) >> get<1>(t) >> get<2>(t) >> get<3>(t) >> get<4>(t);
}
template <class Head> void INPUT(Head &head) { cin >> head; }
template <class Head, class... Tail> void INPUT(Head &head, Tail &...tail) {
  INPUT(head);
  INPUT((tail)...);
}
#pragma endregion Input
#pragma region Print
template <class T1> void PRINT2(tuple<T1> t);
template <class T1, class T2> void PRINT2(tuple<T1, T2> t);
template <class T1, class T2, class T3> void PRINT2(tuple<T1, T2, T3> t);
template <class T1, class T2, class T3, class T4>
void PRINT2(tuple<T1, T2, T3, T4> t);
template <class T1, class T2, class T3, class T4, class T5>
void PRINT2(tuple<T1, T2, T3, T4, T5> t);
template <class Head> void PRINT2(vector<Head> head);
template <class Head> void PRINT2(vector<vector<Head>> head);
template <size_t N, class Head> void PRINT2(vector<array<Head, N>> head);
template <class... Head> void PRINT2(vector<tuple<Head...>> head);
template <size_t N, class Head> void PRINT2(array<Head, N> head);
template <class Head> void PRINT2(Head head);
template <class Head, class... Tail> void PRINT2(Head head, Tail... tail);
template <class Head> void PRINT(Head head);
template <class... Head> void PRINT(Head... head);
template <int N, class Head> void PRINT(Head head);
template <class T1> void PRINT2(tuple<T1> t) { PRINT2(get<0>(t)); }
template <class T1, class T2> void PRINT2(tuple<T1, T2> t) {
  PRINT2(get<0>(t), get<1>(t));
}
template <class T1, class T2, class T3> void PRINT2(tuple<T1, T2, T3> t) {
  PRINT2(get<0>(t), get<1>(t), get<2>(t));
}
template <class T1, class T2, class T3, class T4>
void PRINT2(tuple<T1, T2, T3, T4> t) {
  PRINT2(get<0>(t), get<1>(t), get<2>(t), get<3>(t));
}
template <class T1, class T2, class T3, class T4, class T5>
void PRINT2(tuple<T1, T2, T3, T4, T5> t) {
  PRINT2(get<0>(t), get<1>(t), get<2>(t), get<3>(t), get<4>(t));
}
template <class Head> void PRINT2(vector<Head> head) {
  for (auto i = head.begin(); i != head.end(); i++) {
    PRINT2(*i);
    if (i != head.end() - 1) {
      cout << " ";
    }
  }
}
template <class Head> void PRINT2(vector<vector<Head>> head) {
  for (auto i = head.begin(); i != head.end(); i++) {
    PRINT2(*i);
    if (i != head.end() - 1) {
      cout << endl;
    }
  }
}
template <size_t N, class Head> void PRINT2(vector<array<Head, N>> head) {
  for (auto i = head.begin(); i != head.end(); i++) {
    PRINT2(*i);
    if (i != head.end() - 1) {
      cout << endl;
    }
  }
}
template <class... Head> void PRINT2(vector<tuple<Head...>> head) {
  for (auto i = head.begin(); i != head.end(); i++) {
    PRINT2(*i);
    if (i != head.end() - 1) {
      cout << endl;
    }
  }
}
template <size_t N, class Head> void PRINT2(array<Head, N> head) {
  for (auto i = head.begin(); i != head.end(); i++) {
    PRINT2(*i);
    if (i != head.end() - 1) {
      cout << " ";
    }
  }
}
template <class Head> void PRINT2(Head head) { cout << head; }
template <class Head, class... Tail> void PRINT2(Head head, Tail... tail) {
  PRINT2(head);
  cout << " ";
  PRINT2((tail)...);
}
template <class Head> void PRINT(Head head) {
  PRINT2(head);
  cout << endl;
}
template <class... Head> void PRINT(Head... head) {
  PRINT2((head)...);
  cout << endl;
}
template <int N, class Head> void PRINT(Head head) {
  PRINT2(head);
  cout << endl;
}
#pragma endregion Print
#pragma endregion Macro

int main() {
  PRE_COMMAND
  int h, w, end, start, a, b, m, lim = 0, inf = pow(10, 9), ini;
  INPUT(h, w);
  map<int, int> end_start;
  multiset<int> mset;
  mset.insert(inf);
  REP(i, w) {
    end_start[i] = i;
    mset.insert(0);
  }
  vector<tuple<int, int>> temp;
  for (int i = 0; i < h; i++) {
    INPUT(a, b);
    temp = {};
    m = -1, ini = -1;
    for (auto itr = end_start.lower_bound(a - 1);
         itr != end_start.upper_bound(b - 1); itr++) {
      tie(end, start) = *itr;
      temp.push_back(*itr);
      ini = max(ini, start);
      m = max(m, end);
    }
    for (auto t : temp) {
      tie(end, start) = t;
      end_start.erase(end);
      mset.erase(mset.find(end - start));
    }
    if (b < w && ini != -1 && end_start.count(b) == 0) {
      end_start[b] = ini;
      mset.insert(b - ini);
    }
    int ans = *mset.lower_bound(-1);
    if (ans == inf) {
      PRINT(-1);
    } else {
      PRINT(ans + i + 1);
    }
  }
}


Error: TLE

Fixed submission:

#pragma region Macro
#pragma region Basic
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define FOR(i, a, b) for (int i = (a), i##_len = (b); i < i##_len; i++)
#define FORD(i, a, b) for (int i = (a), i##_len = (b); i > i##_len; i--)
#define FORR(t, a) for (auto t : a)
#define ALL(x) (x).begin(), (x).end() // sortなどの引数を省略したい
#define GET(i, j) get<j>(i)
#define MT(x, ...) make_tuple(x, __VA_ARGS__)
#define SZ(x) ((int)(x).size())
#define UNUSED(x) (void(x));
#define PB push_back
#define EB emplace_back
#ifdef _DEBUG
#define PRE_COMMAND                                                            \\
  std::cin.rdbuf(in.rdbuf());                                                  \\
  cout << fixed << setprecision(15);
#else
#define PRE_COMMAND cout << fixed << setprecision(15);
#endif
const double EPS = 1e-10;
const double PI = acos(-1.0);
template <class T> auto MAX(T &seq) {
  return *max_element(seq.begin(), seq.end());
}
template <class T> auto MIN(T &seq) {
  return *min_element(seq.begin(), seq.end());
}
template <class T> auto SUM(T &seq) {
  T temp{0};
  auto &temp2 = temp[0];
  return accumulate(seq.begin(), seq.end(), temp2);
}
template <class T> void SORT(T &seq) { sort(seq.begin(), seq.end()); }
template <class T, class S> void SORT(T &seq, S &sort_order) {
  sort(seq.begin(), seq.end(), sort_order);
}
template <class T> void SORTR(vector<T> &seq) {
  sort(seq.begin(), seq.end(), greater<T>());
}
template <class T> void CHMAX(T &a, const T &b) {
  if (a < b) {
    a = b;
  }
}
template <class T> void CHMIN(T &a, const T &b) {
  if (b < a) {
    a = b;
  }
}
template <class T> set<T> VECTOR_2_SET(vector<T> &v) {
  set<T> s;
  for (auto ele : v) {
    s.insert(ele);
  }
  return s;
}
template <class T> vector<T> SET_2_VECTOR(set<T> &s) {
  vector<T> v;
  for (auto ele : s) {
    v.push_back(ele);
  }
  return v;
}
// pythonみたいなスライス
template <class T>
vector<T> SLICE(vector<T> &v, int first, int last, int step) {
  int n = (int)v.size(), now = first;
  vector<T> ret;
  if (first < 0 || n <= last) {
    return {};
  }
  if (step * (last - first) <= 0) {
    return {};
  }
  if (step > 0) {
    while (first <= now && now < last) {
      ret.push_back(v[now]);
      now += step;
    }
  } else {
    while (first >= now && now > last) {
      ret.push_back(v[now]);
      now += step;
    }
  }
  return ret;
}
template <class T, class S, class R> long long pow(T n_0, S k_0, R mod_0) {
  long long n = n_0;
  long long k = k_0;
  long long mod = mod_0;
  long long now = 1;
  while (true) {
    if (k & 1) {
      now = now * n % mod;
    }
    k >>= 1;
    if (k == 0) {
      return now;
    }
    n = (n * n) % mod;
  }
}
#pragma endregion Basic
#pragma region Input
template <class Head> void INPUT(vector<Head> &head);
template <size_t N, class Head> void INPUT(array<Head, N> &head);
template <class T1> void INPUT(tuple<T1> &t);
template <class T1, class T2> void INPUT(tuple<T1, T2> &t);
template <class T1, class T2, class T3> void INPUT(tuple<T1, T2, T3> &t);
template <class T1, class T2, class T3, class T4>
void INPUT(tuple<T1, T2, T3, T4> &t);
template <class T1, class T2, class T3, class T4, class T5>
void INPUT(tuple<T1, T2, T3, T4, T5> &t);
template <class Head> void INPUT(Head &head);
template <class Head, class... Tail> void INPUT(Head &head, Tail &...tail);
// 本体
template <class Head> void INPUT(vector<Head> &head) {
  for (size_t i = 0; i < head.size(); i++) {
    INPUT(head[i]);
  }
}
template <size_t N, class Head> void INPUT(array<Head, N> &head) {
  for (size_t i = 0; i < head.size(); i++) {
    INPUT(head[i]);
  }
}
template <class T1> void INPUT(tuple<T1> &t) { cin >> get<0>(t); }
template <class T1, class T2> void INPUT(tuple<T1, T2> &t) {
  cin >> get<0>(t) >> get<1>(t);
}
template <class T1, class T2, class T3> void INPUT(tuple<T1, T2, T3> &t) {
  cin >> get<0>(t) >> get<1>(t) >> get<2>(t);
}
template <class T1, class T2, class T3, class T4>
void INPUT(tuple<T1, T2, T3, T4> &t) {
  cin >> get<0>(t) >> get<1>(t) >> get<2>(t) >> get<3>(t);
}
template <class T1, class T2, class T3, class T4, class T5>
void INPUT(tuple<T1, T2, T3, T4, T5> &t) {
  cin >> get<0>(t) >> get<1>(t) >> get<2>(t) >> get<3>(t) >> get<4>(t);
}
template <class Head> void INPUT(Head &head) { cin >> head; }
template <class Head, class... Tail> void INPUT(Head &head, Tail &...tail) {
  INPUT(head);
  INPUT((tail)...);
}
#pragma endregion Input
#pragma region Print
template <class T1> void PRINT2(tuple<T1> t);
template <class T1, class T2> void PRINT2(tuple<T1, T2> t);
template <class T1, class T2, class T3> void PRINT2(tuple<T1, T2, T3> t);
template <class T1, class T2, class T3, class T4>
void PRINT2(tuple<T1, T2, T3, T4> t);
template <class T1, class T2, class T3, class T4, class T5>
void PRINT2(tuple<T1, T2, T3, T4, T5> t);
template <class Head> void PRINT2(vector<Head> head);
template <class Head> void PRINT2(vector<vector<Head>> head);
template <size_t N, class Head> void PRINT2(vector<array<Head, N>> head);
template <class... Head> void PRINT2(vector<tuple<Head...>> head);
template <size_t N, class Head> void PRINT2(array<Head, N> head);
template <class Head> void PRINT2(Head head);
template <class Head, class... Tail> void PRINT2(Head head, Tail... tail);
template <class Head> void PRINT(Head head);
template <class... Head> void PRINT(Head... head);
template <int N, class Head> void PRINT(Head head);
template <class T1> void PRINT2(tuple<T1> t) { PRINT2(get<0>(t)); }
template <class T1, class T2> void PRINT2(tuple<T1, T2> t) {
  PRINT2(get<0>(t), get<1>(t));
}
template <class T1, class T2, class T3> void PRINT2(tuple<T1, T2, T3> t) {
  PRINT2(get<0>(t), get<1>(t), get<2>(t));
}
template <class T1, class T2, class T3, class T4>
void PRINT2(tuple<T1, T2, T3, T4> t) {
  PRINT2(get<0>(t), get<1>(t), get<2>(t), get<3>(t));
}
template <class T1, class T2, class T3, class T4, class T5>
void PRINT2(tuple<T1, T2, T3, T4, T5> t) {
  PRINT2(get<0>(t), get<1>(t), get<2>(t), get<3>(t), get<4>(t));
}
template <class Head> void PRINT2(vector<Head> head) {
  for (auto i = head.begin(); i != head.end(); i++) {
    PRINT2(*i);
    if (i != head.end() - 1) {
      cout << " ";
    }
  }
}
template <class Head> void PRINT2(vector<vector<Head>> head) {
  for (auto i = head.begin(); i != head.end(); i++) {
    PRINT2(*i);
    if (i != head.end() - 1) {
      cout << endl;
    }
  }
}
template <size_t N, class Head> void PRINT2(vector<array<Head, N>> head) {
  for (auto i = head.begin(); i != head.end(); i++) {
    PRINT2(*i);
    if (i != head.end() - 1) {
      cout << endl;
    }
  }
}
template <class... Head> void PRINT2(vector<tuple<Head...>> head) {
  for (auto i = head.begin(); i != head.end(); i++) {
    PRINT2(*i);
    if (i != head.end() - 1) {
      cout << endl;
    }
  }
}
template <size_t N, class Head> void PRINT2(array<Head, N> head) {
  for (auto i = head.begin(); i != head.end(); i++) {
    PRINT2(*i);
    if (i != head.end() - 1) {
      cout << " ";
    }
  }
}
template <class Head> void PRINT2(Head head) { cout << head; }
template <class Head, class... Tail> void PRINT2(Head head, Tail... tail) {
  PRINT2(head);
  cout << " ";
  PRINT2((tail)...);
}
template <class Head> void PRINT(Head head) {
  PRINT2(head);
  cout << endl;
}
template <class... Head> void PRINT(Head... head) {
  PRINT2((head)...);
  cout << endl;
}
template <int N, class Head> void PRINT(Head head) {
  PRINT2(head);
  cout << endl;
}
#pragma endregion Print
#pragma endregion Macro

int main() {
  PRE_COMMAND
  int h, w, end, start, a, b, m, lim = 0, inf = pow(10, 9), ini;
  INPUT(h, w);
  map<int, int> end_start;
  multiset<int> mset;
  mset.insert(inf);
  REP(i, w) {
    end_start[i] = i;
    mset.insert(0);
  }
  vector<tuple<int, int>> temp;
  for (int i = 0; i < h; i++) {
    INPUT(a, b);
    temp = {};
    m = -1, ini = -1;
    for (auto itr = end_start.lower_bound(a - 1);
         itr != end_start.upper_bound(b - 1); itr++) {
      tie(end, start) = *itr;
      temp.push_back(*itr);
      ini = max(ini, start);
      m = max(m, end);
    }
    for (auto t : temp) {
      tie(end, start) = t;
      end_start.erase(end);
      mset.erase(mset.find(end - start));
      // PRINT(end, start);
    }
    if (b < w && ini != -1 && end_start.count(b) == 0) {
      end_start[b] = ini;
      mset.insert(b - ini);
      // PRINT("", b, ini);
    }
    // PRINT(mset.size());
    if (MIN(mset) == inf) {
      PRINT(-1);
    } else {
      PRINT(MIN(mset) + i + 1);
    }
  }
}