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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <vector>

using namespace std;
typedef pair<double, double> pp;
vector<pp> a;

#define sqr(x) ((x) * (x))
double Plus(double x) { return x < 0 ? -x : x; }
double dist(pp x, pp y) {
  return sqr(x.first - y.first) + sqr(x.second - y.second);
}

double go(int l, int r) {
  if (r - l == 1)
    return dist(a[l], a[r]);
  if (r - l == 2)
    return min(dist(a[l], a[l + 1]),
               min(dist(a[l], a[l + 2]), dist(a[l + 1], a[l + 2])));

  int m = (l + r) / 2;
  double d = min(go(l, m), go(m + 1, r));

  vector<pp> v;
  for (auto i = l; i <= r; i++)
    if (sqr(a[i].first - a[m].first) < d)
      v.emplace_back(a[i].second, a[i].first);
  sort(v.begin(), v.end());
  for (auto i = v.begin(); i != v.end(); i++)
    for (auto j = i + 1; j != v.end(); j++) {
      if (sqr(j->first - i->first) > d)
        break;
      d = min(d, dist(*i, *j));
    }
  return d;
}

int main() {
  int i, n;
  double x, y;
  scanf("%d", &n);
  for (i = 0; i < n; i++)
    scanf("%lf%lf", &x, &y), a.emplace_back(x, y);
  sort(a.begin(), a.end());
  printf("%lf ", sqrt(go(0, n - 1)));
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <complex>
#include <cstdio>
#include <functional>
#include <iostream>
#include <list>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define _USE_MATH_DEFINES

#include <map>
#include <math.h>

#define INF 200000000

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;

struct P {
  double x, y;
  P(double x, double y) : x(x), y(y) {}
  P() : P(0, 0) {}
  P operator+(const P &v) const { return P(x + v.x, y + v.y); }
  P operator-(const P &v) const { return P(x - v.x, y - v.y); }
  P operator*(const double v) const { return P(x * v, y * v); }
  bool operator==(const P &v) const {
    return abs(x - v.x) <= 0.0000000000001 && abs(y - v.y) <= 0.00000000000001;
  }
  bool operator!=(const P &v) const {
    return abs(x - v.x) > 0.0000000000001 || abs(y - v.y) > 0.00000000000001;
  }

  double dot(const P &v) const { return x * v.x + y * v.y; }
  double cross(const P &v) const { return x * v.y - y * v.x; }
  double length() const { return sqrt(x * x + y * y); }
  double length2() const { return (x * x + y * y); }

  bool operator<(const P &v) const { return x < v.x || (x == v.x) && y < v.y; }
  bool operator>(const P &v) const { return x > v.x || (x == v.x) && y > v.y; }
};

int ccw(P a, P b, P c) {
  b = b - a;
  c = c - a;
  if (b.cross(c) > 0)
    return +1; // counter clockwise
  if (b.cross(c) < 0)
    return -1; // clockwise
  if (b.dot(c) < 0)
    return +2; // c--a--b on line
  if (b.length2() < c.length2())
    return -2; // a--b--c on line
  return 0;
}

double ClosestPair(vector<P> &points, int l, int r) {
  if (r - l == 1) {
    return INF;
  }
  if (r - l == 2) {
    return (points[l] - points[l + 1]).length();
  }

  int m = (l + r) / 2;
  double d = min(ClosestPair(points, l, m), ClosestPair(points, m, r));

  vector<P> tempP;
  for (int i = l; i < r; i++) {
    if ((points[l].x - points[m].x) * (points[l].x - points[m].x) < d) {
      tempP.emplace_back(P(points[i].y, points[i].x));
    }
  }
  sort(tempP.begin(), tempP.end());
  for (int i = 0; i != tempP.size(); i++) {
    for (int j = i + 1; j != tempP.size(); j++) {
      if ((tempP[j].x - tempP[i].x) * (tempP[j].x - tempP[i].x) > d) {
        break;
      }
      d = min(d, (tempP[i] - tempP[j]).length());
    }
  }

  return d;
}

vector<P> points;

int main() {
  points.reserve(100000);
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;

  for (int i = 0; i < n; i++) {
    double x, y;
    cin >> x >> y;
    points.push_back(P(x, y));
  }

  sort(points.begin(), points.end());

  printf("%.10lf\\n", ClosestPair(points, 0, n));

  return 0;
}