Problem p03682 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
using lli = long long int;
template <class T, class U> void init_n(vector<T> &v, size_t n, U x) {
  v = vector<T>(n, x);
}
template <class T> void init_n(vector<T> &v, size_t n) { init_n(v, n, T()); }
template <class T> void read_n(vector<T> &v, size_t n, size_t o = 0) {
  v = vector<T>(n + o);
  for (lli i = o; i < n + o; ++i)
    cin >> v[i];
}
template <class T> void read_n(T a[], size_t n, size_t o = 0) {
  for (lli i = o; i < n + o; ++i)
    cin >> a[i];
}
template <class T> T gabs(const T &x) { return max(x, -x); }
#define abs gabs

struct UnionFind {
  vector<lli> v;

  UnionFind(lli n = 0) : v(n) { iota(begin(v), end(v), 0); }
  lli root(lli i) { return i == v[i] ? i : root(v[i]); }
  bool same(lli i, lli j) { return root(i) == root(j); }
  void unite(lli i, lli j) { v[root(i)] = root(j); }
};

struct Edge {
  lli u, v, c;
};

lli n;
lli px[100001], py[100001], pi[100001];
vector<Edge> e;
UnionFind uf;

int main() {
  cin >> n;
  for (lli i = 0; i < n; ++i) {
    cin >> px[i] >> py[i];
    pi[i] = i;
  }

  uf = UnionFind(n);

  for (lli k = 0; k <= 1; ++k) {
    const auto &pz = k == 0 ? px : py;
    sort(&pi[0], &pi[n], [&](lli i, lli j) { return pz[i] < pz[j]; });
    for (lli i = 0; i < n - 1; ++i) {
      lli u = pi[i], v = pi[i + 1];
      e.push_back({u, v, pz[v] - pz[u]});
    }
  }

  sort(begin(e), end(e), [](Edge &f, Edge &g) { return f.c < g.c; });

  lli ans = 0;
  for (lli i = 0; i < e.size(); ++i) {
    Edge &f = e[i];
    lli u = f.u, v = f.v, c = f.c;

    if (!uf.same(u, v)) {
      uf.unite(u, v);
      ans += c;
    }
  }

  cout << ans << '\n';
  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
using lli = long long int;
template <class T, class U> void init_n(vector<T> &v, size_t n, U x) {
  v = vector<T>(n, x);
}
template <class T> void init_n(vector<T> &v, size_t n) { init_n(v, n, T()); }
template <class T> void read_n(vector<T> &v, size_t n, size_t o = 0) {
  v = vector<T>(n + o);
  for (lli i = o; i < n + o; ++i)
    cin >> v[i];
}
template <class T> void read_n(T a[], size_t n, size_t o = 0) {
  for (lli i = o; i < n + o; ++i)
    cin >> a[i];
}
template <class T> T gabs(const T &x) { return max(x, -x); }
#define abs gabs

struct UnionFind {
  vector<lli> v;

  UnionFind(lli n = 0) : v(n) { iota(begin(v), end(v), 0); }
  lli root(lli i) { return i == v[i] ? i : v[i] = root(v[i]); }
  bool same(lli i, lli j) { return root(i) == root(j); }
  void unite(lli i, lli j) { v[root(i)] = root(j); }
};

struct Edge {
  lli u, v, c;
};

lli n;
lli px[100001], py[100001], pi[100001];
vector<Edge> e;
UnionFind uf;

int main() {
  cin >> n;
  for (lli i = 0; i < n; ++i) {
    cin >> px[i] >> py[i];
    pi[i] = i;
  }

  uf = UnionFind(n);

  for (lli k = 0; k <= 1; ++k) {
    const auto &pz = k == 0 ? px : py;
    sort(&pi[0], &pi[n], [&](lli i, lli j) { return pz[i] < pz[j]; });
    for (lli i = 0; i < n - 1; ++i) {
      lli u = pi[i], v = pi[i + 1];
      e.push_back({u, v, pz[v] - pz[u]});
    }
  }

  sort(begin(e), end(e), [](Edge &f, Edge &g) { return f.c < g.c; });

  lli ans = 0;
  for (lli i = 0; i < e.size(); ++i) {
    Edge &f = e[i];
    lli u = f.u, v = f.v, c = f.c;

    if (!uf.same(u, v)) {
      uf.unite(u, v);
      ans += c;
    }
  }

  cout << ans << '\n';
  return 0;
}