import sys
import numpy as np
import numba
from numba import njit, b1, i4, i8, f8
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
uf_t = numba.types.UniTuple(i8[:], 2)
@njit((uf_t, i8), cache=True)
def find_root(uf, x):
root = uf[0]
while root[x] != x:
root[x] = root[root[x]]
x = root[x]
return x
@njit((uf_t, i8, i8), cache=True)
def merge(uf, x, y):
root, size = uf
x, y = find_root(uf, x), find_root(uf, y)
if x == y:
return False
if size[x] < size[y]:
x, y = y, x
size[x] += size[y]
root[y] = root[x]
return True
@njit((i8[:], ), cache=True)
def build(raw_data):
bit = raw_data.copy()
for i in range(len(bit)):
j = i + (i & (-i))
if j < len(bit):
bit[j] += bit[i]
return bit
@njit((i8[:], i8), cache=True)
def get_sum(bit, i):
s = 0
while i:
s += bit[i]
i -= i & -i
return s
@njit((i8[:], i8, i8), cache=True)
def add(bit, i, x):
while i < len(bit):
bit[i] += x
i += i & -i
@njit((i8[:], i8), cache=True)
def find_kth_element(bit, k):
N = len(bit)
x, sx = 0, 0
dx = 1
while 2 * dx < N:
dx *= 2
while dx:
y = x + dx
if y < N:
sy = sx + bit[y]
if sy < k:
x, sx = y, sy
dx //= 2
return x + 1
@njit((i8[:], ), cache=True)
def main(XY):
N = len(XY) // 2
root = np.arange(N + 1, dtype=np.int64)
size = np.ones_like(root)
uf = (root, size)
XtoY = np.zeros(N + 1, np.int64)
YtoX = np.zeros(N + 1, np.int64)
for i in range(0, N + N, 2):
x, y = XY[i:i + 2]
XtoY[x] = y
YtoX[y] = x
# 残してある y 座標集合を bit で管理
bit_raw = np.ones(N + 1, np.int64)
bit_raw[0] = 0
bit = build(bit_raw)
rest_Y = np.ones(N + 1, np.int64)
rest_Y_cnt = N
for x in range(1, N + 1):
y = XtoY[x]
if not rest_Y[y]:
continue
k = get_sum(bit, y)
largest_x = x
for i in range(k + 1, rest_Y_cnt + 1):
y1 = find_kth_element(bit, i)
x1 = YtoX[y1]
merge(uf, x, x1)
largest_x = max(largest_x, x1)
for i in range(k + 1, rest_Y_cnt + 1):
y1 = find_kth_element(bit, k + 1)
x1 = YtoX[y1]
if x1 != largest_x:
rest_Y[y1] = 0
rest_Y_cnt -= 1
add(bit, y1, -1)
rest_Y[y] = 0
rest_Y_cnt -= 1
add(bit, y, -1)
for i in range(0, N + N, 2):
x, y = XY[i:i + 2]
rx = find_root(uf, x)
print(size[rx])
N = int(readline())
XY = np.array(read().split(), np.int64)
main(XY)