Problem:
Score : 300 points

Problem Statement

There are N cities on a 2D plane. The coordinate of the i-th city is (x_i, y_i). Here (x_1, x_2, \dots, x_N) and (y_1, y_2, \dots, y_N) are both permuations of (1, 2, \dots, N).

For each k = 1,2,\dots,N, find the answer to the following question:

Rng is in City k.
Rng can perform the following move arbitrarily many times:

move to another city that has a smaller x-coordinate and a smaller y-coordinate, or a larger x-coordinate and a larger y-coordinate, than the city he is currently in.

How many cities (including City k) are reachable from City k?

Constraints

1 \leq N \leq 200,000

(x_1, x_2, \dots, x_N) is a permutation of (1, 2, \dots, N).

(y_1, y_2, \dots, y_N) is a permutation of (1, 2, \dots, N).

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
x_2 y_2
:
x_N y_N

Output

Print N lines. In i-th line print the answer to the question when k = i.

Sample Input 1

4
1 4
2 3
3 1
4 2

Sample Output 1

1
1
2
2

Rng can reach City 4 from City 3, or conversely City 3 from City 4.

Sample Input 2

7
6 4
4 3
3 5
7 1
2 7
5 2
1 6

Sample Output 2

3
3
1
1
2
3
2

Solution in C++ (GCC 9.2.1) (judge verdict: Wrong Answer)

#ifdef xay5421
#define D(...) fprintf(stderr,__VA_ARGS__)
#else
#define D(...) ((void)0)
#define NDEBUG
#endif
#include<bits/stdc++.h>
#define PB push_back
#define SZ(x) ((int)(x).size())
#define D(...) fprintf(stderr,__VA_ARGS__)
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
using namespace std;
const int N=200005,INF=0X3F3F3F3F;
int n,A[N],B[N],dis[N],fa[N],ans[N];
vector<int>G[N];
bool vis[N];
int fd(int k1){return fa[k1]==k1?k1:fa[k1]=fd(fa[k1]);}
int t[N*4],res;
int mymin(int k1,int k2){
	return B[k1]<B[k2]?k1:k2;
}
void bud(int k1,int k2,int k3){
	if(k2==k3){
		if(G[k2].empty())t[k1]=0;
		else t[k1]=G[k2].back();
		return;
	}
	int mid=(k2+k3)>>1;
	bud(k1*2,k2,mid),bud(k1*2+1,mid+1,k3);
	t[k1]=mymin(t[k1*2],t[k1*2+1]);
}
void qry(int k1,int k2,int k3,int k4,int k5){
	if(k2>k5||k3<k4)return;
	if(k4<=k2&&k3<=k5){
		res=mymin(res,t[k1]);
		return;
	}
	int mid=(k2+k3)>>1;
	qry(k1*2,k2,mid,k4,k5),qry(k1*2+1,mid+1,k3,k4,k5);
}
void del(int k1,int k2,int k3,int k4){
	if(k2==k3){
		G[k2].pop_back();
		if(G[k2].empty())t[k1]=0;
		else t[k1]=G[k2].back();
		return;
	}
	int mid=(k2+k3)>>1;
	if(k4<=mid)del(k1*2,k2,mid,k4);else del(k1*2+1,mid+1,k3,k4);
	t[k1]=mymin(t[k1*2],t[k1*2+1]);
}
bool cmp(const int&k1,const int&k2){
	return A[k1]^A[k2]?A[k1]>A[k2]:B[k1]>B[k2];
}
int id[N];
int main(){
	scanf("%d",&n);
	rep(i,1,n){
		scanf("%d%d",A+i,B+i);
		fa[i]=i;
		id[i]=i;
	}
	sort(id+1,id+1+n,cmp);
	B[0]=n+1;
	rep(i,1,n)G[A[i]].PB(i);
	rep(i,1,n)sort(G[i].begin(),G[i].end(),[&](int k1,int k2){return B[k1]>B[k2];});
	bud(1,1,n);
	rep(_,1,n){
		int i=id[_];
		if(!vis[i]){
			queue<int>q;
			q.push(i);
			while(q.size()){
				int k1=q.front();q.pop();
				vis[k1]=1;
				int x=A[k1],y=B[k1];
				while(1){
					res=0;
					qry(1,1,n,1,x);
					if(B[res]>y)break;
					D("%d -> %d\n",k1,res);
					fa[fd(k1)]=fd(res);
					q.push(res);
					del(1,1,n,A[res]);
				}
			}
		}
	}
	rep(i,1,n)++ans[fd(i)];
	rep(i,1,n)printf("%d\n",ans[fd(i)]);
	return 0;
}