Problem p03682 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1000000007
using namespace std;

bool comp(pair<long, long> a, pair<long, long> b) {
  if (a.second != b.second) {
    return a.second < b.second;
  } else {
    return a.first < b.first;
  }
}

bool xcomp(pair<int, pair<long, long>> a, pair<int, pair<long, long>> b) {
  if (a.second.first != b.second.first) {
    return a.second.first < b.second.first;
  } else {
    return a.second.second < b.second.second;
  }
}

bool ycomp(pair<int, pair<long, long>> a, pair<int, pair<long, long>> b) {
  if (a.second.second != b.second.second) {
    return a.second.second < b.second.second;
  } else {
    return a.second.first < b.second.first;
  }
}

int main() {
  int n;
  cin >> n;
  vector<pair<int, pair<long, long>>> xy(n), origin(n);
  rep(i, n) {
    cin >> xy[i].second.first >> xy[i].second.second;
    xy[i].first = i;
  }
  origin = xy;
  vector<int> root(n);
  rep(i, n) root[i] = i;
  vector<pair<long, pair<long, long>>> dist(2 * n - 2);
  sort(xy.begin(), xy.end(), xcomp);
  rep(i, n - 1) {
    long dis = xy[i + 1].second.first - xy[i].second.first;
    dist[i].first = dis;
    dist[i].second.first = xy[i].first;
    dist[i].second.second = xy[i + 1].first;
  }
  sort(xy.begin(), xy.end(), ycomp);
  rep(i, n - 1) {
    long dis = xy[i + 1].second.second - xy[i].second.second;
    dist[n - 1 + i].first = dis;
    dist[n - 1 + i].second.first = xy[i].first;
    dist[n - 1 + i].second.second = xy[i + 1].first;
  }
  sort(dist.begin(), dist.end());
  long ans = 0, cnt = 0;
  rep(i, 2 * n - 2) {
    // long x = origin[dist[i].second].second.first;
    // long y = origin[dist[i].second].second.second;
    long x = dist[i].second.first;
    long y = dist[i].second.second;
    long tmpx = 0, tmpy = 0;
    while (root[x] != x) {
      x = root[x];
      tmpx++;
    }
    while (root[y] != y) {
      y = root[y];
      tmpy++;
    }
    if (x != y) {
      if (tmpx > tmpy) {
        root[x] = y;
      } else {
        root[y] = x;
      }
      ans += dist[i].first;
      cnt++;
    }
    if (cnt == n)
      break;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1000000007
using namespace std;

bool comp(pair<long, long> a, pair<long, long> b) {
  if (a.second != b.second) {
    return a.second < b.second;
  } else {
    return a.first < b.first;
  }
}

bool xcomp(pair<int, pair<long, long>> a, pair<int, pair<long, long>> b) {
  if (a.second.first != b.second.first) {
    return a.second.first < b.second.first;
  } else {
    return a.second.second < b.second.second;
  }
}

bool ycomp(pair<int, pair<long, long>> a, pair<int, pair<long, long>> b) {
  if (a.second.second != b.second.second) {
    return a.second.second < b.second.second;
  } else {
    return a.second.first < b.second.first;
  }
}

int main() {
  int n;
  cin >> n;
  vector<pair<int, pair<long, long>>> xy(n), origin(n);
  rep(i, n) {
    cin >> xy[i].second.first >> xy[i].second.second;
    xy[i].first = i;
  }
  origin = xy;
  vector<int> root(n);
  rep(i, n) root[i] = i;
  vector<pair<long, pair<long, long>>> dist(2 * n - 2);
  sort(xy.begin(), xy.end(), xcomp);
  rep(i, n - 1) {
    long dis = xy[i + 1].second.first - xy[i].second.first;
    dist[i].first = dis;
    dist[i].second.first = xy[i].first;
    dist[i].second.second = xy[i + 1].first;
  }
  sort(xy.begin(), xy.end(), ycomp);
  rep(i, n - 1) {
    long dis = xy[i + 1].second.second - xy[i].second.second;
    dist[n - 1 + i].first = dis;
    dist[n - 1 + i].second.first = xy[i].first;
    dist[n - 1 + i].second.second = xy[i + 1].first;
  }
  sort(dist.begin(), dist.end());
  long ans = 0, cnt = 0;
  rep(i, 2 * n - 2) {
    // long x = origin[dist[i].second].second.first;
    // long y = origin[dist[i].second].second.second;
    long x = dist[i].second.first;
    long y = dist[i].second.second;
    long tmpx = 0, tmpy = 0;
    while (root[x] != x) {
      x = root[x];
      tmpx++;
    }
    while (root[y] != y) {
      y = root[y];
      tmpy++;
    }
    if (x != y) {
      if (tmpx < tmpy) {
        root[x] = y;
      } else {
        root[y] = x;
      }
      ans += dist[i].first;
      cnt++;
    }
    if (cnt == n)
      break;
  }
  cout << ans << endl;
  return 0;
}