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# (.NET Core 3.1.201) (judge verdict: Wrong Answer)

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using AtCoderLibraryCSharp;

namespace Tasks
{
    public class A
    {
        static void Main(string[] args)
        {
            var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
            Console.SetOut(sw);
            Solve();
            Console.Out.Flush();
        }

        public static void Solve()
        {
            var N = Scanner.Scan<int>();
            var IXY = new (int I, int X, int Y)[N];
            for (var i = 0; i < N; i++)
            {
                var (x, y) = Scanner.Scan<int, int>();
                IXY[i] = (i, x, y);
            }

            var dsu = new DisjointSetUnion(N);
            var remains = IXY.ToArray();
            while (remains.Any())
            {
                var item = remains[0];
                var next = new List<(int I, int X, int Y)>();
                foreach (var rem in remains)
                {
                    if (rem.I == item.I) continue;
                    if (item.X < rem.X && item.Y < rem.Y ||
                    item.X > rem.X && item.Y > rem.Y)
                        dsu.Merge(item.I, rem.I);
                    else next.Add(rem);
                }

                remains = next.ToArray();
            }

            for (var k = 0; k < N; k++)
            {
                Console.WriteLine(dsu.SizeOf(k));
            }
        }

        public static class Scanner
        {
            private static Queue<string> queue = new Queue<string>();
            public static T Next<T>()
            {
                if (!queue.Any()) foreach (var item in Console.ReadLine().Trim().Split(" ")) queue.Enqueue(item);
                return (T)Convert.ChangeType(queue.Dequeue(), typeof(T));
            }
            public static T Scan<T>() => Next<T>();
            public static (T1, T2) Scan<T1, T2>() => (Next<T1>(), Next<T2>());
            public static (T1, T2, T3) Scan<T1, T2, T3>() => (Next<T1>(), Next<T2>(), Next<T3>());
            public static (T1, T2, T3, T4) Scan<T1, T2, T3, T4>() => (Next<T1>(), Next<T2>(), Next<T3>(), Next<T4>());
            public static (T1, T2, T3, T4, T5) Scan<T1, T2, T3, T4, T5>() => (Next<T1>(), Next<T2>(), Next<T3>(), Next<T4>(), Next<T5>());
            public static IEnumerable<T> ScanEnumerable<T>() => Console.ReadLine().Trim().Split(" ").Select(x => (T)Convert.ChangeType(x, typeof(T)));
        }
    }
}

namespace AtCoderLibraryCSharp
{
    public class DisjointSetUnion
    {
        private readonly int _n;
        private readonly int[] _parentOrSize;

        public DisjointSetUnion(int n = 0)
        {
            _n = n;
            _parentOrSize = Enumerable.Repeat(-1, n).ToArray();
        }

        public int Merge(int a, int b)
        {
            if (a < 0 || _n <= a) throw new IndexOutOfRangeException(nameof(a));
            if (b < 0 || _n <= b) throw new IndexOutOfRangeException(nameof(b));
            var (x, y) = (LeaderOf(a), LeaderOf(b));
            if (x == y) return x;
            if (-_parentOrSize[x] < -_parentOrSize[y]) (x, y) = (y, x);
            _parentOrSize[x] += _parentOrSize[y];
            _parentOrSize[y] = x;
            return x;
        }

        public bool IsSame(int a, int b)
        {
            if (a < 0 || _n <= a) throw new IndexOutOfRangeException(nameof(a));
            if (b < 0 || _n <= b) throw new IndexOutOfRangeException(nameof(b));
            return LeaderOf(a) == LeaderOf(b);
        }

        public int LeaderOf(int a)
        {
            if (a < 0 || _n <= a) throw new IndexOutOfRangeException(nameof(a));
            if (_parentOrSize[a] < 0) return a;
            return _parentOrSize[a] = LeaderOf(_parentOrSize[a]);
        }

        public int SizeOf(int a)
        {
            if (a < 0 || _n <= a) throw new IndexOutOfRangeException(nameof(a));
            return -_parentOrSize[LeaderOf(a)];
        }

        public IEnumerable<IEnumerable<int>> GetGroups()
        {
            var leaders = new int[_n];
            var groupSize = new int[_n];
            for (var i = 0; i < _n; i++)
            {
                leaders[i] = LeaderOf(i);
                groupSize[leaders[i]]++;
            }

            var ret = new List<int>[_n].Select(x => new List<int>()).ToArray();
            for (var i = 0; i < _n; i++) ret[leaders[i]].Add(i);
            return ret.Where(x => x.Any());
        }
    }
}