Language: C++

#include <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <cctype>
#include <cstring>
#include <algorithm>
#include <numeric>
#include <functional>
#include <string>
#include <vector>
#include <deque>
#include <stack>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <utility>
#include <bitset>
using namespace std;

// #define DEBUG 1

#ifdef DEBUG
#define NDEBUG
#include "cout11.h"
#endif
#undef NDEBUG
#include <cassert>

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> ii;
typedef pair<ll,ll> llll;
typedef pair<double,double> dd;

#define sz(a)  int((a).size())
#define pb  push_back
#define FOR(var,from,to) for(int var=(from);var<=(to);++var)
#define rep(var,n)  for(int var=0;var<(n);++var)
#define rep1(var,n)  for(int var=1;var<=(n);++var)
#define repC2(vari,varj,n)  for(int vari=0;vari<(n)-1;++vari)for(int varj=vari+1;varj<(n);++varj)
#define ALL(c)  (c).begin(),(c).end()
#define tr(i,c)  for(auto i=(c).begin(); i!=(c).end(); ++i)
#define found(s,e)  ((s).find(e)!=(s).end())
#define mset(arr,val)  memset(arr,val,sizeof(arr))
#define mid(x,y) ((x)+((y)-(x))/2)
#define foreqr(eqr,mm,v) for (auto eqr=mm.equal_range(v);eqr.first!=eqr.second;++eqr.first)
#define lower_index(s,v)  (int)(lower_bound(s.begin(),s.end(),v)-s.begin())
#define upper_index(s,v)  (int)(upper_bound(s.begin(),s.end(),v)-s.begin())


#define INTSPACE 11
char _buf[INTSPACE*1000000 + 3];  // ここ要確認

int loadint() {
    fgets(_buf, INTSPACE+3, stdin);
    return atoi(_buf);
}
int loadvec(vector<int>& v, int N) {
    int bufsize = INTSPACE*N + 3;
    fgets(_buf, bufsize, stdin);
    v.resize(N);

    int i=0;
    bool last = false;
    for (char *p=&_buf[0]; ;) {
        char *q = p;
        while (*q > ' ') ++q;
        if (*q == 0x0D || *q == 0x0A) last = true;
        *q = 0;
        v[i++] = atoi(p);
        if (last || i == N) break;
        p = q+1;
    }
    // assert(i <= N);
    return i;
}

class UnionFind {
  vector<int> data;
 public:
  UnionFind(int size) : data(size, -1) { }
  bool unionSet(int x, int y) {
    x = root(x); y = root(y);
    if (x != y) {
      if (data[y] < data[x]) swap(x, y);
      data[x] += data[y]; data[y] = x;
    }
    return x != y;
  }
  bool findSet(int x, int y) { return root(x) == root(y); }
  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  int size(int x) { return -data[root(x)]; }
};

//auto it##eqr=mm.equal_range(v);for(auto it=it##eqr.first,end=it##eqr.second;it!=end;++it)


ll solve(int n, vector<int>&x, vector<int>&y) {
//    UnionFind uf(n);
    assert(x.size() == n && y.size() == n);

    vector<bool> vis(n, false);
    multimap<int, int> xx,xxv,xxa, yy,yyv,yya;
    multimap<int, ii> ox;
    multimap<int, ii> oy;

    vis[0] = true;
    ll cost = 0;

    auto initialize = [](int n, vector<bool>& vis,
                vector<int>& x,
                multimap<int,int>& xx,
                multimap<int,int>& xxv, multimap<int,int>& xxa,
                multimap<int,ii>& ox, const char *x_y) {
#ifdef VERBOSE
        printf("<%s> initializing...\n", x_y);
#endif
        rep(i, n) {
            xx.insert(ii(x[i], i));
            if (vis[i]) {
                xxv.insert(ii(x[i], i));
            } else {
                xxa.insert(ii(x[i], i));
            }
        }

        int i_at_lastV = -1, xi_at_lastV = -1;
        int i_at_lastA = -1, xi_at_lastA = -1;
#ifdef VERBOSE
        cout << "xx:" << xx << endl;
#endif
        for (auto xxj : xx) {
            int xi = xxj.first, i = xxj.second;
            if (vis[i]) { // V
                if (i_at_lastA != -1) {
                    assert(i != i_at_lastA);
                    int d = abs(xi - xi_at_lastA);
                    ox.insert( pair<int, ii>(d, ii(i, i_at_lastA)) );
                }
                i_at_lastV = i;
                xi_at_lastV = xi;
            } else { // A
                if (i_at_lastV != -1) {
                    assert(i != i_at_lastV);
                    int d = abs(xi_at_lastV - xi);
                    ox.insert( pair<int, ii>(d, ii(i_at_lastV, i)) );
                }
                i_at_lastA = i;
                xi_at_lastA = xi;
            }
        }
    };
    initialize(n,vis,x,xx,xxv,xxa,ox, "x");
    initialize(n,vis,y,yy,yyv,yya,oy, "y");

    for (int z=1; z<n; ++z) {
        auto take_the_smallest = [](vector<bool>& vis, multimap<int,ii>& ox, ll& cost, int& v, int& a) {
            auto ox0 = ox.begin();
            if (ox0 == ox.end()) {
                v = a = -1; return;
            }
            while (vis[ox0->second.second]) ++ox0;

            cost += ox0->first;
            v = ox0->second.first; a = ox0->second.second;
        };

        auto fn0 = [](vector<bool>& vis, multimap<int,ii>& ox) {
            for (auto ax=ox.begin(); ax!=ox.end(); ++ax) {
                if (!vis[ax->second.second]) {
                    return ax->first;
                }
            }
            return INT_MAX;
        };
        int x_nearest = fn0(vis, ox);
        int y_nearest = fn0(vis, oy);
        if (x_nearest == INT_MAX && y_nearest == INT_MAX) {
            assert(false);
        }

        int v, a;
        if (x_nearest < y_nearest) {
            take_the_smallest(vis,ox, cost,v,a);
        } else {
            take_the_smallest(vis,oy, cost,v,a);
        }

        vis[a] = true;
        {
            auto move_a_from_xxa_to_xxv = [](vector<int>& x, multimap<int,int>& xxa, multimap<int,int>& xxv, int a, const char *x_y) {
//                foreqr(eqr,xxa,x[a]) {
//                    if (eqr.first->second == a) { xxa.erase(eqr.first++); eqr.first--; break; }
//                }
                xxv.insert(ii(x[a], a));
            };
            move_a_from_xxa_to_xxv(x,xxa,xxv,a, "x");
            move_a_from_xxa_to_xxv(y,yya,yyv,a, "y");

            auto update_ox = [](vector<bool>& vis, vector<int>& x, multimap<int,int>& xxa, multimap<int,ii>& ox, int a, const char *x_y) {
                auto ax = xxa.lower_bound(x[a]), bx = ax;
                while (ax != xxa.end()) {
                    int i = ax->second;
                    if (i != a && !vis[i]) {
                        ox.insert( pair<int, ii>(abs(x[a] - x[i]), ii(a, i)) );
                        break;
                    }
                    if (ax == xxa.begin()) break;
                    --ax;
                }
                while (bx != xxa.end()) {
                    int i = bx->second;
                    if (i != a && !vis[i]) {
                        ox.insert( pair<int, ii>(abs(x[a] - x[i]), ii(a, i)) );
                        break;
                    }
                    ++bx;
                }
            };
            update_ox(vis,x,xxa,ox,a, "x");
            update_ox(vis,y,yya,oy,a, "y");
        }
    }
    return cost;
}

int main(){
    int N = loadint();
    vector<int> tmp(2);

    vector<int> x(N), y(N);
    rep(i, N) {
        loadvec(tmp, 2);
        x[i]=tmp[0]; y[i]=tmp[1];
    }
    printf("%lld\n", solve(N, x, y));

    return 0;
}


Compiler output:
a.cc: In lambda function:
a.cc:176:20: error: 'INT_MAX' was not declared in this scope
  176 |             return INT_MAX;
      |                    ^~~~~~~
a.cc:30:1: note: 'INT_MAX' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'
   29 | #include <cassert>
  +++ |+#include <climits>
   30 | 
a.cc: In function 'll solve(int, std::vector<int>&, std::vector<int>&)':
a.cc:180:26: error: 'INT_MAX' was not declared in this scope
  180 |         if (x_nearest == INT_MAX && y_nearest == INT_MAX) {
      |                          ^~~~~~~
a.cc:180:26: note: 'INT_MAX' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'