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 Rust (1.42.0) (judge verdict: Accepted)

#![allow(unused_imports)]
use {
    itertools::Itertools,
    proconio::{fastout, input, marker::*},
    std::cmp::*,
    std::collections::*,
    std::ops::*,
};

#[allow(unused_macros)]
macro_rules !dbg {($($e :expr ) ,*) =>{#[cfg (debug_assertions ) ] $({let (e ,mut err ) =(stringify !($e ) ,std ::io ::stderr () ) ;writeln !(err ,"{} = {:?}" ,e ,$e ) .unwrap () } ) *} ;}

struct UnionFind {
    n: usize,
    par: Vec<usize>,
    rank: Vec<usize>,
}
impl UnionFind {
    fn new(n: usize) -> Self {
        UnionFind {
            n,
            par: (0..n).collect(),
            rank: vec![1; n],
        }
    }
    fn unite(&mut self, x: usize, y: usize) {
        if self.same(x, y) {
            return;
        }
        let rx = self.par[x];
        let ry = self.par[y];
        if self.rank[rx] > self.rank[ry] {
            self.par[ry] = rx;
            self.rank[rx] += self.rank[ry];
        } else {
            self.par[rx] = ry;
            self.rank[ry] += self.rank[rx];
        }
    }
    fn same(&mut self, x: usize, y: usize) -> bool {
        self.root(x) == self.root(y)
    }
    fn root(&mut self, x: usize) -> usize {
        if self.par[x] == x {
            x
        } else {
            self.par[x] = self.root(self.par[x]);
            self.par[x]
        }
    }
    #[allow(dead_code)]
    fn size(&mut self, x: usize) -> usize {
        let r = self.root(x);
        self.rank[r]
    }
    #[allow(dead_code)]
    fn sizes(&mut self) -> Vec<usize> {
        let mut s = vec![];
        for x in 0..self.n {
            if x == self.root(x) {
                s.push(self.rank[x]);
            }
        }
        s
    }
}

#[fastout]
fn main() {
    input! {
       n: usize,
       xy: [(Usize1, Usize1); n]
    }
    let mut xyi = xy
        .iter()
        .enumerate()
        .map(|(i, &(x, y))| (x, y, i))
        .collect::<Vec<_>>();
    xyi.sort();
    let mut uf = UnionFind::new(n);
    let mut s = vec![xyi[0].1];
    for &(_, y, _) in xyi.iter().skip(1) {
        if s[s.len() - 1] < y {
            let yi = s[s.len() - 1];
            let mut yj = None;
            while let Some(s) = s.pop() {
                if s > y {
                    yj = Some(s);
                    break;
                }
                uf.unite(s, y);
            }
            if let Some(yj) = yj {
                s.push(yj);
            }
            s.push(yi);
        } else {
            s.push(y);
        }
    }
    let mut ans = vec![0; n];
    for &(_, y, i) in xyi.iter() {
        ans[i] = uf.size(y);
    }
    println!("{}", ans.iter().format("\n"));
}