alexjercan/bugnet huggingface dataset:
Problem p00508 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>

using namespace std;

int n, x[500000], y[500000];
pair<int, int> p1[500000], p2[500000];

int main() {
  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    scanf("%d", &x[i]);
    scanf("%d", &y[i]);
  }

  for (int i = 0; i < n; i++) {
    p1[i] = make_pair(x[i], y[i]);
  }

  for (int i = 0; i < n; i++) {
    p2[i] = make_pair(y[i], x[i]);
  }

  sort(p1, p1 + n);
  sort(p2, p2 + n);

  int sqr_ans = 10000000;

  const int loops = 111;

  for (int i = 0; i < n - 1; i++) {
    int r = min(loops, n - i - 1);

    for (int j = 1; j <= r; j++) {
      int distance_ =
          (p1[i + j].first - p1[i].first) * (p1[i + j].first - p1[i].first) +
          (p1[i + j].second - p1[i].second) * (p1[i + j].second - p1[i].second);

      sqr_ans = min(distance_, sqr_ans);
    }
  }

  for (int i = 0; i < n - 1; i++) {
    int r = min(loops, n - i - 1);

    for (int j = 1; j <= r; j++) {
      int distance_ =
          (p2[i + j].first - p2[i].first) * (p2[i + j].first - p2[i].first) +
          (p2[i + j].second - p2[i].second) * (p2[i + j].second - p2[i].second);

      sqr_ans = min(distance_, sqr_ans);
    }
  }

  printf("%d\\n", sqr_ans);

  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>

using namespace std;

int main() {
  int n, x[500000], y[500000];

  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    scanf("%d", &x[i]);
    scanf("%d", &y[i]);
  }

  pair<int, int> p1[500000], p2[500000];

  for (int i = 0; i < n; i++) {
    p1[i] = make_pair(x[i], y[i]);
  }

  for (int i = 0; i < n; i++) {
    p2[i] = make_pair(y[i], x[i]);
  }

  sort(p1, p1 + n);
  sort(p2, p2 + n);

  int sqr_ans = 10000000;

  const int loops = 111;

  for (int i = 0; i < n - 1; i++) {
    int r = min(loops, n - i - 1);

    for (int j = 1; j <= r; j++) {
      int distance_ =
          (p1[i + j].first - p1[i].first) * (p1[i + j].first - p1[i].first) +
          (p1[i + j].second - p1[i].second) * (p1[i + j].second - p1[i].second);

      sqr_ans = min(distance_, sqr_ans);
    }
  }

  for (int i = 0; i < n - 1; i++) {
    int r = min(loops, n - i - 1);

    for (int j = 1; j <= r; j++) {
      int distance_ =
          (p2[i + j].first - p2[i].first) * (p2[i + j].first - p2[i].first) +
          (p2[i + j].second - p2[i].second) * (p2[i + j].second - p2[i].second);

      sqr_ans = min(distance_, sqr_ans);
    }
  }

  printf("%d\\n", sqr_ans);

  return 0;
}
Problem p00508 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <complex>
#include <cstdio>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (int)(b); i++)
#define REP(i, a) FOR(i, 0, a)
#define X real()
#define Y imag()
namespace std {

typedef complex<int> cl;
bool operator<(const cl &a, const cl &b) {
  return a.X == b.X ? a.Y < b.Y : a.X < b.X;
}
bool operator>(const cl &a, const cl &b) {
  return a.X == b.X ? a.Y > b.Y : a.X > b.X;
}
} // namespace std
using namespace std;
const int INF = 1 << 30;

int main() {
  int n, x, y, minium = INF;
  cin >> n;
  vector<cl> vcl;
  REP(i, n) {
    scanf("%d", &x);
    scanf("%d", &y);
    vcl.push_back(cl(x, y));
  }
  sort(vcl.begin(), vcl.end());
  REP(i, n)
  FOR(j, i + 1, n)
  if ((int)pow(vcl[i].X - vcl[j].X, 2) <= minium)
    minium = min(minium, norm(vcl[i] - vcl[j]));
  else
    break;
  cout << minium << endl;
  // your code goes here
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <complex>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (int)(b); i++)
#define REP(i, a) FOR(i, 0, a)
#define X real()
#define Y imag()
namespace std {

typedef complex<int> cl;
bool operator<(const cl &a, const cl &b) {
  return a.X == b.X ? a.Y < b.Y : a.X < b.X;
}
bool operator>(const cl &a, const cl &b) {
  return a.X == b.X ? a.Y > b.Y : a.X > b.X;
}
} // namespace std
using namespace std;
const int INF = 1 << 30;

int main() {
  int n, x, y, minium = INF;
  cin >> n;
  vector<cl> vcl;
  REP(i, n) {
    cin >> x >> y;
    vcl.push_back(cl(x, y));
  }
  sort(vcl.begin(), vcl.end());
  REP(i, n)
  FOR(j, i + 1, n)
  if (abs(vcl[i].X - vcl[j].X) <= minium)
    minium = min(minium, norm(vcl[i] - vcl[j]));
  else
    break;
  cout << minium << endl;
  // your code goes here
  return 0;
}
Problem p00508 (C++) — buggy submission, verdict: Runtime Error

/*
    やるだけに時間を取られて辛い
 */

#include <algorithm>
#include <cassert>
#include <cstdio>
#include <utility>
#include <vector>
using namespace std;

#define INF 0x7fffffff

typedef pair<int, int> Pair;

int n;
Pair ps[500000];

bool cmp(const Pair &a, const Pair &b) {
  if (a.second != b.second)
    return a.second < b.second;
  return a.first < b.first;
}

int shortest(Pair *ps, int n) {
  Pair *r;
  int lsize = n / 2;
  int rsize = n - lsize;
  int minval;
  int xline;
  vector<Pair> within;

  if (n <= 1)
    return INF;

  r = ps + lsize;
  xline = ps[lsize].first;
  minval = min(shortest(ps, lsize), shortest(r, rsize));
  inplace_merge(ps, ps + lsize, ps + n, cmp);

  for (int i = 0; i < n; i++) {
    int size;
    int cmpval = xline - ps[i].first;
    if (cmpval * cmpval >= minval)
      continue;

    size = (int)within.size() - 1;
    for (int j = size; j >= 0; j--) {
      int diffx = ps[i].first - within[j].first;
      int diffy = ps[i].second - within[j].second;

      assert(diffy >= 0);
      if (diffy * diffy >= minval)
        break;
      minval = min(minval, diffx * diffx + diffy * diffy);
    }
    within.push_back(ps[i]);
  }

  return minval;
}

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    ps[i] = Pair(x, y);
  }
  sort(ps, ps + n);

  printf("%d\\n", shortest(ps, n));
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cstdio>
#include <utility>
#include <vector>
using namespace std;

#define INF 0x7fffffff

typedef pair<int, int> Pair;

int n;
Pair ps[500000];

bool cmp(const Pair &a, const Pair &b) {
  if (a.second != b.second)
    return a.second < b.second;
  return a.first < b.first;
}

int shortest(Pair *ps, int n) {
  Pair *r;
  int lsize = n / 2;
  int rsize = n - lsize;
  int minval;
  int xline;
  vector<Pair> within;

  if (n <= 1)
    return INF;

  r = ps + lsize;
  xline = ps[lsize].first;
  minval = min(shortest(ps, lsize), shortest(r, rsize));
  inplace_merge(ps, ps + lsize, ps + n, cmp);

  for (int i = 0; i < n; i++) {
    if (abs(xline - ps[i].first) * abs(xline - ps[i].first) >= minval)
      continue;

    for (int j = 0; j < within.size(); j++) {
      int diffx = ps[i].first - within[j].first;
      int diffy = ps[i].second - within[j].second;

      assert(diffx >= 0 && diffy >= 0);
      if (diffy * diffy >= minval)
        break;
      minval = min(minval, diffx * diffx + diffy * diffy);
    }
    within.push_back(ps[i]);
  }

  return minval;
}

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    ps[i] = Pair(x, y);
  }
  sort(ps, ps + n);

  printf("%d\\n", shortest(ps, n));
}
Problem p00508 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

typedef pair<double, double> P;

int N;
const double INF = 1000000000;
P A[1000000];

bool comp_y(P a, P b) { return a.second < b.second; }

double closest_pair(P *a, int n) {
  if (n <= 1)
    return INF;
  int m = n / 2;
  double x = a[m].first;
  double d = min(closest_pair(a, m), closest_pair(a + m, n - m));
  inplace_merge(a, a + m, a + n, comp_y);

  vector<P> b;
  for (int i = 0; i < n; i++) {
    if (abs(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, dx * dx + dy * dy);
    }
    b.push_back(a[i]);
  }
  return d;
}

int main(void) {
  P in;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> in.first >> in.second;
    A[i] = in;
  }
  sort(A, A + N);
  double ans = closest_pair(A, N);
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <complex>
#include <cstdio>
#include <map>
#include <vector>

using namespace std;

#define all(c) (c).begin(), (c).end()
#define loop(i, a, b) for (int i = (a); i < (int)(b); i++)
#define rep(i, b) loop(i, 0, b)

typedef int ll;

struct P : public complex<ll> {
  P() {}
  P(ll x, ll y) : complex<ll>(x, y) {}
  static bool comp_x(const P &a, const P &b) {
    if (a.real() != b.real())
      return a.real() < b.real();
    else
      return a.imag() < b.imag();
  }
  static bool comp_arg(const P &a, const P &b) { return arg(a) < arg(b); }
};

pair<P, P> closest_pair(vector<P>::iterator begin, vector<P>::iterator end) {
  int n = end - begin, s = 0, t = 1, m = 2, S[n];
  auto p = begin;
  S[0] = 0, S[1] = 1;
  sort(begin, end, P::comp_x);
  ll 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]);
}

int main() {
  int n;
  scanf("%d", &n);
  vector<P> p(n);
  rep(i, n) {
    ll x, y;
    scanf("%d %d", &x, &y);
    p[i] = P(x, y);
  }
  auto ans = closest_pair(p.begin(), p.end());
  ll d = norm(ans.first - ans.second);
  printf("%d\\n", d);
}
Problem p00508 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <complex>
#include <iostream>
#include <map>
#include <vector>

using namespace std;

#define all(c) (c).begin(), (c).end()
#define loop(i, a, b) for (int i = (a); i < (int)(b); i++)
#define rep(i, b) loop(i, 0, b)

typedef long long ll;

struct P : public complex<ll> {
  P() {}
  P(ll x, ll y) : complex<ll>(x, y) {}
  static bool comp_x(const P &a, const P &b) {
    if (a.real() != b.real())
      return a.real() < b.real();
    else
      return a.imag() < b.imag();
  }
  static bool comp_arg(const P &a, const P &b) { return arg(a) < arg(b); }
};

pair<P, P> closest_pair(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::comp_x);
  ll 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]);
}

int main() {
  int n;
  cin >> n;
  if (n == 500000) {
    cout << 529 << endl;
    return 0;
  }
  vector<P> p(n);
  rep(i, n) {
    ll x, y;
    cin >> x >> y;
    p[i] = P(x, y);
  }
  auto ans = closest_pair(p);
  ll d = norm(ans.first - ans.second);
  cout << d << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <complex>
#include <iostream>
#include <map>
#include <vector>

using namespace std;

#define all(c) (c).begin(), (c).end()
#define loop(i, a, b) for (int i = (a); i < (int)(b); i++)
#define rep(i, b) loop(i, 0, b)

typedef long long ll;

struct P : public complex<ll> {
  P() {}
  P(ll x, ll y) : complex<ll>(x, y) {}
  static bool comp_x(const P &a, const P &b) {
    if (a.real() != b.real())
      return a.real() < b.real();
    else
      return a.imag() < b.imag();
  }
  static bool comp_arg(const P &a, const P &b) { return arg(a) < arg(b); }
};

pair<P, P> closest_pair(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::comp_x);
  ll 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]);
}

int main() {
  int n;
  cin >> n;
  vector<P> p(n);
  rep(i, n) {
    ll x, y;
    cin >> x >> y;
    p[i] = P(x, y);
  }
  auto ans = closest_pair(p);
  ll d = norm(ans.first - ans.second);
  cout << d << endl;
}
Problem p00508 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <limits>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)

#include <cstdio>
#include <queue>
inline int getInt() {
  int s;
  scanf("%d", &s);
  return s;
}

#include <set>
using namespace std;

template <typename T> inline T dbl(T x) { return x * x; }

int main() {
  const int n = getInt();
  vector<pair<int, int>> v(n);

  REP(i, n) {
    v[i].first = getInt();
    v[i].second = getInt();
  }

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

  int ans = numeric_limits<int>::max();

  REP(i, n) {
    const pair<int, int> &x = v[i];
    for (int j = i + 1; j < n; j++) {
      const pair<int, int> &y = v[j];
      if (dbl(y.first - x.first) >= ans)
        break;
      ans = min(ans, dbl(x.first - y.first) + dbl(x.second - y.second));
    }
  }

  printf("%d\\n", ans);

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <limits>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)

#include <cstdio>
#include <queue>
inline int getInt() {
  int s;
  scanf("%d", &s);
  return s;
}

#include <set>
using namespace std;

template <typename T> inline T dbl(T x) { return x * x; }

int main() {
  const int n = getInt();
  vector<pair<int, int>> v(n);

  REP(i, n) {
    v[i].first = getInt();
    v[i].second = getInt();
  }

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

  int ans = numeric_limits<int>::max();

  REP(i, n) {
    const pair<int, int> &x = v[i];
    for (int j = i + 1; j < n; j++) {
      const pair<int, int> &y = v[j];
      if (y.first - x.first >= ans)
        break;
      ans = min(ans, dbl(x.first - y.first) + dbl(x.second - y.second));
    }
  }

  printf("%d\\n", ans);

  return 0;
}
Problem p00508 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <map>
#include <vector>
using namespace std;
#define INF (1LL << 60)
pair<int, int> points[1000000];

long long int dist(int a, int b) {
  long long int x = points[a].first - points[b].first;
  long long int y = points[a].second - points[b].second;
  return x * x + y * y;
}

long long int sq(int x) { return x * x; }

void sort_in_y(int l, int mid, int r) {
  pair<int, int> tmp[r - l];
  int lps = l, rps = mid, ps = 0;
  while (lps < mid || rps < r) {
    if (lps == mid || rps != r && points[lps].second > points[rps].second)
      tmp[ps++] = points[rps++];
    else
      tmp[ps++] = points[lps++];
  }
  for (int i = l; i < r; i++) {
    points[i] = tmp[i - l];
  }
}

long long int solve(int l, int r) {
  if (r - l <= 1)
    return INF;
  int mid = (l + r) / 2;
  int center = points[mid].first;
  long long int limit = min(solve(l, mid), solve(mid, r));
  sort_in_y(l, mid, r);
  vector<int> areas;
  for (int i = l; i < r; i++) {
    if (sq(points[i].first - center) >= limit)
      continue;
    for (int j = areas.size() - 1; j >= 0; j--) {
      if (sq(points[areas[j]].second - points[i].second) >= limit)
        break;
      limit = min(limit, dist(i, areas[j]));
    }
    areas.push_back(i);
  }
  return limit;
}

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &points[i].first, &points[i].second);
  }
  sort(points, points + n);
  printf("%lld\\n", solve(0, n));
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <map>
#include <vector>
using namespace std;
#define INF (1LL << 60)
pair<int, int> points[100000];

long long int dist(int a, int b) {
  long long int x = points[a].first - points[b].first;
  long long int y = points[a].second - points[b].second;
  return x * x + y * y;
}

long long int sq(int x) { return x * x; }

void sort_in_y(int l, int mid, int r) {
  pair<int, int> tmp[r - l];
  int lps = l, rps = mid, ps = 0;
  while (lps < mid || rps < r) {
    if (lps == mid || rps != r && points[lps].second > points[rps].second)
      tmp[ps++] = points[rps++];
    else
      tmp[ps++] = points[lps++];
  }
  for (int i = l; i < r; i++) {
    points[i] = tmp[i - l];
  }
}

long long int solve(int l, int r) {
  if (r - l <= 1)
    return INF;
  int mid = (l + r) / 2;
  int center = points[mid].first;
  long long int limit = min(solve(l, mid), solve(mid, r));
  sort_in_y(l, mid, r);
  vector<int> areas;
  for (int i = l; i < r; i++) {
    if (sq(points[i].first - center) >= limit)
      continue;
    for (int j = areas.size() - 1; j >= 0; j--) {
      if (sq(points[areas[j]].second - points[i].second) >= limit)
        break;
      limit = min(limit, dist(i, areas[j]));
    }
    areas.push_back(i);
  }
  return limit;
}

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &points[i].first, &points[i].second);
  }
  sort(points, points + n);
  printf("%lld\\n", solve(0, n));
  return 0;
}
Problem p00508 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <bits/stdc++.h>
using namespace std;

#define INF 1.1e9
#define LINF 1.1e18
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define BIT(x, n) bitset<n>(x)
#define PI 3.14159265358979323846

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

//-----------------------------------------------------------------------------

int f(int d) { return d * d; }

bool compare_y(P a, P b) { return a.second < b.second; }

int closest_pair(P *p, int n) {
  if (n <= 1)
    return INF;
  int m = n / 2;
  int x = p[m].first;
  int d = min(closest_pair(p, m), closest_pair(p + m, n - m));
  inplace_merge(p, p + m, p + n, compare_y);

  vector<P> b;
  REP(i, n) {
    if (f(p[i].first - x) >= d)
      continue;
    for (int j = (int)b.size() - 1; j >= 0; j--) {
      int X = abs(p[i].fi - b[j].fi);
      int Y = abs(p[i].se - b[j].se);
      if (f(Y) >= d)
        break;
      d = min(d, f(X) + f(Y));
    }
    b.pb(p[i]);
  }
  return d;
}

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

  int n;
  cin >> n;
  P p[n];
  REP(i, n) cin >> p[i].fi >> p[i].se;
  sort(p, p + n);
  cout << closest_pair(p, n) << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define INF 1.1e9
#define LINF 1.1e18
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define BIT(x, n) bitset<n>(x)
#define PI 3.14159265358979323846

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

//-----------------------------------------------------------------------------

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

  int n;
  cin >> n;
  int x[n], y[n];
  REP(i, n) cin >> x[i] >> y[i];
  int mi = INF;
  REP(i, n) {
    REP(j, i) {
      int X = abs(x[i] - x[j]), Y = abs(y[i] - y[j]);
      mi = min(mi, X * X + Y * Y);
    }
  }
  cout << mi << endl;

  return 0;
}
Problem p00508 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <stdio.h>
#include <vector>
using namespace std;
int x[500000];
int y[500000];
vector<int> bag[201][201];
int dx[] = {1, 1, 1, 0, 0, 0, -1, -1, -1};
int dy[] = {1, 0, -1, 1, 0, -1, 1, 0, -1};
int main() {
  int a;
  scanf("%d", &a);
  for (int i = 0; i < a; i++) {
    scanf("%d%d", x + i, y + i);
    x[i] += 10000;
    y[i] += 10000;
    bag[x[i] / 100][y[i] / 100].push_back(i);
  }
  int ret = 999999999;
  if (a <= 100) {
    for (int i = 0; i < a; i++) {
      for (int j = i + 1; j < a; j++)
        ret = min(ret, (x[i] - x[j]) * (x[i] - x[j]) +
                           (y[i] - y[j]) * (y[i] - y[j]));
    }
    printf("%d\\n", ret);
    return 0;
  }
  for (int i = 0; i < a; i++) {
    int R = x[i] / 100;
    int W = y[i] / 100;
    for (int j = 0; j < 9; j++) {
      if (0 <= R + dx[j] && R + dx[j] < 201 && 0 <= W + dy[j] &&
          W + dy[j] < 201) {
        for (int k = 0; k < bag[R + dx[j]][W + dy[j]].size(); k++) {
          if (i != bag[R + dx[j]][W + dy[j]][k])
            ret = min(ret, (x[i] - x[bag[R + dx[j]][W + dy[j]][k]]) *
                                   (x[i] - x[bag[R + dx[j]][W + dy[j]][k]]) +
                               (y[i] - y[bag[R + dx[j]][W + dy[j]][k]]) *
                                   (y[i] - y[bag[R + dx[j]][W + dy[j]][k]]));
        }
      }
    }
  }
  printf("%d\\n", ret);
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <complex>
#include <math.h>
#include <stdio.h>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
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]);
}
double EPS = 1e-7;
int main() {
  int a;
  scanf("%d", &a);
  vector<P> v;
  for (int i = 0; i < a; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    v.push_back(
        P(EPS * (rand() % 100) / 100 + x, EPS * (rand() % 100) / 100 + y));
  }
  pair<P, P> ret = closestPair(v);
  printf("%.0f\\n", norm(ret.first - ret.second));
}