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 Ruby (2.7.1) (judge verdict: Time Limit Exceeded)

END { load __FILE__ unless $stdin.eof? }

class SCC
  def initialize(n = 0)
    @n = n
    @edges = []
  end

  def add_edge(from, to)
    @edges << [from, to]
    self
  end

  def scc
    group_num, ids = scc_ids
    groups = Array.new(group_num) { [] }
    ids.each_with_index { |id, i| groups[id] << i }
    groups
  end

  private

  def scc_ids
    g_start = Array.new(@n + 1, 0)
    g_elist = Array.new(@edges.size)
    @edges.each { |from, _| g_start[from+1] += 1 }
    1.upto(@n) { |i| g_start[i] += g_start[i-1] }
    g_start.dup.tap do |counter|
      @edges.each do |from, to|
        g_elist[counter[from]] = to
        counter[from] += 1
      end
    end

    now_ord = 0
    group_num = 0
    visited = []
    low = Array.new(@n)
    ord = Array.new(@n)
    ids = Array.new(@n)

    dfs = lambda do |this, v|
      low[v] = ord[v] = now_ord
      now_ord += 1
      visited << v

      (g_start[v]...g_start[v+1]).each do |i|
        to = g_elist[i]
        if ord[to]
          low[v] = [low[v], ord[to]].min
        else
          this.call(this, to)
          low[v] = [low[v], low[to]].min
        end
      end

      if low[v] == ord[v]
        begin
          u = visited.pop
          ord[u] = @n
          ids[u] = group_num
        end until u == v
        group_num += 1
      end
    end

    @n.times { |i| dfs.call(dfs, i) unless ord[i] }
    ids.map! { |x| group_num - 1 - x }

    [group_num, ids]
  end
end

n = gets.to_i
towns = Array.new(n) { |i| gets.split.map!(&:to_i) << i }.sort!

scc = SCC.new(n)
while (tx, ty, tid = towns.shift)
  tymax = n
  loop do
    town = towns.find { |x, y| y > ty && y <= tymax }
    break unless town
    scc.add_edge(tid, town[2])
    scc.add_edge(town[2], tid)
    tymax = town[1] - 1
  end
end

group_num, ids = scc.send(:scc_ids)
groups = Array.new(group_num) { [] }
ids.each_with_index { |id, i| groups[id] << i }
groups.map!(&:size)

n.times do |k|
  puts groups[ids[k]]
end