Problem p02540 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
template <class T> using vc = vector<T>;
template <class T> using vvc = vector<vector<T>>;

#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n)-1; i >= 0; i--)
#define repe(i, l, r) for (ll i = (l); i < (r); i++)
#define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--)
#define repa(i, n) for (auto &i : n)

template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
struct init {
  init() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
  }
} init_;

#ifdef DEBUG
template <class T, class N> void verr(const T &a, const N &n) {
  rep(i, n) cerr << a[i] << " ";
  cerr << "\\n" << flush;
}
ll dbgt = 1;
void err() { cerr << "passed " << dbgt++ << "\\n" << flush; }
template <class H, class... T> void err(H &&h, T &&...t) {
  cerr << h << (sizeof...(t) ? " " : "\\n") << flush;
  if (sizeof...(t) > 0)
    err(forward<T>(t)...);
}
#endif

const ll INF = 4e18;
const ld EPS = 1e-11;
const ld PI = acos(-1.0L);
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
//--------------------------------------------------------------------------------//
struct UnionFind {
private:
  vector<int> par;
  vector<int> count;
  vector<int> rank;

public:
  UnionFind(int N) {
    count.assign(N, 1);
    rank.assign(N, 0);
    par.assign(N, 0);
    rep(i, N) par[i] = i;
  }

  int root(int x) {
    if (par[x] == x)
      return x;
    else {
      par[x] = root(par[x]);
      return par[x];
    }
  }

  void unite(int x, int y) {
    x = root(x), y = root(y);
    if (x == y)
      return;
    if (rank[x] < rank[y])
      swap(x, y);
    else if (rank[x] == rank[y])
      rank[y]++;
    par[y] = x;
    count[x] += count[y];
    return;
  }

  int size(int x) { return count[root(x)]; }

  bool issame(int x, int y) { return root(x) == root(y); }
};
using UF = struct UnionFind;

struct p {
  ll x, y, id;
  p(ll x = 0, ll y = 0, ll id = 0) : x(x), y(y), id(id) {}
};

int main() {
  ll N;
  cin >> N;
  vc<p> P(N);
  rep(i, N) cin >> P[i].x >> P[i].y, P[i].x--, P[i].y--, P[i].id = i;

  sort(all(P), [](p a, p b) { return a.x < b.x; });

  UF uf(N);
  auto comp = [](p a, p b) { return a.y < b.y; };
  set<p, decltype(comp)> st{comp};

  st.emplace(P[0]);

  repe(i, 1, N) {
    auto [x, y, id] = P[i];
    bool merged = false;

    for (auto it = st.begin(); it != st.lower_bound(P[i]);) {
      auto [tx, ty, tid] = *it;
      bool erased = false;
      uf.unite(id, tid);
      if (merged)
        it = st.erase(it), erased = true;
      else
        it++;
      merged = true;
    }
    if (!merged)
      st.emplace(P[i]);
    // for (auto [tx, ty, tid] : st) err(i, tx, ty, tid);
  }

  rep(i, N) cout << uf.size(i) << '\\n';
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
template <class T> using vc = vector<T>;
template <class T> using vvc = vector<vector<T>>;

#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n)-1; i >= 0; i--)
#define repe(i, l, r) for (ll i = (l); i < (r); i++)
#define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--)
#define repa(i, n) for (auto &i : n)

template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
struct init {
  init() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
  }
} init_;

#ifdef DEBUG
template <class T, class N> void verr(const T &a, const N &n) {
  rep(i, n) cerr << a[i] << " ";
  cerr << "\\n" << flush;
}
ll dbgt = 1;
void err() { cerr << "passed " << dbgt++ << "\\n" << flush; }
template <class H, class... T> void err(H &&h, T &&...t) {
  cerr << h << (sizeof...(t) ? " " : "\\n") << flush;
  if (sizeof...(t) > 0)
    err(forward<T>(t)...);
}
#endif

const ll INF = 4e18;
const ld EPS = 1e-11;
const ld PI = acos(-1.0L);
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
//--------------------------------------------------------------------------------//
struct UnionFind {
private:
  vector<int> par;
  vector<int> count;
  vector<int> rank;

public:
  UnionFind(int N) {
    count.assign(N, 1);
    rank.assign(N, 0);
    par.assign(N, 0);
    rep(i, N) par[i] = i;
  }

  int root(int x) {
    if (par[x] == x)
      return x;
    else {
      par[x] = root(par[x]);
      return par[x];
    }
  }

  void unite(int x, int y) {
    x = root(x), y = root(y);
    if (x == y)
      return;
    if (rank[x] < rank[y])
      swap(x, y);
    else if (rank[x] == rank[y])
      rank[y]++;
    par[y] = x;
    count[x] += count[y];
    return;
  }

  int size(int x) { return count[root(x)]; }

  bool issame(int x, int y) { return root(x) == root(y); }
};
using UF = struct UnionFind;

struct p {
  ll x, y, id;
  p(ll x = 0, ll y = 0, ll id = 0) : x(x), y(y), id(id) {}
};

int main() {
  ll N;
  cin >> N;
  vc<p> P(N);
  rep(i, N) cin >> P[i].x >> P[i].y, P[i].x--, P[i].y--, P[i].id = i;

  sort(all(P), [](p a, p b) { return a.x < b.x; });

  UF uf(N);
  auto comp = [](p a, p b) { return a.y < b.y; };
  set<p, decltype(comp)> st{comp};

  st.emplace(P[0]);

  repe(i, 1, N) {
    auto [x, y, id] = P[i];
    bool merged = false;

    for (auto it = st.begin(); it != st.lower_bound(P[i]);) {
      auto [tx, ty, tid] = *it;
      bool erased = false;
      uf.unite(id, tid);
      if (merged)
        st.erase(it), erased = true;
      merged = true;
      if (!erased)
        it++;
    }
    if (!merged)
      st.emplace(P[i]);
    // for (auto [tx, ty, tid] : st) err(i, tx, ty, tid);
  }

  rep(i, N) cout << uf.size(i) << '\\n';
}