Problem p03682 (C++)

Buggy submission (status: Time Limit Exceeded):
// Anve$hi $hukla
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 100005;

struct edge {
  int from, to, w;
};

bool cmp(edge a, edge b) { return a.w < b.w; }

int n, m;
vector<edge> e;
int st[MAXN];
long long ans;

int getSet(int x) {
  if (st[x] == x)
    return x;
  st[x] = getSet(st[x]);
  return st[x];
}

void unite(int a, int b) {
  a = getSet(a);
  b = getSet(b);
  if (rand() & 1)
    swap(a, b);
  st[a] = b;
}

int getDist(pair<int, int> p1, pair<int, int> p2) {
  return min(abs(p1.first - p2.first), abs(p1.second - p2.second));
}

int main() {
  // assert(freopen("input.txt","r",stdin));
  // assert(freopen("output.txt","w",stdout));

  scanf("%d", &n);
  vector<pair<pair<int, int>, int>> v1(n);
  vector<pair<pair<int, int>, int>> v2(n);

  for (int i = 0; i < n; i++) {
    scanf("%d %d", &v1[i].first.first, &v1[i].first.second);
    v1[i].second = i + 1;
    v2[i] = v1[i];
    swap(v2[i].first.first, v2[i].first.second);
    // cout << v1[i].first.first << " " << v1[i].first.second << " " <<
    // v1[i].second << endl; cout << v2[i].first.first << " " <<
    // v2[i].first.second << " " << v2[i].second << endl;
  }

  sort(v1.begin(), v1.end());
  sort(v2.begin(), v2.end());

  for (int i = 0; i < n; i++) {
    for (int j = max(i - 64, 0); j < min(i + 64, n); j++) {
      if (i != j) {
        edge add;
        add.from = v1[i].second;
        add.to = v1[j].second;
        add.w = getDist(v1[i].first, v1[j].first);
        e.push_back(add);

        add.from = v2[i].second;
        add.to = v2[j].second;
        add.w = getDist(v2[i].first, v2[j].first);
        e.push_back(add);
      }
    }
  }

  for (int i = 1; i <= n; i++)
    st[i] = i;

  sort(e.begin(), e.end(), cmp);

  for (int i = 0; i < (int)e.size(); i++) {
    int from = getSet(e[i].from), to = getSet(e[i].to);
    if (from != to) {
      ans += (long long)e[i].w;
      unite(from, to);
    }
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
// Anve$hi $hukla
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 100005;

struct edge {
  int from, to, w;
};

bool cmp(edge a, edge b) { return a.w < b.w; }

int n, m;
vector<edge> e;
int st[MAXN];
long long ans;

int getSet(int x) {
  if (st[x] == x)
    return x;
  st[x] = getSet(st[x]);
  return st[x];
}

void unite(int a, int b) {
  a = getSet(a);
  b = getSet(b);
  if (rand() & 1)
    swap(a, b);
  st[a] = b;
}

int getDist(pair<int, int> p1, pair<int, int> p2) {
  return min(abs(p1.first - p2.first), abs(p1.second - p2.second));
}

int main() {
  // assert(freopen("input.txt","r",stdin));
  // assert(freopen("output.txt","w",stdout));

  scanf("%d", &n);
  vector<pair<pair<int, int>, int>> v1(n);
  vector<pair<pair<int, int>, int>> v2(n);

  for (int i = 0; i < n; i++) {
    scanf("%d %d", &v1[i].first.first, &v1[i].first.second);
    v1[i].second = i + 1;
    v2[i] = v1[i];
    swap(v2[i].first.first, v2[i].first.second);
    // cout << v1[i].first.first << " " << v1[i].first.second << " " <<
    // v1[i].second << endl; cout << v2[i].first.first << " " <<
    // v2[i].first.second << " " << v2[i].second << endl;
  }

  sort(v1.begin(), v1.end());
  sort(v2.begin(), v2.end());

  for (int i = 0; i < n; i++) {
    for (int j = max(i - 8, 0); j < min(i + 8, n); j++) {
      if (i != j) {
        edge add;
        add.from = v1[i].second;
        add.to = v1[j].second;
        add.w = getDist(v1[i].first, v1[j].first);
        e.push_back(add);

        add.from = v2[i].second;
        add.to = v2[j].second;
        add.w = getDist(v2[i].first, v2[j].first);
        e.push_back(add);
      }
    }
  }

  for (int i = 1; i <= n; i++)
    st[i] = i;

  sort(e.begin(), e.end(), cmp);

  for (int i = 0; i < (int)e.size(); i++) {
    int from = getSet(e[i].from), to = getSet(e[i].to);
    if (from != to) {
      ans += (long long)e[i].w;
      unite(from, to);
    }
  }

  cout << ans << endl;

  return 0;
}