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

#include <bits/stdc++.h>
using namespace std;
#define ll int
#define endl '\\n'
#define mod 1000000007
#define pb push_back
#define ff first
#define ss second
#define con continue
#define ub upper_bound
#define lb lower_bound
#define sum(a) (accumulate((a).begin(), (a).end(), 0ll))
#define mine(a) (*min_element((a).begin(), (a).end()))
#define maxe(a) (*max_element((a).begin(), (a).end()))
#define mini(a) (min_element((a).begin(), (a).end()) - (a).begin())
#define maxi(a) (max_element((a).begin(), (a).end()) - (a).begin())
#define lowb(a, x) (lower_bound((a).begin(), (a).end(), (x)) - (a).begin())
#define uppb(a, x) (upper_bound((a).begin(), (a).end(), (x)) - (a).begin())

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};

const int dx8[] = {-1, 0, 1, 0, 1, 1, -1, -1};
const int dy8[] = {0, -1, 0, 1, 1, -1, 1, -1};

vector<ll> arr, rank1;
unordered_map<ll, ll> m;

ll p(ll x) {

  if (x == arr[x])
    return x;
  return arr[x] = p(arr[x]);
}
void join(ll x, ll y) {

  x = p(x), y = p(y);
  if (rank1[x] < rank1[y])
    arr[x] = y;
  else if (rank1[x] > rank1[y])
    arr[y] = x;
  else {
    rank1[y]++;
    arr[x] = y;
  }
}

void solve() {
  ll n;
  cin >> n;
  vector<pair<ll, pair<ll, ll>>> ans, ans2;

  arr.clear();
  rank1.clear();
  for (ll i = 0; i < n; i++) {
    rank1.pb(0);
    arr.pb(i);
  }

  for (ll i = 0; i < n; i++) {
    ll x, y;
    cin >> x >> y;
    ans.pb({x * -1, {i, y}});
    ans2.pb({y * -1, {i, x}});
  }
  sort(ans.begin(), ans.end());
  stack<ll> s;
  for (ll i = n - 1; i >= 0; i--) {
    while (!s.empty() && ans[i].ss.ss > ans[s.top()].ss.ss) {
      join(ans[i].ss.ff, ans[s.top()].ss.ff);
      s.pop();
    }
    s.push(i);
  }

  sort(ans2.begin(), ans2.end());
  stack<ll> s2;
  for (ll i = n - 1; i >= 0; i--) {
    while (!s2.empty() && ans2[i].ss.ss > ans2[s2.top()].ss.ss) {
      join(ans2[i].ss.ff, ans2[s2.top()].ss.ff);
      s2.pop();
    }
    s2.push(i);
  }
  stack<ll> s3;
  for (ll i = 0; i < n; i++) {
    while (!s3.empty() && ans[i].ss.ss < ans[s3.top()].ss.ss) {
      join(ans[i].ss.ff, ans[s3.top()].ss.ff);
      s3.pop();
    }
    s3.push(i);
  }

  stack<ll> s4;
  for (ll i = 0; i < n; i++) {
    while (!s4.empty() && ans2[i].ss.ss < ans2[s4.top()].ss.ss) {
      join(ans2[i].ss.ff, ans2[s4.top()].ss.ff);
      s4.pop();
    }
    s4.push(i);
  }

  m.clear();
  for (ll i = 0; i < n; i++) {

    m[p(i)]++;
  }
  for (ll i = 0; i < n; i++) {
    cout << m[p(i)] << endl;
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\\n'
#define mod 1000000007
#define pb push_back
#define ff first
#define ss second
#define con continue
#define ub upper_bound
#define lb lower_bound
#define sum(a) (accumulate((a).begin(), (a).end(), 0ll))
#define mine(a) (*min_element((a).begin(), (a).end()))
#define maxe(a) (*max_element((a).begin(), (a).end()))
#define mini(a) (min_element((a).begin(), (a).end()) - (a).begin())
#define maxi(a) (max_element((a).begin(), (a).end()) - (a).begin())
#define lowb(a, x) (lower_bound((a).begin(), (a).end(), (x)) - (a).begin())
#define uppb(a, x) (upper_bound((a).begin(), (a).end(), (x)) - (a).begin())

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};

const int dx8[] = {-1, 0, 1, 0, 1, 1, -1, -1};
const int dy8[] = {0, -1, 0, 1, 1, -1, 1, -1};

vector<ll> arr, rank1;
unordered_map<ll, ll> m;

ll p(ll x) {

  if (x == arr[x])
    return x;
  return arr[x] = p(arr[x]);
}
ll join(ll x, ll y) {

  x = p(x), y = p(y);
  if (rank1[x] < rank1[y])
    arr[x] = y;
  else if (rank1[x] > rank1[y])
    arr[y] = x;
  else {
    rank1[y]++;
    arr[x] = y;
  }
}

void solve() {
  ll n;
  cin >> n;
  vector<pair<ll, pair<ll, ll>>> ans;

  arr.clear();
  rank1.clear();
  for (ll i = 0; i < n; i++) {
    rank1.pb(0);
    arr.pb(i);
  }

  for (ll i = 0; i < n; i++) {
    ll x, y;
    cin >> x >> y;
    ans.pb({x * -1, {i, y}});
  }
  sort(ans.begin(), ans.end());
  stack<ll> s;
  for (ll i = n - 1; i >= 0; i--) {
    while (!s.empty() && ans[i].ss.ss > ans[s.top()].ss.ss) {
      join(ans[i].ss.ff, ans[s.top()].ss.ff);
      s.pop();
    }
    s.push(i);
  }
  m.clear();
  for (ll i = 0; i < arr.size(); i++) {

    m[p(i)]++;
  }
  for (ll i = 0; i < n; i++) {
    cout << m[p(i)] << endl;
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
}