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 with AC Library v1.1) (judge verdict: Accepted)

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
using ll = long long;
using ull = unsigned long long;
#define REP(i,n) for (ll i = 0; i < (n); ++i)
#define RREP(i, n) for (ll i = (n) - 1; i >= 0; --i)
#define ALL(v) (v).begin(), (v).end()
template<class T>
using reverse_priority_queue = priority_queue<T, vector<T>, greater<T>>;


struct union_find {
    vector<ll> par;
    vector<ll> rank;
    vector<ll> count;

    union_find(ll n) : par(n, 0), rank(n, 0), count(n, 1) {
        for(ll i = 0; i < n; ++i){ par.at(i) = i; }
    }

    ll find(ll x){
        if(par.at(x) == x){
            return x;
        }else{
            return par.at(x) = find(par.at(x));
        }
    }

    void unite(ll x, ll y){
        x = find(x);
        y = find(y);
        if(x == y){ return; }
        if(rank.at(x) < rank.at(y)){
            par.at(x) = y;
        }else{
            par.at(y) = x;
            if(rank.at(x) == rank.at(y)){
                rank.at(x)++;
            }
        }

        ll new_count = count.at(x) + count.at(y);
        count.at(find(x)) = new_count;
    }

    bool same(ll x, ll y){
        return find(x) == find(y);
    }

    ll size(ll x){
        return count.at(find(x));
    }
};


int main(){
    ll n;
    cin >> n;

    vector<array<ll, 3>> xy(n);
    REP(i, n){
        cin >> xy.at(i).at(0) >> xy.at(i).at(1);
        xy.at(i).at(0)--;
        xy.at(i).at(1)--;
        xy.at(i).at(2) = i;
    }
    sort(ALL(xy));

    union_find uf(n);

    {
        set<pair<ll, ll>> ys1;
        REP(i, n){
            const auto& a = xy.at(i);
            const ll idx = a.at(2);
            const ll y = a.at(1);
            auto it = ys1.lower_bound(make_pair(y, 0));
            if(it != ys1.begin()){
                --it;
                uf.unite(it->second, idx);
            }
            if(ys1.size() && ys1.begin()->first < y){
                uf.unite(ys1.begin()->second, idx);
            }

            ys1.insert(make_pair(y, idx));
        }
    }

    {
        set<pair<ll, ll>> ys2;
        RREP(i, n){
            const auto& a = xy.at(i);
            const ll idx = a.at(2);
            const ll y = a.at(1);
            auto it = ys2.lower_bound(make_pair(y, 0));
            if(it != ys2.end()){
                uf.unite(it->second, idx);
            }
            if(ys2.size() && ys2.rbegin()->first > y){
                uf.unite(ys2.rbegin()->second, idx);
            }

            ys2.insert(make_pair(y, idx));
        }
    }

    REP(i, n){
        cout << uf.size(i) << endl;
    }
    return 0;
}