Language: C++

#include <cstdio> 
#include <vector>
#include <algorithm>
#include <functional> 
using namespace std;

struct Rook {
    int x, y, id;
    bool operator <(const Rook& other) const {
        return x < other.x;
    }
};

long long hack;

template<class C>
void min_self (C &a, C &&b) {
    a = min(a, b);
};

int main() {
    int n;
    scanf("%d", &n);
    vector<Rook> rooks(n);
    for(int i = 0; i < n; ++i) {
        rooks[i].id = i;
        scanf("%d%d", &rooks[i].x, &rooks[i].y);
    }
    sort(rooks.begin(), rooks.end());
    vector<int> ys{INT_MIN, INT_MAX}; // two sentinels for convenience
    for(int i = 0; i < n; ++i) {
        ys.push_back(rooks[i].y);
    }
    sort(ys.begin(), ys.end());
    vector< pair<long long, long long> > answer(n, {-1, -1});
    for(int start = 0; start < n; ++start) {
        if(answer[rooks[start].id].first != -1) {
            continue;
        }

        int start_y = lower_bound(ys.begin(), ys.end(), rooks[start].y) - ys.begin();
        int y1 = start_y - 1, y2 = start_y + 1;

        int L = start, R = start;
        int prev_dir = -1;
        vector< pair<int, int> > intervals;
        while(true) {
            bool found = false;
            for(int i : {L - 1, R + 1}) {
                if(0 <= i && i < n) {
                    bool prev_y = (ys[y1] == rooks[i].y);
                    bool next_y = (ys[y2] == rooks[i].y);
                    if(prev_y || next_y) {
                        int direction = (prev_y == (i == L - 1));
                        if(prev_dir == -1) {
                            prev_dir = direction;
                        }
                        if(prev_dir != direction) {
                            intervals.emplace_back(L, R);
                            prev_dir = direction;
                        }
                        prev_y ? y1-- : y2++;
                        i == L-1 ? L-- : R++;
                        found = true;
                        break;
                    }
                }
            }
            if(!found) {
                break;
            }
        }
        if(intervals.empty() || intervals.back() != make_pair(L, R)) {
            intervals.emplace_back(L, R);
        }
        const int s = intervals.size();

        const long long INF = 1e18L + 5;
        
        auto f = [&] (int i, int j) {
            return abs(rooks[i].x - rooks[j].x) + abs(rooks[i].y - rooks[j].y);
        };
        
        vector< vector<long long> > dp(s, vector<long long>(2, INF));
        dp[s-1][0] = dp[s-1][1] = 0;
        for(int who = s - 1; who >= 1; --who) {
            int L1 = intervals[who].first;
            int L2 = intervals[who-1].first;
            int R1 = intervals[who-1].second;
            int R2 = intervals[who].second;
            hack += (long long) (L2 - L1) * (R2 - R1);
            min_self(dp[who-1][0], dp[who][0] + (R1 == R2 ? f(L1, L2) : f(L1, R2) + f(R2, L2)));
            min_self(dp[who-1][1], dp[who][0] + f(L1, R2) + f(R2, R1));
            min_self(dp[who-1][0], dp[who][1] + f(R2, L1) + f(L1, L2));
            min_self(dp[who-1][1], dp[who][1] + (L1 == L2 ? f(R1, R2) : f(R2, L1) + f(L1, R1)));
        }
        int low = intervals[0].first, high = intervals[0].second;
        for(int i = low; i <= high; ++i) {
            answer[rooks[i].id] = {
                R - L,
                min(dp[0][0] + f(low, high) + f(high, i), dp[0][1] + f(high, low) + f(low, i))
            };
        }
    }
    for(auto p : answer) {
        printf("%lld\n", p.second - p.first);
    }
}

Compiler output:
a.cc: In function 'int main()':
a.cc:30:20: error: 'INT_MIN' was not declared in this scope
   30 |     vector<int> ys{INT_MIN, INT_MAX}; // two sentinels for convenience
      |                    ^~~~~~~
a.cc:5:1: note: 'INT_MIN' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'
    4 | #include <functional>
  +++ |+#include <climits>
    5 | using namespace std;
a.cc:30:29: error: 'INT_MAX' was not declared in this scope
   30 |     vector<int> ys{INT_MIN, INT_MAX}; // two sentinels for convenience
      |                             ^~~~~~~
a.cc:30:29: note: 'INT_MAX' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'
a.cc:30:36: error: no matching function for call to 'std::vector<int>::vector(<brace-enclosed initializer list>)'
   30 |     vector<int> ys{INT_MIN, INT_MAX}; // two sentinels for convenience
      |                                    ^
In file included from /usr/include/c++/14/vector:66,
                 from a.cc:2:
/usr/include/c++/14/bits/stl_vector.h:707:9: note: candidate: 'template<class _InputIterator, class> std::vector<_Tp, _Alloc>::vector(_InputIterator, _InputIterator, const allocator_type&) [with <template-parameter-2-2> = _InputIterator; _Tp = int; _Alloc = std::allocator<int>]'
  707 |         vector(_InputIterator __first, _InputIterator __last,
      |         ^~~~~~
/usr/include/c++/14/bits/stl_vector.h:707:9: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/stl_vector.h:678:7: note: candidate: 'std::vector<_Tp, _Alloc>::vector(std::initializer_list<_Tp>, const allocator_type&) [with _Tp = int; _Alloc = std::allocator<int>; allocator_type = std::allocator<int>]'
  678 |       vector(initializer_list<value_type> __l,
      |       ^~~~~~
/usr/include/c++/14/bits/stl_vector.h:678:7: note:   conversion of argument 1 would be ill-formed:
/usr/include/c++/14/bits/stl_vector.h:659:7: note: candidate: 'std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>&&, std::__type_identity_t<_Alloc>&) [with _Tp = int; _Alloc = std::allocator<int>; std::__type_identity_t<_Alloc> = std::allocator<int>]'
  659 |       vector(vector&& __rv, const __type_identity_t<allocator_type>& __m)
      |       ^~~~~~
/usr/include/c++/14/bits/stl_vector.h:659:7: note:   conversion of argument 1 would be ill-formed:
/usr/include/c++/14/bits/stl_vector.h:640:7: note: candidate: 'std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>&&, const allocator_type&, std::false_type) [with _Tp = int; _Alloc = std::allocator<int>; allocator_type = std::allocator<int>; std::false_type = std::false_type]'
  640 |       vector(vector&& __rv, const allocator_type& __m, false_type)
      |       ^~~~~~
/usr/include/c++/14/bits/stl_vector.h:640:7: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/14/bits/stl_vector.h:635:7: note: candidate: 'std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>&&, const allocator_type&, std::true_type) [with _Tp = int; _Alloc = std::allocator<int>; allocator_type = std::allocator<int>; std::true_type = std::true_type]'
  635 |       vector(vector&& __rv, const allocator_type& __m, true_type) noexcept
      |       ^~~~~~
/usr/include/c++/14/bits/stl_vector.h:635:7: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/14/bits/stl_vector.h:624:7: note: candidate: 'std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&, std::__type_identity_t<_Alloc>&) [with _Tp = int; _Alloc = std::allocator<int>; std::__type_identity_t<_Alloc> = std::allocator<int>]'
  624 |       vector(const vector& __x, const __type_identity_t<allocator_type>& __a)
      |       ^~~~~~
/usr/include/c++/14/bits/stl_vector.h:624:7: note:   conversion of argument 1 would be ill-formed:
/usr/include/c++/14/bits/stl_vector.h:620:7: note: candidate: 'std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>&&) [with _Tp = int; _Alloc = std::allocator<int>]'
  620 |       vector(vector&&) noexcept = default;
      |       ^~~~~~
/usr/include/c++/14/bits/stl_vector.h:620:7: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_vector.h:601:7: note: candidate: 'std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&) [with _Tp = int; _Alloc = std::allocator<int>]'
  601 |       vector(const vector& __x)
      |       ^~~~~~
/usr/include/c++/14/bits/stl_vector.h:601:7: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_vector.h:569:7: note: candidate: 'std::vector<_Tp, _Alloc>::vector(size_type, const value_type&, const allocator_type&) [with _Tp = int; _Alloc = std::allocator<int>; size_type = long unsigned int; value_type = int; allocator_type = std::allocator<int>]'
  569 |       vector(size_type __n, const value_type& __value,
      |       ^~~~~~
/usr/include/c++/14/bits/stl_vector.h:569:7: note:   conversion of argument 1 would be ill-formed:
/usr/include/c++/14/bits/stl_vector.h:556:7: note: candidate: 'std::vector<_Tp, _Alloc>::vector(size_type, const allocator_type&) [with _Tp = int; _Alloc = std::allocator<int>; size_type = long unsigned int; allocator_type = std::allocator<int>]'
  556 |       vector(size_type __n, const allocator_type& __a = allocator_type())
      |       ^~~~~~
/usr/include/c++/14/bits/stl_vector.h:556:7: note:   conversion of argument 1 would be ill-formed:
/usr/include/c++/14/bits/stl_vector.h:542:7: note: candidate: 'std::vector<_Tp, _Alloc>::vector(const allocator_type&) [with _Tp = int; _Alloc = std::allocator<int>; allocator_type = std::allocator<int>]'
  542 |       vector(const allocator_type& __a) _GLIBCXX_NOEXCEPT
      |       ^~~~~~
/usr/include/c++/14/bits/stl_vector.h:542:7: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_vector.h:531:7: note: candidate: 'std::vector<_Tp, _Alloc>::vector() [with _Tp = int; _Alloc = std::allocator<int>]'
  531 |       vector() = default;
      |       ^~~~~~
/usr/include/c++/14/bits/stl_vector.h:531:7: note:   candidate expects 0 arguments, 2 provided