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

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
static const double EPS = 1e-10;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rev(i, n) for (int i = n - 1; i >= 0; i--)
#define all(a) a.begin(), a.end()
#define mp(a, b, c) make_pair(make_pair(a, b), c)
#define pb(a) push_back(a)
#define SS stringstream
#define DBG1(a)                                                                \\
  rep(_X, sz(a)) { printf("%d ", a[_X]); }                                     \\
  puts("");
#define DBG2(a)                                                                \\
  rep(_X, sz(a)) {                                                             \\
    rep(_Y, sz(a[_X])) printf("%d ", a[_X][_Y]);                               \\
    puts("");                                                                  \\
  }
#define bitcount(b) __builtin_popcount(b)
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

#define delete(a, n) a.erase(remove(all(a), n), a.end())
template <typename T, typename S> vector<T> &operator<<(vector<T> &a, S b) {
  a.push_back(b);
  return a;
}
template <typename T> void operator>>(vector<T> &a, int b) {
  while (b--)
    if (!a.empty())
      a.pop_back();
}
bool isprime(int n) {
  if (n < 2)
    return false;
  for (int i = 2; i * i <= n; i++)
    if (n % i == 0)
      return false;
  return true;
}
ll b_pow(ll x, ll n) { return n ? b_pow(x * x, n / 2) * (n % 2 ? x : 1) : 1ll; }
string itos(int n) {
  stringstream ss;
  ss << n;
  return ss.str();
}
#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define ALL(c) (c).begin(), (c).end()

typedef unsigned int uint;
int in() {
  int x = 0, c;
  for (; (uint)((c = getchar()) - '0') >= 10;) {
    if (c == '-')
      return -in();
    if (!~c)
      throw ~0;
  }
  do {
    x = (x << 3) + (x << 1) + (c - '0');
  } while ((uint)((c = getchar()) - '0') < 10);
  return x;
}

typedef complex<double> P;
namespace std {
bool operator<(const P &a, const P &b) {
  return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);
}
} // namespace std

pair<P, P> closestPair(vector<P> p) {
  int n = p.size(), s = 0, t = 1, m = 2, S[n];
  S[0] = 0, S[1] = 1;
  sort(ALL(p)); // "p < q" <=> "p.x < q.x"
  double d = norm(p[s] - p[t]);
  for (int i = 2; i < n; S[m++] = i++) {
    REP(j, m) {
      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 make_pair(p[s], p[t]);
}

vector<int> v[20010] = {};

long long closestPair2(vector<P> &p) {
  long long dist = 10e10;
  for (int i = 0; i < 20010; i++) {
    v[i].push_back(-1000000);
    v[i].push_back(1000000);
    sort(v[i].begin(), v[i].end());
  }

  for (int i = 0; i < p.size(); i++) {
    int x = p[i].real(), y = p[i].imag();
    for (int X = x; X <= 20000; X++) {
      if ((long long)(x - X) * (x - X) > dist) {
        break;
      }
      int idx = lower_bound(v[X].begin(), v[X].end(), y) - v[X].begin();
      long long d1 = (long long)(v[X][idx] - y) * (v[X][idx] - y) +
                     (long long)(x - X) * (x - X);
      long long d2 = (long long)(v[X][idx - 1] - y) * (v[X][idx - 1] - y) +
                     (long long)(x - X) * (x - X);
      if (d1)
        dist = min(dist, d1);
      if (d2)
        dist = min(dist, d2);
    }
  }
  return dist;
}

vector<P> vv;
int main() {
  int n = in();
  for (int i = 0; i < n; i++) {
    int X = in();
    int Y = in();
    v[X + 10000].push_back(Y + 10000);
    vv.push_back(P(X + 10000, Y + 10000));
  }
  double a = closestPair2(vv);
  // pair<P,P> a = closestPair(v);
  // printf("%lld\\n",(long
  // long)(abs(a.first-a.second)*abs(a.first-a.second)+0.5));
  printf("%lld\\n", (long long)(a + 0.5));
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
static const double EPS = 1e-10;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rev(i, n) for (int i = n - 1; i >= 0; i--)
#define all(a) a.begin(), a.end()
#define mp(a, b, c) make_pair(make_pair(a, b), c)
#define pb(a) push_back(a)
#define SS stringstream
#define DBG1(a)                                                                \\
  rep(_X, sz(a)) { printf("%d ", a[_X]); }                                     \\
  puts("");
#define DBG2(a)                                                                \\
  rep(_X, sz(a)) {                                                             \\
    rep(_Y, sz(a[_X])) printf("%d ", a[_X][_Y]);                               \\
    puts("");                                                                  \\
  }
#define bitcount(b) __builtin_popcount(b)
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

#define delete(a, n) a.erase(remove(all(a), n), a.end())
template <typename T, typename S> vector<T> &operator<<(vector<T> &a, S b) {
  a.push_back(b);
  return a;
}
template <typename T> void operator>>(vector<T> &a, int b) {
  while (b--)
    if (!a.empty())
      a.pop_back();
}
bool isprime(int n) {
  if (n < 2)
    return false;
  for (int i = 2; i * i <= n; i++)
    if (n % i == 0)
      return false;
  return true;
}
ll b_pow(ll x, ll n) { return n ? b_pow(x * x, n / 2) * (n % 2 ? x : 1) : 1ll; }
string itos(int n) {
  stringstream ss;
  ss << n;
  return ss.str();
}
#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define ALL(c) (c).begin(), (c).end()

typedef unsigned int uint;
int in() {
  int x = 0, c;
  for (; (uint)((c = getchar()) - '0') >= 10;) {
    if (c == '-')
      return -in();
    if (!~c)
      throw ~0;
  }
  do {
    x = (x << 3) + (x << 1) + (c - '0');
  } while ((uint)((c = getchar()) - '0') < 10);
  return x;
}

typedef complex<double> P;
namespace std {
bool operator<(const P &a, const P &b) {
  return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);
}
} // namespace std

pair<P, P> closestPair(vector<P> p) {
  int n = p.size(), s = 0, t = 1, m = 2, S[n];
  S[0] = 0, S[1] = 1;
  sort(ALL(p)); // "p < q" <=> "p.x < q.x"
  double d = norm(p[s] - p[t]);
  for (int i = 2; i < n; S[m++] = i++)
    REP(j, m) {
      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 make_pair(p[s], p[t]);
}

vector<P> v;
const double PI = acos(-1);
int main() {
  srand(time(NULL));
  double t = (rand() % 1000) * 2 * PI / 1000.0;
  int n = in();
  for (int i = 0; i < n; i++) {
    double x = in();
    double y = in();
    double X = x * cos(t) - y * sin(t);
    double Y = x * sin(t) + y * cos(t);
    v.push_back(P(X, Y));
  }
  pair<P, P> a = closestPair(v);
  printf("%lld\\n",
         (long long)(abs(a.first - a.second) * abs(a.first - a.second) + 0.5));
}