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

#if loc || local
#define _GLIBCXX_DEBUG
#endif

#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (ll i = 0; i < ll(n); ++i)
#define repr(i, n) for (ll i = ll(n); i-- > 0;)
using ll = int_fast64_t;
using pll = pair<ll, ll>;
constexpr ll INF = 1LL << 60;
constexpr ll MOD = 1e9 + 7;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#if loc || local
template <class T> void dump(T &&t) { cerr << t << endl; }
template <class T, class... Ts> void dump(T &&h, Ts &&...t) {
  cerr << h << ", ";
  dump(forward<Ts>(t)...);
}
#else
void dump() {}
template <class T, class... Ts> void dump(T &&h, Ts &&...t) {}
#endif
template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &elemnt : v)
    is >> elemnt;
  return is;
}
template <class T, class U> istream &operator>>(istream &is, pair<T, U> &p) {
  is >> p.first >> p.second;
  return is;
}
template <class T> ostream &operator<<(ostream &os, vector<T> const &v) {
  for (auto const &vi : v)
    os << vi << " ";
  return os;
}
template <class T, class U>
ostream &operator<<(ostream &os, pair<T, U> const &p) {
  os << p.first << "," << p.second;
  return os;
}
template <class T> vector<T> vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto vec(size_t a, Ts... ts) {
  return vector<decltype(vec<T>(ts...))>(a, vec<T>(ts...));
}
struct union_find {
  size_t n;
  std::vector<int> par;
  size_t group;
  union_find() {}
  union_find(size_t _n) : n(_n), group(_n) { par.resize(n, -1); }
  void reset(size_t _n) {
    n = _n;
    group = n;
    par.clear();
    par.resize(n, -1);
  }
  int root(int k) {
    if (par[k] < 0)
      return k;
    return par[k] = root(par[k]);
  }
  int size(int k) {
    k = root(k);
    return -par[k];
  }
  int operator[](int k) { return size(k); }
  bool unite(int a, int b) {
    a = root(a);
    b = root(b);
    if (a == b)
      return false;
    if (size(a) < size(b))
      std::swap(a, b);
    group--;
    par[a] += par[b];
    par[b] = a;
    return true;
  }
  bool operator()(int a, int b) { return unite(a, b); }
  bool same(int a, int b) { return root(a) == root(b); }
};
using T3 = tuple<ll, ll, ll>;
using T4 = tuple<ll, ll, ll, ll>;
signed main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  constexpr char newl = '\\n';

  int n;
  cin >> n;
  vector<T3> points(n);
  rep(i, n) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    points[i] = {x, y, i};
  }
  sort(begin(points), end(points));

  union_find uni(n);
  set<pll> st;
  rep(i, n) {
    auto [xi, yi, ii] = points[i];
    ll merge_min = yi;
    for (auto itr = begin(st); itr != end(st);) {
      auto [yj, j] = *itr;
      if (yj > yi)
        break;
      uni.unite(ii, j);
      chmin(merge_min, yj);
      itr = st.erase(itr);
    }
    st.emplace(merge_min, ii);
  }
  rep(k, n) cout << uni[k] << newl;
}

Error: TLE

Fixed submission:

#if loc || local
#define _GLIBCXX_DEBUG
#endif

#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (ll i = 0; i < ll(n); ++i)
#define repr(i, n) for (ll i = ll(n); i-- > 0;)
using ll = int_fast64_t;
using pll = pair<ll, ll>;
constexpr ll INF = 1LL << 60;
constexpr ll MOD = 1e9 + 7;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#if loc || local
template <class T> void dump(T &&t) { cerr << t << endl; }
template <class T, class... Ts> void dump(T &&h, Ts &&...t) {
  cerr << h << ", ";
  dump(forward<Ts>(t)...);
}
#else
void dump() {}
template <class T, class... Ts> void dump(T &&h, Ts &&...t) {}
#endif
template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &elemnt : v)
    is >> elemnt;
  return is;
}
template <class T, class U> istream &operator>>(istream &is, pair<T, U> &p) {
  is >> p.first >> p.second;
  return is;
}
template <class T> ostream &operator<<(ostream &os, vector<T> const &v) {
  for (auto const &vi : v)
    os << vi << " ";
  return os;
}
template <class T, class U>
ostream &operator<<(ostream &os, pair<T, U> const &p) {
  os << p.first << "," << p.second;
  return os;
}
template <class T> vector<T> vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto vec(size_t a, Ts... ts) {
  return vector<decltype(vec<T>(ts...))>(a, vec<T>(ts...));
}
struct union_find {
  size_t n;
  std::vector<int> par;
  size_t group;
  union_find() {}
  union_find(size_t _n) : n(_n), group(_n) { par.resize(n, -1); }
  void reset(size_t _n) {
    n = _n;
    group = n;
    par.clear();
    par.resize(n, -1);
  }
  int root(int k) {
    if (par[k] < 0)
      return k;
    return par[k] = root(par[k]);
  }
  int size(int k) {
    k = root(k);
    return -par[k];
  }
  int operator[](int k) { return size(k); }
  bool unite(int a, int b) {
    a = root(a);
    b = root(b);
    if (a == b)
      return false;
    if (size(a) < size(b))
      std::swap(a, b);
    group--;
    par[a] += par[b];
    par[b] = a;
    return true;
  }
  bool operator()(int a, int b) { return unite(a, b); }
  bool same(int a, int b) { return root(a) == root(b); }
};
using T3 = tuple<ll, ll, ll>;
using T4 = tuple<ll, ll, ll, ll>;
signed main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  constexpr char newl = '\\n';

  int n;
  cin >> n;
  vector<T3> points(n);
  rep(i, n) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    points[i] = {x, y, i};
  }
  sort(begin(points), end(points));

  union_find uni(n);
  stack<pll> st;
  rep(i, n) {
    auto [xi, yi, ii] = points[i];
    stack<pll> nxt;
    ll merge_min = yi;
    while (!st.empty()) {
      auto [yj, j] = st.top();
      st.pop();
      if (yi > yj) {
        uni.unite(ii, j);
        chmin(merge_min, yj);
      } else {
        nxt.emplace(yj, j);
      }
    }
    nxt.emplace(merge_min, ii);
    st.swap(nxt);
  }
  rep(k, n) cout << uni[k] << newl;
}