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

#include <bits/stdc++.h>
#define REP(i, a, n) for (ll i = ((ll)a); i < ((ll)n); i++)
using namespace std;
typedef long long ll;
typedef complex<double> Complex;

const double EPS = 1e-10, PI = M_PI;
ll N;
Complex P[10];
double L[10];

bool check(Complex p, vector<double> r) {
  bool ret = true;
  REP(i, 0, N) ret = ret && abs(p - P[i]) < r[i] + EPS;
  return ret;
}

bool ok(double h) {
  vector<double> r(N);
  REP(i, 0, N) {
    if (h > L[i])
      return false;
    r[i] = sqrt(L[i] * L[i] - h * h);
  }

  REP(i, 0, N) if (check(P[i], r)) return true;

  REP(i, 0, N) REP(j, i + 1, N) {
    Complex p1 = P[i], p2 = P[j];
    double r1 = r[i], r2 = r[j];

    if (abs(p2 - p1) > r1 + r2)
      continue;
    if (abs(p2 - p1) < abs(r2 - r1))
      continue;

    double d = abs(p2 - p1);
    double t = abs((d * d + r1 * r1 - r2 * r2) / (d * 2));
    double h = sqrt(r1 * r1 - t * t);

    Complex u = (p2 - p1) / d;
    Complex v = u * Complex(0, 1);
    Complex q1 = p1 + u * t - v * h;
    Complex q2 = p1 + u * t + v * h;

    if (check(q1, r))
      return true;
    if (check(q2, r))
      return true;
  }

  return false;
}

int main(void) {
  while (cin >> N, N) {
    REP(i, 0, N) {
      double x, y;
      cin >> x >> y >> L[i];
      P[i] = Complex(x, y);
    }

    double l = 1, h = 1e5;
    REP(_, 0, 100) {
      double m = (l + h) / 2;
      if (ok(m))
        l = m;
      else
        h = m;
    }
    printf("%.15lf\\n", l);
  }
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, a, n) for (ll i = ((ll)a); i < ((ll)n); i++)
using namespace std;
typedef long long ll;
typedef complex<double> Complex;

const double EPS = 1e-10, PI = M_PI;
ll N;
Complex P[10];
double L[10];

bool ok(double h) {
  vector<double> r(N);
  REP(i, 0, N) {
    if (h > L[i])
      return false;
    r[i] = sqrt(L[i] * L[i] - h * h);
  }

  bool ret = false;
  REP(m, 0, N) {
    vector<pair<double, double>> theta(N);
    REP(i, 0, N) if (i != m) {
      Complex p1 = P[m], p2 = P[i];
      double r1 = r[m], r2 = r[i];

      if (abs(p2 - p1) > r1 + r2)
        return false;
      if (abs(p2 - p1) < abs(r2 - r1)) {
        if (r1 < r2) {
          theta[i] = pair<double, double>(-PI, PI);
        } else {
          theta[i] = pair<double, double>(1e20, 1e20);
        }
        continue;
      }

      double d = abs(p2 - p1);
      double t = abs((d * d + r1 * r1 - r2 * r2) / (d * 2));
      double h = sqrt(r1 * r1 - t * t);

      Complex u = (p2 - p1) / d;
      Complex v = u * Complex(0, 1);
      Complex q1 = p1 + u * t - v * h;
      Complex q2 = p1 + u * t + v * h;

      double theta1 = arg(q1 - p1);
      double theta2 = arg(q2 - p1);
      theta[i] = pair<double, double>(theta1, theta2);
    }

    vector<double> uniq;
    REP(i, 0, N) if (i != m) {
      uniq.push_back(theta[i].first);
      uniq.push_back(theta[i].second);
    }
    sort(uniq.begin(), uniq.end());
    uniq.erase(unique(uniq.begin(), uniq.end()), uniq.end());

    vector<pair<ll, ll>> range(N);
    REP(i, 0, N) if (i != m) {
      range[i].first =
          lower_bound(uniq.begin(), uniq.end(), theta[i].first) - uniq.begin();
      range[i].second =
          lower_bound(uniq.begin(), uniq.end(), theta[i].second) - uniq.begin();
    }

    ll k = uniq.size();
    vector<ll> sum(k + 1, 0);
    REP(i, 0, N) if (i != m) {
      if (range[i].first <= range[i].second) {
        sum[range[i].first]++;
        sum[range[i].second + 1]--;
      } else {
        sum[range[i].first]++;
        sum[k]--;
        sum[0]++;
        sum[range[i].second + 1]--;
      }
    }
    REP(i, 0, k) sum[i + 1] += sum[i];

    REP(i, 0, k + 1) ret = ret || sum[i] >= N - 1;
  }

  return ret;
}

int main(void) {
  while (cin >> N, N) {
    REP(i, 0, N) {
      double x, y;
      cin >> x >> y >> L[i];
      P[i] = Complex(x, y);
    }

    double l = 1, h = 1e5;
    REP(_, 0, 100) {
      double m = (l + h) / 2;
      if (ok(m))
        l = m;
      else
        h = m;
    }
    printf("%.15lf\\n", l);
  }
}