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 Java (OpenJDK 11.0.6) (judge verdict: Wrong Answer)


import java.io.*;
import java.util.*;

public class Main {
	
	public static FastReader scan = new FastReader();

	public static void main(String[] args) {
		
//		int t = scan.nextInt();
//		
//		while(t--> 0) {
//			int n = scan.nextInt();
//			ArrayList<Integer> pos = new ArrayList<>();
//			ArrayList<Integer> neg = new ArrayList<>();
//			
//			int o = 0, m = 0;
//			
//			for(int i=0; i<n; i++) {
//				int a = scan.nextInt();
//				if(a < 0) neg.add(a);
//				else pos.add(a);
//			}
//			
//			Collections.sort(neg, Collections.reverseOrder());
//			Collections.sort(pos);
//			
//			
//			
//		}
		
		int n = scan.nextInt();
		int a[][] = new int[n][2];
		int c[] =  new int[n];
		
		for(int i=0; i<n; i++) {
			a[i][0] = scan.nextInt();
			a[i][1] = scan.nextInt();
		}
		
		int ij = 2;
		while(ij--> 0)
			for(int i=0; i<n; i++) {
				int cnt = 1;
				for(int j=0; j<n; j++) {
					if(a[i][0] < a[j][0] && a[i][1] < a[j][1]) {
						cnt++;
						if(c[j] != 0 && c[j] > cnt) {
							cnt = c[j];
							break;
						}
					}
					if(a[i][0] > a[j][0] && a[i][1] > a[j][1]) {
						cnt++;
						if(c[j] != 0 && c[j] > cnt) {
							cnt = c[j];
							break;
						}
					}
				}
				
				if(ij == 1) System.out.println(cnt);
				c[i] = cnt;
			}
		
  }

	public static int gcd(int a, int b) {
		if(b == 0) return a;
		return gcd(b, a%b);
	}
	
	public static long abs(long a, long b) {return Math.abs(a - b);}
	public static long min(long a, long b) {return Math.min(a, b);}
	public static long max(long a, long b) {return Math.max(a, b);}
	
	// sieveOfEratosthenes 
	public static boolean[] sieve(int n) {
		boolean[] prime = new boolean[n+1];
		
		Arrays.fill(prime, true);
		prime[0] = false;
		prime[1] = false;
		
		for(int i=2; i*i<n; i++) {
			if(prime[i] == true) {				
				for(int j=i*2; j<=n; j+=i) {
					prime[j] = false;
				}
			}
		}
		
		return prime;
	}
	
	//smallestFactor function
	public static int smallestFactor(int n) {
		int i;
		for(i=2; i*i<=n; i++) {
			if(n%i == 0) 
				break;
		}
		return i;
	}
	
	//isPrime 
	public static boolean isPrime(int n) {
		boolean isPrime = true;
		for(int i=2; i*i<=n; i++) if(n % i == 0) isPrime = false;
		return isPrime;
	}

	//FastReader Class
	static class FastReader 
	{ 
		BufferedReader br; 
		StringTokenizer st; 

		public FastReader() 
		{ 
			br = new BufferedReader(new
					InputStreamReader(System.in)); 
		} 

		String next() 
		{ 
			while (st == null || !st.hasMoreElements()) 
			{ 
				try
				{ 
					st = new StringTokenizer(br.readLine()); 
				} 
				catch (IOException e) 
				{ 
					e.printStackTrace(); 
				} 
			} 
			return st.nextToken(); 
		} 

		int nextInt() 
		{ 
			return Integer.parseInt(next()); 
		} 

		long nextLong() 
		{ 
			return Long.parseLong(next()); 
		} 

		double nextDouble() 
		{ 
			return Double.parseDouble(next()); 
		} 
		
		char[] nextChar() 
		{
			return next().toCharArray();
		}

		String nextLine() 
		{ 
			String str = ""; 
			try
			{ 
				str = br.readLine(); 
			} 
			catch (IOException e) 
			{ 
				e.printStackTrace(); 
			} 
			return str; 
		} 
	}  
}