Problem p02303 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
// typedef complex<double> P;
typedef pair<double, double> P;
#define X first
#define Y second
#define curr(P, i) P[i]
#define next(P, i) P[(i + 1) % P.size()]
#define prev(P, i) P[(i + P.size() - 1) % P.size()]
#define diff(P, i) (next(P, i) - curr(P, i))
const double EPS = 1e-8;
const double INF = 1e12;
// namespace std {
//   bool operator < (const P& a, const P& b) {
//     return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);
//   }
// }

typedef vector<P> G;

double compare_x(const P &a, const P &b) {
  return a.X != b.X ? a.X < b.X : a.Y < b.Y;
}

double compare_y(const P &a, const P &b) {
  return a.Y != b.Y ? a.Y < b.Y : a.X < b.X;
}

double length_of_the_nearest_point_to(pair<double, double> a[100000], int n) {
  if (n <= 1)
    return 999999999.9;
  int m = n / 2;
  double x = a[m].first;
  double d = min(length_of_the_nearest_point_to(a, m),
                 length_of_the_nearest_point_to(a + m, n - m));
  sort(a, a + n, compare_y);
  vector<pair<double, double>> b;
  for (int i = 0; i < n; i++) {
    if (fabs(a[i].first - x) >= d)
      continue;
    for (int j = 0; j < b.size(); j++) {
      double dx = a[i].first - b[b.size() - j - 1].first;
      double dy = a[i].second - b[b.size() - j - 1].second;
      if (dy >= d)
        break;
      d = min(d, sqrt(dx * dx + dy * dy));
    }
    b.push_back(a[i]);
  }
  return d;
}

int main() {
  int N;
  cin >> N;
  G g(N);
  REP(i, N) {
    double x, y;
    cin >> x >> y;
    g[i] = P(x, y);
  }
  sort(g.begin(), g.end(), compare_x);
  printf("%.10lf\\n", length_of_the_nearest_point_to(&g[0], N));
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef complex<double> P;
#define X real()
#define Y imag()
#define curr(P, i) P[i]
#define next(P, i) P[(i + 1) % P.size()]
#define prev(P, i) P[(i + P.size() - 1) % P.size()]
#define diff(P, i) (next(P, i) - curr(P, i))
const double EPS = 1e-8;
const double INF = 1e12;
namespace std {
bool operator<(const P &a, const P &b) {
  return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);
  // return imag(a) != imag(b) ? imag(a) < imag(b) : real(a) < real(b);
}
} // namespace std

typedef vector<P> G;

pair<P, P> closestPair(G p) {
  int n = p.size(), s = 0, t = 1, m = 2, S[n];
  S[0] = 0, S[1] = 1;
  SORT(p);
  // double d = norm(p[s]-p[t]);
  double d = abs(p[s] - p[t]);
  for (int i = 2; i < n; S[m++] = i++)
    REP(j, m) {
      double nd = abs(p[S[j]] - p[i]);
      if (nd < d) {
        d = nd;
        s = S[j];
        t = i;
      }
      if (real(p[S[j]]) < real(p[i]) - d)
        S[j--] = S[--m];
    }
  return make_pair(p[s], p[t]);
}

int main() {
  int N;
  cin >> N;
  G g(N);
  REP(i, N) {
    double x, y;
    cin >> x >> y;
    g[i] = P(x, y);
  }
  SORT(g);
  pair<P, P> ans = closestPair(g);
  printf("%.10lf\\n", abs(ans.second - ans.first));
  return 0;
}