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

#include <bits/stdc++.h>
#define int long long
#define ld long double
#define pb push_back
#define ff first
#define ss second
#define PI acos(-1)
#define bp __builtin_popcountll
#define all(x) x.begin(), x.end()
#define sp fixed << setprecision
#define pii pair<long long, long long>
#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
using namespace std;

int MOD = (1e9) + 7;
const int N = 200005;

int root[N], siz[N];

int get_root(int c) {
  if (root[c] == c)
    return c;
  return root[c] = get_root(root[c]);
}
void _union(int a, int b) {
  a = get_root(a);
  b = get_root(b);
  if (a <= b)
    root[b] = a, siz[a] += siz[b];
  else
    root[a] = b, siz[b] += siz[a];
}
void init(int n) {
  for (int i = 1; i <= n; i++)
    root[i] = i, siz[i] = 1;
}

signed main() {
  FAST int tc = 1;
  // cin>>tc;
  for (int ti = 1; ti <= tc; ti++) {
    int n;
    cin >> n;
    init(n);
    vector<pair<pii, int>> a(n);
    for (int i = 0; i < n; i++) {
      int x, y;
      cin >> x >> y;
      a[i] = {{x, y}, i};
    }
    sort(all(a));
    set<int> s;
    for (int i = 0; i < n; i++) {
      // for(int x:s)cout<<x<<" - ";cout<<endl;
      int now = a[i].ff.ss;
      if (!s.size()) {
        s.insert(now);
        continue;
      }
      auto it = s.begin();
      if (*it > now) {
        s.insert(now);
        continue;
      }
      int r = *it;
      vector<int> v;
      it++;
      while (it != s.end()) {
        if (*it > now)
          break;
        _union(r, *it);
        v.pb(*it);
        it++;
      }
      for (auto u : v)
        s.erase(u);
      _union(now, r);
    }
    int ans[n];
    for (int i = 0; i < n; i++) {
      int now = get_root(a[i].ff.ss);
      ans[a[i].ss] = siz[now];
    }
    for (int i = 0; i < n; i++)
      cout << ans[i] << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define int long long
#define ld long double
#define pb push_back
#define ff first
#define ss second
#define PI acos(-1)
#define bp __builtin_popcountll
#define all(x) x.begin(), x.end()
#define sp fixed << setprecision
#define pii pair<long long, long long>
#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
using namespace std;

int MOD = (1e9) + 7;
const int N = 200005;

int root[N], siz[N];

int get_root(int c) {
  if (root[c] == c)
    return c;
  return root[c] = get_root(root[c]);
}
void _union(int a, int b) {
  a = get_root(a);
  b = get_root(b);
  if (a <= b)
    root[b] = a, siz[a] += siz[b];
  else
    root[a] = b, siz[b] += siz[a];
}
int init(int n) {
  for (int i = 1; i <= n; i++)
    root[i] = i, siz[i] = 1;
}

signed main() {
  FAST int tc = 1;
  // cin>>tc;
  for (int ti = 1; ti <= tc; ti++) {
    int n;
    cin >> n;
    init(n);
    vector<pair<pii, int>> a(n);
    for (int i = 0; i < n; i++) {
      int x, y;
      cin >> x >> y;
      a[i] = {{x, y}, i};
    }
    sort(all(a));
    // for(int i=0;i<n;i++)cout<<a[i].ff.ff<<" "<<a[i].ff.ss<<endl;
    set<int> s;
    for (int i = 0; i < n; i++) {
      // for(int x:s)cout<<x<<" - ";cout<<endl;
      int now = a[i].ff.ss;
      if (!s.size()) {
        s.insert(now);
        continue;
      }
      auto it = s.begin();
      if (*it > now) {
        s.insert(now);
        continue;
      }
      int r = get_root(*it);
      set<int> v;
      it++;
      while (it != s.end()) {
        if (*it > now)
          break;
        _union(r, *it);
        v.insert(*it);
        it++;
      }
      for (int u : v)
        s.erase(u);
      _union(now, r);
    }
    int ans[n];
    for (int i = 0; i < n; i++) {
      int now = get_root(a[i].ff.ss);
      ans[a[i].ss] = siz[now];
    }
    for (int i = 0; i < n; i++)
      cout << ans[i] << endl;
  }
  return 0;
}