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)

#include <bits/stdc++.h>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

struct UnionFind{
  vector<int64_t> S;
  vector<int64_t> weight;
  
  UnionFind(int N){
    S = vector<int64_t>(N,0);
    weight = vector<int64_t>(N,1);
    for(int i = 0 ; i < N ; i++){
      S.at(i) = i;
      weight.at(i) = 1;
    }
  }
  
  int root(int x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
    if(S.at(x) == x){
     return x; 
    }
    return S.at(x) = root(S.at(x));
  }
  
  void unite(int x, int y) { // xとyの木を併合
    int rx = root(x); //xの根をrx
    int ry = root(y); //yの根をry
    //cout << rx << ":" << ry <<endl;
    if (rx == ry){
      return;
    }
    if (S.at(x) > S.at(y)){
      swap(x, y);
    }
    //cout << x << ":" << y <<endl;
    weight.at(S.at(x)) += weight.at(S.at(y));
    S.at(y) = S.at(x);
    return; //xとyの根が同じでない(=同じ木にない)時：xの根rxをyの根ryにつける
  }
  
  int size(int x){
    return weight.at(x);
  }
  
  //bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
    //int rx = root(x);
    //int ry = root(y);
    //return rx == ry;
  //}
  
};

int main(){//main_program
  int N;
  cin >> N;
  vector<tuple<int, int, int>> S(N);
  vector<pair<int, int>> H(N);
  
  for(int i = 0;i < N;i++){
    int a,b;
    cin >> a >> b;
    S.at(i) = make_tuple(a, b, i);
  }
  sort(S.begin(), S.end());
  
   for (int i = 0; i < N; i++) {
    int b, a,c;
    tie(a,b,c) = S.at(i);  // b, a の順であることに注意
    //cout << a << " " << b <<":" << c << endl;
     H.at(i) = make_pair(c,i);
  }
  sort(H.begin(), H.end());
  
  //for (int i = 0; i < N; i++) {
    //int a,b;
    //tie(a,b) = H.at(i);  // b, a の順であることに注意
    //cout << a << ":" << b << endl;
  //}
  
  for (int i = 0; i < N; i++) {
    int a,b;
    int count = 1;
    tie(a,b) = H.at(i);
    int x,y,z;
    tie(x,y,z) = S.at(b);
    for(int l = 0; l < b; l++){
      int o,p,q;
      tie(o,p,q) = S.at(l);
      //cout << o << " " << p <<":" << q << endl;
      if( p< y){
        //cout <<":" << b << endl;
        count++;
      }
    }
    for(int l = b+1; l < N; l++){
      int o,p,q;
      tie(o,p,q) = S.at(l);
      //cout << o << " " << p <<":" << q << endl;
      if(y < p){
        //cout <<":" << b << endl;
        count++;
      }
    }
    cout << count << endl;
  }
  
}