Language: C++

#ifdef BUILT_LOCAL

#include "niklib.h"

#endif

#include <iostream>
#include <utility>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <string>
#include <cstdio>
#include <map>
#include <set>
#include <list>

using namespace std;

typedef struct {
    int x, y;
} pos_t;

typedef struct {
    pos_t *a, *b;
} path_t;

path_t make_path(pos_t *a, pos_t *b) {
    path_t t;
    t.a = a;
    t.b = b;
    return t;
}


int main() {
    int n = 0;
    scanf("%d", &n);
    pos_t *p = (pos_t *) malloc(sizeof(pos_t) * n);
    pos_t **px = (pos_t **) malloc(sizeof(pos_t *) * n);
    pos_t **py = (pos_t **) malloc(sizeof(pos_t *) * n);
    for (int i = 0; i < n; i++) {
        scanf("%d %d", &p[i].x, &p[i].y);
        px[i] = py[i] = &p[i];
    }
    auto cpx = [](pos_t *a, pos_t *b) { return a->x == b->x ? a < b : a->x < b->x; };
    auto cpy = [](pos_t *a, pos_t *b) { return a->y == b->y ? a < b : a->y < b->y; };

    sort(px, px + n, cpx);
    sort(py, py + n, cpy);

    auto dist = [](path_t &a) { return min(abs(a.a->x - a.b->x), abs(a.a->y - a.b->y)); };
    auto comp = [&dist](path_t &a, path_t &b) { return dist(a) > dist(b); };

    priority_queue<path_t, deque<path_t>, typeof(comp)> q(comp);
    set<pos_t *> t;
    int r = 0;

    t.insert(&p[0]);
    
    auto pushnext = [&this](pos_t *pp) {
        auto rx = lower_bound(px, px + n, pp, cpx);
        auto ry = lower_bound(py, py + n, pp, cpy);
        if (rx != px) q.push(make_path(*rx, *(rx - 1)));
        if (rx + 1 != px + n) q.push(make_path(*rx, *(rx + 1)));
        if (ry != py) q.push(make_path(*ry, *(ry - 1)));
        if (ry + 1 != py + n) q.push(make_path(*ry, *(ry + 1)));
    };

    pushnext(&p[0]);

    while (t.size() != n) {
        auto pp = q.top();
        q.pop();
        if (t.find(pp.b) == t.end()) {
            r += dist(pp);
            t.insert(pp.b);
            pushnext(pp.b);
        }
    }
    printf("%d\n", r);
    return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:62:22: error: 'this' cannot be captured by reference
   62 |     auto pushnext = [&this](pos_t *pp) {
      |                      ^
a.cc: In lambda function:
a.cc:63:31: error: 'px' is not captured
   63 |         auto rx = lower_bound(px, px + n, pp, cpx);
      |                               ^~
a.cc:62:27: note: the lambda has no capture-default
   62 |     auto pushnext = [&this](pos_t *pp) {
      |                           ^
a.cc:41:13: note: 'pos_t** px' declared here
   41 |     pos_t **px = (pos_t **) malloc(sizeof(pos_t *) * n);
      |             ^~
a.cc:63:35: error: 'px' is not captured
   63 |         auto rx = lower_bound(px, px + n, pp, cpx);
      |                                   ^~
a.cc:62:27: note: the lambda has no capture-default
   62 |     auto pushnext = [&this](pos_t *pp) {
      |                           ^
a.cc:41:13: note: 'pos_t** px' declared here
   41 |     pos_t **px = (pos_t **) malloc(sizeof(pos_t *) * n);
      |             ^~
a.cc:63:40: error: 'n' is not captured
   63 |         auto rx = lower_bound(px, px + n, pp, cpx);
      |                                        ^
a.cc:62:27: note: the lambda has no capture-default
   62 |     auto pushnext = [&this](pos_t *pp) {
      |                           ^
a.cc:38:9: note: 'int n' declared here
   38 |     int n = 0;
      |         ^
a.cc:63:47: error: 'cpx' is not captured
   63 |         auto rx = lower_bound(px, px + n, pp, cpx);
      |                                               ^~~
a.cc:62:27: note: the lambda has no capture-default
   62 |     auto pushnext = [&this](pos_t *pp) {
      |                           ^
a.cc:47:10: note: 'main()::<lambda(pos_t*, pos_t*)> cpx' declared here
   47 |     auto cpx = [](pos_t *a, pos_t *b) { return a->x == b->x ? a < b : a->x < b->x; };
      |          ^~~
a.cc:64:31: error: 'py' is not captured
   64 |         auto ry = lower_bound(py, py + n, pp, cpy);
      |                               ^~
a.cc:62:27: note: the lambda has no capture-default
   62 |     auto pushnext = [&this](pos_t *pp) {
      |                           ^
a.cc:42:13: note: 'pos_t** py' declared here
   42 |     pos_t **py = (pos_t **) malloc(sizeof(pos_t *) * n);
      |             ^~
a.cc:64:35: error: 'py' is not captured
   64 |         auto ry = lower_bound(py, py + n, pp, cpy);
      |                                   ^~
a.cc:62:27: note: the lambda has no capture-default
   62 |     auto pushnext = [&this](pos_t *pp) {
      |                           ^
a.cc:42:13: note: 'pos_t** py' declared here
   42 |     pos_t **py = (pos_t **) malloc(sizeof(pos_t *) * n);
      |             ^~
a.cc:64:40: error: 'n' is not captured
   64 |         auto ry = lower_bound(py, py + n, pp, cpy);
      |                                        ^
a.cc:62:27: note: the lambda has no capture-default
   62 |     auto pushnext = [&this](pos_t *pp) {
      |                           ^
a.cc:38:9: note: 'int n' declared here
   38 |     int n = 0;
      |         ^
a.cc:64:47: error: 'cpy' is not captured
   64 |         auto ry = lower_bound(py, py + n, pp, cpy);
      |                                               ^~~
a.cc:62:27: note: the lambda has no capture-default
   62 |     auto pushnext = [&this](pos_t *pp) {
      |                           ^
a.cc:48:10: note: 'main()::<lambda(pos_t*, pos_t*)> cpy' declared here
   48 |     auto cpy = [](pos_t *a, pos_t *b) { return a->y == b->y ? a < b : a->y < b->y; };
      |          ^~~
a.cc:65:19: error: 'px' is not captured
   65 |         if (rx != px) q.push(make_path(*rx, *(rx - 1)));
      |                   ^~
a.cc:62:27: note: the lambda has no capture-default
   62 |     auto pushnext = [&this](pos_t *pp) {
      |                           ^
a.cc:41:13: note: 'pos_t** px' declared here
   41 |     pos_t **px = (pos_t **) malloc(sizeof(pos_t *) * n);
      |             ^~
a.cc:65:23: error: 'q' is not captured
   65 |         if (rx != px) q.push(make_path(*rx, *(rx - 1)));
      |                       ^
a.cc:62:27: note: the lambda has no capture-default
   62 |     auto pushnext = [&this](pos_t *pp) {
      |                           ^
a.cc:56:57: note: 'std::priority_queue<path_t, std::deque<path_t>, main()::<lambda(path_t&, path_t&)> > q' declared here
   56 |     priority_queue<path_t, deque<path_t>, typeof(comp)> q(comp);
      |                                                         ^
a.cc:66:23: error: 'px' is not captured
   66 |         if (rx + 1 != px + n) q.push(make_path(*rx, *(rx + 1)));
      |                       ^~
a.cc:62:27: note: the lambda has no capture-default
   62 |     auto pushnext = [&this](pos_t *pp) {
      |                           ^
a.cc:41:13: note: 'pos_t** px' declared here
   41 |     pos_t **px = (pos_t **) malloc(sizeof(pos_t *) * n);
      |             ^~
a.cc:66:28: error: 'n' is not captured
   66 |         if (rx + 1 != px + n) q.push(make_path(*rx, *(rx + 1)));
      |                            ^
a.cc:62:27: note: the lambda has no capture-default
   62 |     auto pushnext = [&this](pos_t *pp) {
      |                           ^
a.cc:38:9: note: 'int n' declared here
   38 |     int n = 0;
      |         ^
a.cc:66:31: error: 'q' is not captured
   66 |         if (rx + 1 != px + n) q.push(make_path(*rx, *(rx + 1)));
      |                               ^
a.cc:62:27: note: the lambda has no capture-default
   62 |     auto pushnext = [&this](pos_t *pp) {
      |                           ^
a.cc:56:57: note: 'std::priority_queue<path_t, std::deque<path_t>, main()::<lambda(path_t&, path_t&)> > q' declared here
   56 |     priority_queue<path_t, deque<path_t>, typeof(comp)> q(comp);
      |                                                         ^
a.cc:67:19: error: 'py' is not captured
   67 |         if (ry != py) q.push(make_path(*ry, *(ry - 1)));
      |                   ^~
a.cc:62:27: note: the lambda has no capture-default
   62 |     auto pushnext = [&this](pos_t *pp) {
      |                           ^
a.cc:42:13: note: 'pos_t** py' declared here
   42 |     pos_t **py = (pos_t **) malloc(sizeof(pos_t *) * n);
      |             ^~
a.cc:67:23: error: 'q' is not captured
   67 |         if (ry != py) q.push(make_path(*ry, *(ry - 1)));
      |                       ^
a.cc:62:27: note: the lambda has no capture-default
   62 |     auto pushnext = [&this](pos_t *pp) {
      |                           ^
a.cc:56:57: note: 'std::priority_queue<path_t, std::deque<path_t>, main()::<lambda(path_t&, path_t&)> > q' declared here
   56 |     priority_queue<path_t, deque<path_t>, typeof(comp)> q(comp);
      |                                                         ^
a.cc:68:23: error: 'py' is not captured
   68 |         if (ry + 1 != py + n) q.push(make_path(*ry, *(ry + 1)));
      |                       ^~
a.cc:62:27: note: the lambda has no capture-default
   62 |     auto pushnext = [&this](pos_t *pp) {
      |                           ^
a.cc:42:13: note: 'pos_t** py' declared here
   42 |     pos_t **py = (pos_t **) malloc(sizeof(pos_t *) * n);
      |             ^~
a.cc:68:28: error: 'n' is not captured
   68 |         if (ry + 1 != py + n) q.push(make_path(*ry, *(ry + 1)));
      |                            ^
a.cc:62:27: note: the lambda has no capture-default
   62 |     auto pushnext = [&this](pos_t *pp) {
      |                           ^
a.cc:38:9: note: 'int n' declared here
   38 |     int n = 0;
      |         ^
a.cc:68:31: error: 'q' is not captured
   68 |         if (ry + 1 != py + n) q.push(make_path(*ry, *(ry + 1)));
      |                               ^
a.cc:62:27: note: the lambda has no capture-default
   62 |     auto pushnext = [&this](pos_t *pp) {
      |                           ^
a.cc:56:57: note: 'std::priority_queue<path_t, std::deque<path_t>, main()::<lambda(path_t&, path_t&)> > q' declared here
   56 |     priority_queue<path_t, deque<path_t>, typeof(comp)> q(comp);
      |                                                         ^