Problem p01180 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <vector>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define inf (1 << 29)
#define eps (1e-8)
#define equals(a, b) (fabs((a) - (b)) < eps)
#define MAX 2000000
#define all(n) (n).begin(), (n).end()
using namespace std;
struct Circle {
  double x, y, r;
  Circle(double x = inf, double y = inf, double r = inf) : x(x), y(y), r(r) {}
  bool operator<(const Circle &c) const {
    if (!equals(x, c.x))
      return x < c.x;
    return y < c.y;
  }
};

bool compare_y(Circle a, Circle b) { return a.y < b.y; }

int N;
Circle A[MAX];

double compute(Circle *a, int n) {
  if (n <= 1)
    return inf;
  int m = n / 2;
  double x = a[m].x;
  double y = a[m].y;
  double r = a[m].r;
  double d = min(compute(a, m), compute(a + m, n - m));
  inplace_merge(a, a + m, a + n, compare_y);

  vector<Circle> b;

  rep(i, n) {
    double nx = a[i].x - x;
    double ny = a[i].y - y;
    double ndist = sqrt(nx * nx + ny * ny) - a[i].r - r;
    // if(equals(ndist,d) || ndist > d)continue;
    // if(nx >= d)continue;

    rep(j, (int)min((int)b.size(), 10)) {
      int last = b.size() - j - 1;
      double dx = a[i].x - b[last].x;
      double dy = a[i].y - b[last].y;
      double dist = sqrt(dx * dx + dy * dy) - a[i].r - b[last].r;

      if (equals(dist, d) || dist > d)
        continue;
      d = min(d, sqrt(dx * dx + dy * dy) - a[i].r - b[last].r);
    }
    b.push_back(a[i]);
  }
  return d;
}

int main() {
  while (cin >> N, N) {
    assert(N < MAX);
    rep(i, N) cin >> A[i].r >> A[i].x >> A[i].y;
    sort(A, A + N);

    cout << setiosflags(ios::fixed) << setprecision(8) << compute(A, N) << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>
#define EPS 1e-10
#define F first
#define S second
#define INF 1 << 29
using namespace std;

typedef pair<double, double> Pos;
typedef pair<Pos, double> P;
P p[1000000];

bool cmp_y(P a, P b) { return a.F.S < b.F.S; }

double rec(P *a, int n) {
  if (n <= 1)
    return INF;
  int m = n / 2;
  double x = a[m].F.F;
  double r = a[m].S;
  double d = min(rec(a, m), rec(a + m, n - m));
  inplace_merge(a, a + m, a + n, cmp_y);

  vector<P> b;
  for (int i = 0; i < n; i++) {
    if (a[i].F.F < x) {
      if ((a[i].F.F + a[i].S) - (x - r) >= d)
        continue;
    } else {
      if ((x + r) - (a[i].F.F + a[i].S) >= d)
        continue;
    }

    for (int j = 0; j < b.size(); j++) {
      double dx = a[i].F.F - b[b.size() - j - 1].F.F;
      double dy = a[i].F.S - b[b.size() - j - 1].F.S;

      if (a[i].F.S < b[b.size() - j - 1].F.S) {
        if (fabs((b[b.size() - j - 1].F.S - b[b.size() - j - 1].S) -
                 (a[i].F.S + a[i].S)) >= d)
          break;
      } else {
        if (fabs((a[i].F.S - a[i].S) -
                 (b[b.size() - j - i].F.S + b[b.size() - j - 1].S)) >= d)
          break;
      }

      d = min(d, sqrt(dx * dx + dy * dy) - a[i].S - b[b.size() - j - 1].S);
    }
    b.push_back(a[i]);
  }
  return d;
}

int main() {
  int N;
  double R, X, Y;

  while (cin >> N && N) {
    for (int i = 0; i < N; i++) {
      cin >> p[i].S >> p[i].F.F >> p[i].F.S;
    }
    sort(p, p + N);
    printf("%.6f\\n", rec(p, N));
  }

  return 0;
}