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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef tuple<int, int> duo;
const int dx[] = {0, 0, 1, -1, 1, 1, -1, -1};
const int dy[] = {1, -1, 0, 0, 1, -1, 1, -1};
const int Mod = 1000000000 + 0;
//{{{ templates
#define TT_ template <typename T> inline
#define TTF_ template <typename T, typename F> inline
TT_ T sq(T x) { return x * x; }
TT_ T In() {
  T x;
  cin >> x;
  return x;
}
TT_ void Out(T &x) { cout << x; }
TT_ void sort(T &v) { sort(begin(v), end(v)); }
TT_ void revs(T &v) { reverse(begin(v), end(v)); }
TT_ void uniq(T &v) {
  sort(v);
  v.erase(unique(begin(v), end(v)), end(v));
}
TT_ int ubnd(T &v, typename T::value_type x) {
  return upper_bound(begin(v), end(v), x) - begin(v);
}
TT_ int lbnd(T &v, typename T::value_type x) {
  return lower_bound(begin(v), end(v), x) - begin(v);
}
TTF_ void inpt(T &v, int n, F f) {
  for (v.reserve(n); n--; v.emplace_back(f()))
    ;
}
TTF_ void show(T &v, F f, string d = " ", string e = "\\n") {
  int i = 0;
  for (auto &x : v)
    i++ && (cout << d), f(x);
  cout << e;
}
TT_ typename T::iterator minel(T &v) { return min_element(begin(v), end(v)); }
TT_ typename T::iterator maxel(T &v) { return max_element(begin(v), end(v)); }

inline void fast_io() {
  ios::sync_with_stdio(0);
  cin.tie(0);
}
inline int in() {
  int x;
  scanf("%d", &x);
  return x;
}
inline ll pow_mod(ll a, ll k, ll m) {
  ll r = 1;
  for (; k > 0; a = a * a % m, k >>= 1)
    if (k & 1)
      r = r * a % m;
  return r;
}
inline ll mod_inv(ll a, ll p) { return pow_mod(a, p - 2, p); }
//}}} priority_queue queue deque front stringstream max_element min_element
//insert count make_tuple

template <typename T> struct Point {
  typedef Point<T> this_t;
  T x, y;
  Point() {}
  Point(T x, T y) : x(x), y(y) {}
  void read(const char *fs) { scanf(fs, &x, &y); }
  inline T dist(const this_t &a) const { return sq(x - a.x) + sq(y - a.y); }
};
typedef Point<int> Pi;

int bruteForse(Pi *P, int n) {
  int mini = 1 << 30;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      mini = min(mini, P[i].dist(P[j]));
    }
  }
  return mini;
}
int stripClosest(Pi *strip, int n, int d) {
  int mini = d;
  sort(strip, strip + n, [](const Pi &a, const Pi &b) { return a.y < b.y; });
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (strip[j].y - strip[i].y >= mini)
        break;
      mini = min(mini, strip[i].dist(strip[j]));
    }
  }
  return mini;
}
int closestUtil(Pi *P, int n) {
  if (n <= 3)
    return bruteForse(P, n);
  int mid = n >> 1;
  auto midp = P[mid];
  int L = closestUtil(P, mid);
  int R = closestUtil(P + mid, n - mid);
  int d = min(L, R);
  Pi strip[n];
  int sl = 0;
  for (int i = 0; i < n; i++) {
    if (abs(P[i].x - midp.x) < d) {
      strip[sl] = P[i];
      sl++;
    }
  }
  return min(d, stripClosest(strip, sl, d));
}
int closest(Pi *P, int n) {
  sort(P, P + n, [](const Pi &a, const Pi &b) { return a.x < b.x; });
  return closestUtil(P, n);
}

Pi Pn[500500];
int main() {
  int N = in();
  for (int i = 0; i < N; i++)
    Pn[i].read("%d%d");
  cout << closest(Pn, N) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef tuple<int, int> duo;
typedef complex<int> P;

const int dx[] = {0, 0, 1, -1, 1, 1, -1, -1};
const int dy[] = {1, -1, 0, 0, 1, -1, 1, -1};
const int Mod = 1000000000 + 0;
//{{{ templates
#define TT_ template <typename T> inline
#define TTF_ template <typename T, typename F> inline
TT_ T sq(T x) { return x * x; }
TT_ T In() {
  T x;
  cin >> x;
  return x;
}
TT_ void Out(T &x) { cout << x; }
TT_ void sort(T &v) { sort(begin(v), end(v)); }
TT_ void revs(T &v) { reverse(begin(v), end(v)); }
TT_ void uniq(T &v) {
  sort(v);
  v.erase(unique(begin(v), end(v)), end(v));
}
TT_ int ubnd(T &v, typename T::value_type &x) {
  return upper_bound(begin(v), end(v), x) - begin(v);
}
TT_ int lbnd(T &v, typename T::value_type &x) {
  return lower_bound(begin(v), end(v), x) - begin(v);
}
TTF_ void inpt(T &v, int n, F f) {
  for (v.reserve(n); n--; v.emplace_back(f()))
    ;
}
TTF_ void show(T &v, F f, string d = " ", string e = "\\n") {
  int i = 0;
  for (auto &x : v)
    i++ && (cout << d), f(x);
  cout << e;
}
TT_ typename T::iterator minel(T &v) { return min_element(begin(v), end(v)); }
TT_ typename T::iterator maxel(T &v) { return max_element(begin(v), end(v)); }

inline void fast_io() {
  ios::sync_with_stdio(0);
  cin.tie(0);
}
inline int in() {
  int x;
  scanf("%d", &x);
  return x;
}
inline ll pow_mod(ll a, ll k, ll m) {
  ll r = 1;
  for (; k > 0; a = a * a % m, k >>= 1)
    if (k & 1)
      r = r * a % m;
  return r;
}
inline ll mod_inv(ll a, ll p) { return pow_mod(a, p - 2, p); }
//}}} priority_queue queue deque front stringstream max_element min_element
//insert count make_tuple
namespace std {
inline bool operator<(const P &a, const P &b) { return real(a) < real(b); }
} // namespace std

inline int closetPair(vector<P> &p) {
  int n = p.size(), s = 0, t = 1, m = 2, S[n];
  S[0] = 0;
  S[1] = 1;
  sort(p);
  int d = norm(p[s] - p[t]);
  for (int i = 2; i < n; S[m++] = i++) {
    for (int j = 0; j < m; j++) {
      if (norm(p[S[j]] - p[i]) < d)
        d = norm(p[s = S[j]] - p[t = i]);
      if (real(p[S[j]]) < real(p[i]) - d)
        S[j--] = S[--m];
    }
  }
  return norm(p[s] - p[t]);
}

int main() {
  vector<P> ps;
  int n = in();
  ps.reserve(n);
  for (int i = 0; i < n; i++) {
    int x, y;
    x = in(), y = in();
    ps.emplace_back(x, y);
  }
  cout << closetPair(ps) << endl;
  return 0;
}