Language: C++

#include  // Rooks, by Errichto
using namespace std;
#define sim template < class c
#define ris return  this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge range(c i, c j) { return rge{i, j}; }
sim > auto dud(c x) -> decltype(cerr << x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge d) {
  this << "[";
  for (auto it = d.b; it != d.e; ++it)
    this << ", " + 2  (it == d.b) << it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #VA_ARGS ": " << (VA_ARGS) << "] "
// debug & operator << (debug & dd, P p) { dd << "(" << p.x << ", " << p.y << ")"; return dd; }

struct Rook {
    int x, y, id;
    bool operator <(const Rook& other) const {
        return x < other.x;
    }
};
long long hack;
int main() {
    int n;
    scanf("%d", &n);
    assert(2 <= n && n <= 200  1000);
    vector rooks(n);
    for(int i = 0; i < n; ++i) {
        rooks[i].id = i;
        scanf("%d%d", &rooks[i].x, &rooks[i].y);
        assert(1 <= min(rooks[i].x, rooks[i].y) && max(rooks[i].x, rooks[i].y) <= 1000  1000);
    }
    sort(rooks.begin(), rooks.end());
    vector 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());
    for(int i = 0; i < n - 1; ++i) {
        assert(ys[i] != ys[i+1]);
        assert(rooks[i].x != rooks[i+1].x);
    }
    vector> 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> 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)); // 0 or 1
                        // debug() << imie(start) imie(i) imie(direction);
                        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();
        debug() << imie(start) imie(intervals);

        { // complexity check
            static int sum_s = 0, sum_len = 0;
            if(s > 1) sum_s += s;
            assert(sum_s <= n);
            sum_len += R - L + 1;
            assert(sum_len <= 3  n); // N + out + 2  border    ('out' and 'border' are disjoint)
            static vector basic(n), out(n), border(n);
            pair i_basic = intervals[0], i_out = intervals[max(0,s-2)], i_border = intervals.back();
            for(int i = i_border.first; i <= i_border.second; ++i) {
                if(i_basic.first <= i && i <= i_basic.second) {
                    assert(++basic[i] == 1);
                }
                else if(i_out.first <= i && i <= i_out.second) {
                    assert(++out[i] == 1 && border[i] == 0);
                }
                else {
                    assert(++border[i] <= 2 && out[i] == 0);
                }
            }
        }

        const long long INF = 1e18L + 5;
        auto min_self = & {
            a = min(a, b);
        };
        auto f = & { // manhattan distance between two rooks
            return abs(rooks[i].x - rooks[j].x) + abs(rooks[i].y - rooks[j].y);
        };
        vector> dp(s, vector(2, INF));
        // dp[i][0] is minimum number of REMAINING operations if we did interval i and we are at its left end
        // dp[i][1] -- same but right end
        dp[s-1][0] = dp[s-1][1] = 0;
        for(int who = s - 1; who >= 1; --who) {
            // [L1, [L2, R1], R2]
            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) {
            assert(answer[rooks[i].id] == make_pair(-1, -1LL));
            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(pair p : answer) {
        // printf("%d %lld\n", p.first, p.second - p.first);
        printf("%lld\n", p.second - p.first);
    }
    cerr << "hack = " << hack << endl;
}

Compiler output:
a.cc:1:32: error: #include expects "FILENAME" or <FILENAME>
    1 | #include  // Rooks, by Errichto
      |                                ^
a.cc:30:17: error: '#' is not followed by a macro parameter
   30 | #define imie(...) " [" << #VA_ARGS ": " << (VA_ARGS) << "] "
      |                 ^
a.cc:9:7: error: deduced class type 'rge' in function return type
    9 | sim > rge range(c i, c j) { return rge{i, j}; }
      |       ^~~
a.cc:8:14: note: 'template<class c> struct rge' declared here
    8 | sim > struct rge { c b, e; };
      |              ^~~
a.cc:10:33: error: 'cerr' was not declared in this scope
   10 | sim > auto dud(c x) -> decltype(cerr << x, 0);
      |                                 ^~~~
a.cc:1:1: note: 'std::cerr' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
  +++ |+#include <iostream>
    1 | #include  // Rooks, by Errichto
a.cc:10:33: error: 'cerr' was not declared in this scope
   10 | sim > auto dud(c x) -> decltype(cerr << x, 0);
      |                                 ^~~~
a.cc:10:33: note: 'std::cerr' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
a.cc: In member function 'debug& debug::operator<<(const c&)':
a.cc:4:21: error: invalid initialization of non-const reference of type 'debug&' from an rvalue of type 'debug*'
    4 | #define ris return  this
      |                     ^~~~
a.cc:27:21: note: in expansion of macro 'ris'
   27 | sim dor(const c&) { ris; }
      |                     ^~~
a.cc: In function 'int main()':
a.cc:42:5: error: 'scanf' was not declared in this scope
   42 |     scanf("%d", &n);
      |     ^~~~~
a.cc:43:30: error: expected ')' before numeric constant
   43 |     assert(2 <= n && n <= 200  1000);
      |           ~                  ^ ~~~~
      |                              )
a.cc:43:5: error: 'assert' was not declared in this scope
   43 |     assert(2 <= n && n <= 200  1000);
      |     ^~~~~~
a.cc:1:1: note: 'assert' is defined in header '<cassert>'; this is probably fixable by adding '#include <cassert>'
  +++ |+#include <cassert>
    1 | #include  // Rooks, by Errichto
a.cc:44:5: error: 'vector' was not declared in this scope
   44 |     vector rooks(n);
      |     ^~~~~~
a.cc:1:1: note: 'std::vector' is defined in header '<vector>'; this is probably fixable by adding '#include <vector>'
  +++ |+#include <vector>
    1 | #include  // Rooks, by Errichto
a.cc:46:9: error: 'rooks' was not declared in this scope
   46 |         rooks[i].id = i;
      |         ^~~~~
a.cc:48:21: error: 'min' was not declared in this scope; did you mean 'main'?
   48 |         assert(1 <= min(rooks[i].x, rooks[i].y) && max(rooks[i].x, rooks[i].y) <= 1000  1000);
      |                     ^~~
      |                     main
a.cc:48:52: error: 'max' was not declared in this scope
   48 |         assert(1 <= min(rooks[i].x, rooks[i].y) && max(rooks[i].x, rooks[i].y) <= 1000  1000);
      |                                                    ^~~
a.cc:50:10: error: 'rooks' was not declared in this scope
   50 |     sort(rooks.begin(), rooks.end());
      |          ^~~~~
a.cc:50:5: error: 'sort' was not declared in this scope; did you mean 'short'?
   50 |     sort(rooks.begin(), rooks.end());
      |     ^~~~
      |     short
a.cc:51:11: error: expected ';' before 'ys'
   51 |     vector ys{INT_MIN, INT_MAX}; // two sentinels for convenience
      |           ^~~
      |           ;
a.cc:53:9: error: 'ys' was not declared in this scope
   53 |         ys.push_back(rooks[i].y);
      |         ^~
a.cc:55:10: error: 'ys' was not declared in this scope
   55 |     sort(ys.begin(), ys.end());
      |          ^~
a.cc:60:13: error: 'answer' was not declared in this scope
   60 |     vector> answer(n, {-1, -1});
      |             ^~~~~~
a.cc:66:23: error: 'lower_bound' was not declared in this scope
   66 |         int start_y = lower_bound(ys.begin(), ys.end(), rooks[start].y) - ys.begin();
      |                       ^~~~~~~~~~~
a.cc:71:17: error: 'intervals' was not declared in this scope
   71 |         vector> intervals;
      |                 ^~~~~~~~~
a.cc:74:38: error: deducing from brace-enclosed initializer list requires '#include <initializer_list>'
  +++ |+#include <initializer_list>
    1 | #include  // Rooks, by Errichto
......
   74 |             for(int i : {L - 1, R + 1}) {
      |                                      ^
a.cc:99:53: error: 'make_pair' was not declared in this scope
   99 |         if(intervals.empty() || intervals.back() != make_pair(L, R)) {
      |                                                     ^~~~~~~~~
a.cc:1:1: note: 'std::make_pair' is defined in header '<utility>'; this is probably fixable by adding '#include <utility>'
  +++ |+#include <utility>
    1 | #include  // Rooks, by Errichto
a.cc:103:20: error: 'imie' was not declared in this scope
  103 |         debug() << imie(start) imie(intervals);
      |                    ^~~~
a.cc:110:32: error: expected ')' before 'n'
  110 |             assert(sum_len <= 3  n); // N + out + 2  border    ('out' and 'border' are disjoint)
      |                   ~            ^ ~
      |                                )
a.cc:111:20: error: 'vector' does not name a type
  111 |             static vector basic(n), out(n), border(n);
      |                    ^~~~~~
a.cc:112:13: error: 'pair' was not declared in this scope
  112 |             pair i_basic = intervals[0], i_out = intervals[max(0,s-2)], i_border = intervals.back();
      |             ^~~~
a.cc:112:13: note: 'std::pair' is defined in header '<utility>'; this is probably fixable by adding '#include <utility>'
a.cc:113:25: error: 'i_border' was not declared in this scope
  113 |             for(int i = i_border.first; i <= i_border.second; ++i) {
      |                         ^~~~~~~~
a.cc:114:20: error: 'i_basic' was not declared in this scope
  114 |                 if(i_basic.first <= i && i <= i_basic.second) {
      |                    ^~~~~~~
a.cc:115:30: error: 'basic' was not declared in this scope
  115 |                     assert(++basic[i] == 1);
      |                              ^~~~~
a.cc:117:25: error: 'i_out' was not declared in this scope
  117 |                 else if(i_out.first <= i && i <= i_out.second) {
      |                         ^~~~~
a.cc:118:30: error: 'out' was not declared in this scope
  118 |                     assert(++out[i] == 1 && border[i] == 0);
      |                              ^~~
a.cc:118:45: error: 'border' was not declared in this scope
  118 |                     assert(++out[i] == 1 && border[i] == 0);
      |                                             ^~~~~~
a.cc:121:30: error: 'border' was not declared in this scope
  121 |                     assert(++border[i] <= 2 && out[i] == 0);
      |                              ^~~~~~
a.cc:121:48: error: 'out' was not declared in this scope
  121 |                     assert(++border[i] <= 2 && out[i] == 0);
      |                                                ^~~
a.cc:127:27: error: expected primary-expression before '{' token
  127 |         auto min_self = & {
      |                           ^
a.cc:130:20: error: expected primary-expression before '{' token
  130 |         auto f = & { // manhattan distance between two rooks
      |                    ^
a.cc:133:17: error: 'dp' was not declared in this scope
  133 |         vector> dp(s, vector(2, INF));
      |                 ^~
a.cc:143:44: error: expression cannot be used as a function
  143 |             hack += (long long) (L2 - L1)  (R2 - R1);
      |                                 ~~~~~~~~~~~^~~~~~~~~
a.cc:150:31: error: 'high' was not declared in this scope
  150 |         for(int i = low; i <= high; ++i) {
      |                               ^~~~
a.cc:151:43: error: 'make_pair' was not declared in this scope
  151 |             assert(answer[rooks[i].id] == make_pair(-1, -1LL));
      |                                           ^~~~~~~~~
a.cc:151:43: note: 'std::make_pair' is defined in header '<utility>'; this is probably fixable by adding '#include <utility>'
a.cc:154:17: error: 'min' was not declared in this scope; did you mean 'main'?
  154 |                 min(dp[0][0] + f(low, high) + f(high, i), dp[0][1] + f(high, low) + f(low, i))
      |                 ^~~
      |                 main
a.cc:158:9: error: 'pair' was not declared in this scope
  158 |     for(pair p : answer) {
      |         ^~~~
a.cc:158:9: note: 'std::pair' is defined in header '<utility>'; this is probably fixable by adding '#include <utility>'
a.cc:162:5: error: 'cerr' was not declared in this scope
  162 |     cerr << "hack = " << hack << endl;
      |     ^~~~
a.cc:162:5: note: 'std::cerr' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
a.cc:162:34: error: 'endl' was not declared in this scope
  162 |     cerr << "hack = " << hack << endl;
      |                                  ^~~~
a.cc:1:1: note: 'std::endl' is defined in header '<ostream>'; this is probably fixable by adding '#include <ostream>'
  +++ |+#include <ostream>
    1 | #include  // Rooks, by Errichto
a.cc:163:1: error: expected primary-expression before '}' token
  163 | }
      | ^
a.cc:162:39: error: expected ')' before '}' token
  162 |     cerr << "hack = " << hack << endl;
      |                                       ^
      |                                       )
  163 | }
      | ~                                      
a.cc:158:8: note: to match this '('
  158 |     for(pair p : answer) {
      |        ^
a.cc:163:1: error: expected primary-expression before '}' token
  163 | }
      | ^