Language: C++

#include <iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<math.h>
using namespace std;
typedef long long ll;
#define int long long
#define double long double
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<pii> VP;
typedef vector<string> VS;
typedef priority_queue<int> PQ;
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 (b < a) { a = b; return 1; } return 0; }
#define fore(i,a) for(auto &i:a)
#define REP(i,n) for(int i=0;i<n;i++)
#define eREP(i,n) for(int i=0;i<=n;i++)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define eFOR(i,a,b) for(int i=(a);i<=(b);++i)
#define SORT(c) sort((c).begin(),(c).end())
#define rSORT(c) sort((c).rbegin(),(c).rend())
#define LB(x,a) lower_bound((x).begin(),(x).end(),(a))
#define UB(x,a) upper_bound((x).begin(),(x).end(),(a))
#define INF 1000000000
#define LLINF 9223372036854775807
#define mod 1000000007
#define eps 1e-12 
//priority_queue<int,vector<int>, greater<int> > q2;

struct UnionFind {
	VI par, s;
	UnionFind(int N) :par(N), s(N) {
		REP(i, N)par[i] = i;
		REP(i, N)s[i] = 1;
	}
	int root(int x) {
		if (par[x] == x)return x;
		return par[x] = root(par[x]);
	}
	void unite(int x, int y) {
		int rx = root(x);
		int ry = root(y);
		if (rx == ry)return;
		if (!(same(x, y)))s[ry] += s[rx];
		par[rx] = ry;
	}
	bool same(int x, int y) {
		int rx = root(x);
		int ry = root(y);
		return rx == ry;
	}
	int size(int x) {
		return s[root(x)];
	}
};

signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	int N; cin >> N;
	VP A(N);
	VI a(N);
	UnionFind uni(N);
	REP(i, N) {
		cin >> A[i].first >> A[i].second;
		A[i].first--;
		A[i].second--;
		a[A[i].second] = i;
	}
	SORT(A);
	vector<bool>F(N, false);
	set<int>S;
	VI to(N);
	REP(i, N)to[A[i].second] = i;
	REP(i, N)S.insert(i);
	REP(i, N) {
		if (F[a[A[i].first]])continue;
		F[a[A[i].first]] = 1;
		int now = A[i].second;
		auto it = S.lower_bound(now);
		VI tmp;
		tmp.push_back(*it);
		it++;

		while (it != S.end()) {
			if (!uni.same(a[A[i].second], a[*it]))uni.unite(a[A[i].second], a[*it]);
			F[a[*it]] = 1;
			tmp.push_back(*it);
			//cout << i << " " << *it << " " << to[*it] << endl;
			it++;
		}
		it = S.lower_bound(now);
		fore(j, tmp)S.erase(j);
	}
	reverse(A.begin(), A.end());
	S.clear();
	REP(i, N) {
		S.insert(i);
		F[i] = 0;
	}
	REP(i, N) {
		if (F[a[A[N - i - 1].first]])continue;
		F[a[A[N-1-i].first]] = 1;
		int now = A[i].second;
		auto it = S.lower_bound(now);

		VI tmp;
		while (it != S.begin()) {
			if (!uni.same(a[A[N-1-i].second].first]], a[*it]))uni.unite(a[A[N - 1 - i].second], a[*it]);
			F[a[*it]] = 1;
			tmp.push_back(*it);
			//cout << i << " " << *it << " " << to[*it] << endl;
			it--;
		}
		if (!uni.same(a[A[N - 1 - i].second], a[*it]))uni.unite(a[A[N - 1 - i].second], a[*it]);
		F[a[*it]] = 1;
		tmp.push_back(*it);
		//cout << i << " " << *it << " " << to[*it] << endl;
		fore(j, tmp)S.erase(j);
	}

	VI ans(N);
	REP(i, N)ans[a[A[i].second]] = uni.size(i);
	REP(i, N) cout << ans[i] << '\n';
	return 0;
}



Compiler output:
a.cc: In function 'int main()':
a.cc:116:58: error: request for member 'first' in 'a.std::vector<long long int>::operator[](((std::vector<long long int>::size_type)A.std::vector<std::pair<long long int, long long int> >::operator[](((std::vector<std::pair<long long int, long long int> >::size_type)((N - 1) - i))).std::pair<long long int, long long int>::second))', which is of non-class type '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'}
  116 |                         if (!uni.same(a[A[N-1-i].second].first]], a[*it]))uni.unite(a[A[N - 1 - i].second], a[*it]);
      |                                                          ^~~~~
a.cc:116:63: error: expected ')' before ']' token
  116 |                         if (!uni.same(a[A[N-1-i].second].first]], a[*it]))uni.unite(a[A[N - 1 - i].second], a[*it]);
      |                            ~                                  ^
      |                                                               )
a.cc:116:63: error: expected primary-expression before ']' token