Language: C++

#include <bits/stdc++.h>
// clang-format off
using namespace std; using ll = int64_t; using ull = uint64_t; const ll INF = 9e18;
void print() { cout << endl; }
template<typename Head,typename... Tail> void print(Head head,Tail... tail){cout<<head;if(sizeof...(Tail)>0)cout<<" ";print(tail...);}
void print0() {}
template<typename Head,typename... Tail> void print0(Head head,Tail... tail){cout<<head;print0(tail...);}
// clang-format on

class iunionfind {
   public:
    // 連続するuintにのみ使える。vectorであるため定数時間がとれる。
    // 基本的にこれだけあればよい。任意の構造体を扱うときは、IDと構造体のマッピングをとるvectorを別途用意する。
    // libs-test.cppの iuf_map あたりを検索。
    vector<ull> partition;
    vector<ull> rank;

    iunionfind(ull n) {
        partition.resize(n);
        rank.resize(n);
        for (ull x = 0; x < n; x++) {
            partition[x] = x;
            rank[x] = 0;
        }
    }
    ull find(ull x) {
        if (partition[x] == x) {
            return x;
        } else {
            partition[x] = find(partition[x]);
            return partition[x];
        }
    }
    void unite(ull x, ull y) {
        x = find(x);
        y = find(y);
        if (x == y) {
            return;
        }
        if (rank[x] < rank[y]) {
            partition[x] = y;
        } else {
            partition[y] = x;
            if (rank[x] == rank[y]) {
                rank[x]++;
            }
        }
    }
    bool same(ull x, ull y) {
        return find(x) == find(y);
    }
};

struct point_revy {
    ll id;
    ll x;
    ll y;
};
bool operator<(const point_revy& lhs, const point_revy& rhs) {
    return lhs.y > rhs.y;
}
bool operator==(const point_revy& lhs, const point_revy& rhs) {
    return lhs.y == rhs.y;
}
bool operator!=(const point_revy& lhs, const point_revy& rhs) {
    return !(lhs == rhs);
}
bool operator>(const point_revy& lhs, const point_revy& rhs) {
    return !(lhs == rhs) && !(lhs < rhs);
}
bool operator>=(const point_revy& lhs, const point_revy& rhs) {
    return (lhs == rhs) || (lhs > rhs);
}
bool operator<=(const point_revy& lhs, const point_revy& rhs) {
    return (lhs == rhs) || (lhs < rhs);
}

struct point_x {
    ll id;
    ll x;
    ll y;
};
bool operator<(const point_x& lhs, const point_x& rhs) {
    return lhs.x < rhs.x;
}
bool operator==(const point_x& lhs, const point_x& rhs) {
    return lhs.x == rhs.x;
}
bool operator!=(const point_x& lhs, const point_x& rhs) {
    return !(lhs == rhs);
}
bool operator>(const point_x& lhs, const point_x& rhs) {
    return !(lhs == rhs) && !(lhs < rhs);
}
bool operator>=(const point_x& lhs, const point_x& rhs) {
    return (lhs == rhs) || (lhs > rhs);
}
bool operator<=(const point_x& lhs, const point_x& rhs) {
    return (lhs == rhs) || (lhs < rhs);
}

int main() {
    ll N;
    cin >> N;

    auto uf = iunionfind(N);
    vector<point_x> points(N);
    set<point_revy> pmap_xs;
    set<point_x> pmap_yl;
    for (ll i = 0; i < N; i++) {
        ll x, y;
        cin >> x >> y;
        x--;
        y--;
        points[i] = {i, x, y};
        pmap_xs.insert({i, x, y});
        pmap_yl.insert({i, x, y});
    }
    {
        // x座標が小さい順
        sort(points.begin(), points.end(), [](point_y a, point_y b) { return a.x < b.x; });

        for (ll i = 0; i < N; i++) {
            // y座標が自分より大きい点があれば、それと同じクラスタ
            point_x pt = points[i];
            point_revy pt_y_large = *(pmap_xs.begin());

            if (pt_y_large.id != pt.id) {
                uf.unite(pt.id, pt_y_large.id);
            }

            // 以降現れないよう消す
            pmap_xs.erase({pt.id, pt.x, pt.y});
        }
    }
    {
        // y座標が大きい順
        sort(points.begin(), points.end(), [](point_y a, point_y b) { return a.y > b.y; });

        for (ll i = 0; i < N; i++) {
            // x座標が自分より小さい点があれば、それと同じクラスタ
            point_x pt = points[i];
            point_x pt_x_small = *(pmap_yl.begin());

            if (pt_x_small.id != pt.id) {
                uf.unite(pt.id, pt_x_small.id);
            }

            // 以降現れないよう消す
            pmap_yl.erase({pt.id, pt.x, pt.y});
        }
    }
    vector<vector<ll>> clusters(N);
    for (ll i = 0; i < N; i++) {
        clusters[uf.find(i)].push_back(i);
    }
    for (ll i = 0; i < N; i++) {
        print(clusters[i].size());
    }
}


Compiler output:
a.cc: In function 'int main()':
a.cc:121:47: error: 'point_y' has not been declared
  121 |         sort(points.begin(), points.end(), [](point_y a, point_y b) { return a.x < b.x; });
      |                                               ^~~~~~~
a.cc:121:58: error: 'point_y' has not been declared
  121 |         sort(points.begin(), points.end(), [](point_y a, point_y b) { return a.x < b.x; });
      |                                                          ^~~~~~~
a.cc: In lambda function:
a.cc:121:80: error: request for member 'x' in 'a', which is of non-class type 'int'
  121 |         sort(points.begin(), points.end(), [](point_y a, point_y b) { return a.x < b.x; });
      |                                                                                ^
a.cc:121:86: error: request for member 'x' in 'b', which is of non-class type 'int'
  121 |         sort(points.begin(), points.end(), [](point_y a, point_y b) { return a.x < b.x; });
      |                                                                                      ^
a.cc: In function 'int main()':
a.cc:138:47: error: 'point_y' has not been declared
  138 |         sort(points.begin(), points.end(), [](point_y a, point_y b) { return a.y > b.y; });
      |                                               ^~~~~~~
a.cc:138:58: error: 'point_y' has not been declared
  138 |         sort(points.begin(), points.end(), [](point_y a, point_y b) { return a.y > b.y; });
      |                                                          ^~~~~~~
a.cc: In lambda function:
a.cc:138:80: error: request for member 'y' in 'a', which is of non-class type 'int'
  138 |         sort(points.begin(), points.end(), [](point_y a, point_y b) { return a.y > b.y; });
      |                                                                                ^
a.cc:138:86: error: request for member 'y' in 'b', which is of non-class type 'int'
  138 |         sort(points.begin(), points.end(), [](point_y a, point_y b) { return a.y > b.y; });
      |                                                                                      ^
In file included from /usr/include/c++/14/bits/stl_algobase.h:71,
                 from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/predefined_ops.h: In instantiation of 'constexpr bool __gnu_cxx::__ops::_Iter_comp_iter<_Compare>::operator()(_Iterator1, _Iterator2) [with _Iterator1 = __gnu_cxx::__normal_iterator<point_x*, std::vector<point_x> >; _Iterator2 = __gnu_cxx::__normal_iterator<point_x*, std::vector<point_x> >; _Compare = main()::<lambda(int, int)>]':
/usr/include/c++/14/bits/stl_algo.h:1777:14:   required from 'void std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<point_x*, vector<point_x> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(int, int)> >]'
 1777 |           if (__comp(__i, __first))
      |               ~~~~~~^~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1817:25:   required from 'void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<point_x*, vector<point_x> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(int, int)> >]'
 1817 |           std::__insertion_sort(__first, __first + int(_S_threshold), __comp);
      |           ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1908:31:   required from 'void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<point_x*, vector<point_x> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(int, int)> >]'
 1908 |           std::__final_insertion_sort(__first, __last, __comp);
      |           ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:4805:18:   required from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<point_x*, vector<point_x> >; _Compare = main()::<lambda(int, int)>]'
 4805 |       std::__sort(__first, __last, __gnu_cxx::__ops::__iter_comp_iter(__comp));
      |       ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:121:13:   required from here
  121 |         sort(points.begin(), points.end(), [](point_y a, point_y b) { return a.x < b.x; });
      |         ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/predefined_ops.h:158:30: error: no match for call to '(main()::<lambda(int, int)>) (point_x&, point_x&)'
  158 |         { return bool(_M_comp(*__it1, *__it2)); }
      |                       ~~~~~~~^~~~~~~~~~~~~~~~
a.cc:121:44: note: candidate: 'main()::<lambda(int, int)>'
  121 |         sort(points.begin(), points.end(), [](point_y a, point_y b) { return a.x < b.x; });
      |                                            ^
a.cc:121:44: note:   no known conversion for argument 1 from 'point_x' to 'int'
/usr/include/c++/14/bits/predefined_ops.h: In instantiation of 'constexpr bool __gnu_cxx::__ops::_Iter_comp_iter<_Compare>::operator()(_Iterator1, _Iterator2) [with _Iterator1 = __gnu_cxx::__normal_iterator<point_x*, std::vector<point_x> >; _Iterator2 = __gnu_cxx::__normal_iterator<point_x*, std::vector<point_x> >; _Compare = main()::<lambda(int, int)>]':
/usr/include/c++/14/bits/stl_algo.h:1777:14:   required from 'void std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<point_x*, vector<point_x> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(int, int)> >]'
 1777 |           if (__comp(__i, __first))
      |               ~~~~~~^~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1817:25:   required from 'void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<point_x*, vector<point_x> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(int, int)> >]'
 1817 |           std::__insertion_sort(__first, __first + int(_S_threshold), __comp);
      |           ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1908:31:   required from 'void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<point_x*, vector<point_x> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(int, int)> >]'
 1908 |           std::__final_insertion_sort(__first, __last, __comp);
      |           ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:4805:18:   required from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<point_x*, vector<point_x> >; _Compare = main()::<lambda(int, int)>]'
 4805 |       std::__sort(__first, __last, __gnu_cxx::__ops::__iter_comp_iter(__comp));
      |       ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:138:13:   required from here
  138 |         sort(points.begin(), points.end(), [](point_y a, point_y b) { return a.y > b.y; });
      |         ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/predefined_ops.h:158:30: error: no match for call to '(main()::<lambda(int, int)>) (point_x&, point_x&)'
  158 |         { return bool(_M_comp(*__it1, *__it2)); }
      |                       ~~~~~~~^~~~~~~~~~~~~~~~
a.cc:138:44: note: candidate: 'main()::<lambda(int, int)>'
  138 |         sort(points.begin(), points.end(), [](point_y a, point_y b) { return a.y > b.y; });
      |                                            ^
a.cc:138:44: note:   no known conversion for argument 1 from 'point_x' to 'int'
/usr/include/c++/14/bits/predefined_ops.h: In instantiation of 'bool __gnu_cxx::__ops::_Val_comp_iter<_Compare>::operator()(_Value&, _Iterator) [with _Value = point_x; _Iterator = __gnu_cxx::__normal_iterator<point_x*, std::vector<point_x> >; _Compare = main()::<lambda(int, int)>]':
/usr/include/c++/14/bits/stl_algo.h:1757:20:   required from 'void std::__unguarded_linear_insert(_RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<point_x*, vector<point_x> >; _Compare = __gnu_cxx::__ops::_Val_comp_iter<main()::<lambda(int, int)> >]'
 1757 |       while (__comp(__val, __next))
      |              ~~~~~~^~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1785:36:   required from 'void std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<point_x*, vector<point_x> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(int, int)> >]'
 1785 |             std::__unguarded_linear_insert(__i,
      |             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~
 1786 |                                 __gnu_cxx::__ops::__val_comp_iter(__comp));
      |                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1817:25:   required from 'void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<point_x*, vector<point_x> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(int, int)> >]'
 1817 |           std::__insertion_sort(__first, __first + int(_S_threshold), __comp);
      |           ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1908:31:   required from 'void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<point_x*, vector<point_x> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(int, int)> >]'
 1908 |           std::__final_insertion_sort(__first, __last, __comp);
      |           ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:4805: