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 Go (1.14.1) (judge verdict: Wrong Answer)

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
)

var sc, wr = bufio.NewScanner(os.Stdin), bufio.NewWriter(os.Stdout)

func scanString() string   { sc.Scan(); return sc.Text() }
func scanRunes() []rune    { return []rune(scanString()) }
func scanInt() int         { a, _ := strconv.Atoi(scanString()); return a }
func scanInt64() int64     { a, _ := strconv.ParseInt(scanString(), 10, 64); return a }
func scanFloat64() float64 { a, _ := strconv.ParseFloat(scanString(), 64); return a }

func scanInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = scanInt()
	}
	return a
}

func debug(a ...interface{}) {
	if os.Getenv("ONLINE_JUDGE") == "false" {
		fmt.Fprintln(os.Stderr, a...)
	}
}

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}
func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}
func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

//•*¨*•.¸¸♪main•*¨*•.¸¸♪(　-ω-)ノ　(　・ω・)
func main() {
	defer wr.Flush()
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 10000), 1001001)

	n := scanInt()
	p := make([]pair, n)
	t := make([]int, n)
	for i := 0; i < n; i++ {
		p[i] = pair{scanInt() - 1, scanInt() - 1, i}
		t[p[i].b] = p[i].k
	}
	sort.Slice(p, func(i, j int) bool { return p[i].a < p[j].a })

	b1 := newBinaryIndexedTree(n)
	b2 := newBinaryIndexedTree(n)

	uf := newUnionFind(n)

	for i := 0; i < n; i++ {
		_, y, k := p[i].a, p[i].b, p[i].k
		a := b1.sum(y)
		if a != 0 {
			id := b1.lowerBound(a)
			uf.unite(k, t[id])
		}
		b1.add(y, 1)
	}
	for i := 0; i < n; i++ {
		_, y, k := p[n-1-i].a, p[n-1-i].b, p[n-1-i].k
		a := b2.sum(y) + 1
		id := b2.lowerBound(a)
		if id != n {
			uf.unite(k, t[id])
		}
		b2.add(y, 1)
	}

	for i := 0; i < n; i++ {
		fmt.Fprintln(wr, uf.size(i))
	}
}

type pair struct{ a, b, k int }

type binaryIndexedTree []int

func newBinaryIndexedTree(n int) *binaryIndexedTree {
	bit := make(binaryIndexedTree, n+1)
	return &bit
}

func (t binaryIndexedTree) sum(i int) (s int) {
	for i++; i > 0; i -= i & -i {
		s += t[i]
	}
	return
}

func (t binaryIndexedTree) add(i, x int) {
	for i++; i < len(t) && i > 0; i += i & -i {
		t[i] += x
	}
}

func (t binaryIndexedTree) lowerBound(x int) int {
	idx, k := 0, 1
	for k < len(t) {
		k <<= 1
	}
	for k >>= 1; k > 0; k >>= 1 {
		if idx+k < len(t) && t[idx+k] < x {
			x -= t[idx+k]
			idx += k
		}
	}
	return idx
}

type unionFind []int

func newUnionFind(n int) *unionFind {
	uf := make(unionFind, n)
	for i := 0; i < n; i++ {
		uf[i] = -1
	}
	return &uf
}

func (uf unionFind) find(x int) int {
	if uf[x] < 0 {
		return x
	}
	uf[x] = uf.find(uf[x])
	return uf[x]
}

func (uf unionFind) unite(x, y int) {
	x, y = uf.find(x), uf.find(y)
	if x != y {
		if uf[x] > uf[y] {
			x, y = y, x
		}
		uf[x] += uf[y]
		uf[y] = x
	}
}

func (uf unionFind) size(x int) int {
	return -uf[uf.find(x)]
}