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

#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

#include <complex>
typedef complex<double> P;
#define EPS (1e-8)
#define EQ(x, y) (abs((x) - (h)) < EPS)
#define LT(x, y) ((x) - (y) <= -EPS)
#define LE(x, y) ((x) - (y) < +EPS)
const double PI = acos(-1.0);

double dot(P a, P b) { return (a.real() * b.real() + a.imag() * b.imag()); }
double cross(P a, P b) { return (a.real() * b.imag() - a.imag() * b.real()); }
double distance_l_p(P a, P b, P c) {
  return abs(cross(b - a, c - a)) / abs(b - a);
}
bool interpoint_lc(P a, P b, P c, double r, P ans[]) {
  double di = distance_l_p(a, b, c);
  if (di + EPS > r)
    return false;
  P v = (a - b) / abs(a - b);
  P rv = v * P(0, 1) * di;
  if (distance_l_p(a, b, c + rv) > di + EPS)
    rv = -rv;
  v *= sqrt(r * r - di * di);
  ans[0] = c + rv - v;
  ans[1] = c + rv + v;
  return true;
}
vector<P> cross_circles(P c1, double r1, P c2, double r2) {
  double d = abs(c2 - c1);
  if (LT(r1 + r2, d) || LT(d, fabs(r1 - r2)))
    return vector<P>();
  double l = 0.5 * ((r1 * r1 - r2 * r2) / d + d);
  double h = sqrt(r1 * r1 - l * l);
  vector<P> ret(2);
  ret[0] = P(l, +h) * (c2 - c1) / d + c1;
  ret[1] = P(l, -h) * (c2 - c1) / d + c1;
  return ret;
}
double distance_ls_p(P a, P b, P c) {
  if (dot(b - a, c - a) < EPS)
    return abs(c - a);
  if (dot(a - b, c - b) < EPS)
    return abs(c - b);
  return abs(cross(b - a, c - a)) / abs(b - a);
}
int n, w;
double get_dist(double r, int h) { return sqrt(r * r - (r - h) * (r - h)); }
double get_dist(P a, P b) {
  double dx = a.real() - b.real();
  double dy = a.imag() - b.imag();
  return sqrt(dx * dx + dy * dy);
}
bool ok(vi &x, vi &y, vi &h, double r) {
  vector<pair<P, double>> circles;
  vector<pair<P, P>> lines;
  REP(i, n) {
    if (LE(r, h[i])) {
      circles.push_back(make_pair(P(x[i], y[i]), r));
    } else {
      double d = get_dist(r, h[i]);
      circles.push_back(make_pair(P(x[i], y[i]), d));
    }
  }

  double d;
  if (LE(r, w)) {
    d = r;
  } else {
    d = get_dist(r, w);
  }
  lines.push_back(make_pair(P(0, d), P(100, d)));
  lines.push_back(make_pair(P(0, 100 - d), P(100, 100 - d)));
  lines.push_back(make_pair(P(d, 0), P(d, 100)));
  lines.push_back(make_pair(P(100 - d, 0), P(100 - d, 100)));

  vector<P> points;
  REP(i, circles.size()) {
    FOR(j, i + 1, circles.size()) {
      vector<P> c = cross_circles(circles[i].first, circles[i].second,
                                  circles[j].first, circles[j].second);
      if (c.size()) {
        points.push_back(c[0]);
        points.push_back(c[1]);
      }
    }
    REP(j, lines.size()) {
      P c[2];
      if (interpoint_lc(lines[j].first, lines[j].second, circles[i].first,
                        circles[i].second, c)) {
        points.push_back(c[0]);
        points.push_back(c[1]);
      }
    }
  }
  points.push_back(P(d, d));
  points.push_back(P(100 - d, d));
  points.push_back(P(d, 100 - d));
  points.push_back(P(100 - d, 100 - d));

  vector<pair<P, P>> walls;
  walls.push_back(make_pair(P(0, 0), P(100, 0)));
  walls.push_back(make_pair(P(0, 100 - 0), P(100, 100 - 0)));
  walls.push_back(make_pair(P(0, 0), P(0, 100)));
  walls.push_back(make_pair(P(100 - 0, 0), P(100 - 0, 100)));

  /*
  cerr<<"circles"<<endl;
  REP(i,circles.size()){
      cerr<<circles[i].first.real()<<" "<<circles[i].first.imag()<<"
  "<<circles[i].second<<endl;
  }
  cerr<<"lines"<<endl;
  REP(i,lines.size()){
      cerr<<lines[i].first.real()<<" "<<lines[i].first.imag()<<"
  "<<lines[i].second.real()<<" "<<lines[i].second.imag()<<endl;
  }
  cerr<<"points"<<endl;
  REP(i,points.size()){
      cerr<<points[i].real()<<" "<<points[i].imag()<<endl;
  }
  */
  bool can = false;
  REP(i, points.size()) {
    if (LT(points[i].real(), 0) || LT(points[i].imag(), 0) ||
        LT(100, points[i].real()) || LT(100, points[i].imag())) {
      continue;
    }
    bool tmp = true;
    REP(j, n) {
      double d1 = (LE(r, h[j]) ? r : get_dist(r, h[j]));
      if (LT(get_dist(points[i], P(x[j], y[j])), d1)) {
        /*
        cerr<<i<<"-(needle)-"<<j<<endl;
        cerr<<get_dist(points[i],P(x[j],y[j]))<<" "<<d1<<endl;
        */
        tmp = false;
      }
    }
    REP(j, lines.size()) {
      double d2 = (LE(r, w) ? r : get_dist(r, w));
      if (LT(distance_l_p(walls[j].first, walls[j].second, points[i]), d2)) {
        /*
        cerr<<i<<"-(wall)-"<<j<<endl;
        cerr<<distance_l_p(walls[j].first,walls[j].second,points[i])<<"
        "<<d2<<endl;
        */
        tmp = false;
      }
    }
    if (tmp) {
      can = true;
      break;
    }
  }
  return can;
}
int main() {
  cout.precision(30);
  while (cin >> n >> w, n | w) {
    vi x(n), y(n), h(n);
    REP(i, n) { cin >> x[i] >> y[i] >> h[i]; }

    /*
    ok(x,y,h,100);
    continue;
    */
    double lb = 0, ub = 100000;
    REP(i, 1000) {
      double mid = (lb + ub) / 2;
      if (!ok(x, y, h, mid)) {
        ub = mid;
      } else {
        lb = mid;
      }
    }
    cout << ub << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

#include <complex>
typedef complex<double> P;
#define EPS (1e-8)
#define EQ(x, y) (abs((x) - (h)) < EPS)
#define LT(x, y) ((x) - (y) <= -EPS)
#define LE(x, y) ((x) - (y) < +EPS)
const double PI = acos(-1.0);

double dot(P a, P b) { return (a.real() * b.real() + a.imag() * b.imag()); }
double cross(P a, P b) { return (a.real() * b.imag() - a.imag() * b.real()); }
double distance_l_p(P a, P b, P c) {
  return abs(cross(b - a, c - a)) / abs(b - a);
}
bool interpoint_lc(P a, P b, P c, double r, P ans[]) {
  double di = distance_l_p(a, b, c);
  if (di + EPS > r)
    return false;
  P v = (a - b) / abs(a - b);
  P rv = v * P(0, 1) * di;
  if (distance_l_p(a, b, c + rv) > di + EPS)
    rv = -rv;
  v *= sqrt(r * r - di * di);
  ans[0] = c + rv - v;
  ans[1] = c + rv + v;
  return true;
}
vector<P> cross_circles(P c1, double r1, P c2, double r2) {
  double d = abs(c2 - c1);
  if (LT(r1 + r2, d) || LT(d, fabs(r1 - r2)))
    return vector<P>();
  double l = 0.5 * ((r1 * r1 - r2 * r2) / d + d);
  double h = sqrt(r1 * r1 - l * l);
  vector<P> ret(2);
  ret[0] = P(l, +h) * (c2 - c1) / d + c1;
  ret[1] = P(l, -h) * (c2 - c1) / d + c1;
  return ret;
}
double distance_ls_p(P a, P b, P c) {
  if (dot(b - a, c - a) < EPS)
    return abs(c - a);
  if (dot(a - b, c - b) < EPS)
    return abs(c - b);
  return abs(cross(b - a, c - a)) / abs(b - a);
}
int n, w;
double get_dist(double r, int h) { return sqrt(r * r - (r - h) * (r - h)); }
double get_dist(P a, P b) {
  double dx = a.real() - b.real();
  double dy = a.imag() - b.imag();
  return sqrt(dx * dx + dy * dy);
}
bool ok(vi &x, vi &y, vi &h, double r) {
  vector<pair<P, double>> circles;
  vector<pair<P, P>> lines;
  REP(i, n) {
    if (LE(r, h[i])) {
      circles.push_back(make_pair(P(x[i], y[i]), r));
    } else {
      double d = get_dist(r, h[i]);
      circles.push_back(make_pair(P(x[i], y[i]), d));
    }
  }

  double d;
  if (LE(r, w)) {
    d = r;
  } else {
    d = get_dist(r, w);
  }
  lines.push_back(make_pair(P(0, d), P(100, d)));
  lines.push_back(make_pair(P(0, 100 - d), P(100, 100 - d)));
  lines.push_back(make_pair(P(d, 0), P(d, 100)));
  lines.push_back(make_pair(P(100 - d, 0), P(100 - d, 100)));

  vector<P> points;
  REP(i, circles.size()) {
    FOR(j, i + 1, circles.size()) {
      vector<P> c = cross_circles(circles[i].first, circles[i].second,
                                  circles[j].first, circles[j].second);
      if (c.size()) {
        points.push_back(c[0]);
        points.push_back(c[1]);
      }
    }
    REP(j, lines.size()) {
      P c[2];
      if (interpoint_lc(lines[j].first, lines[j].second, circles[i].first,
                        circles[i].second, c)) {
        points.push_back(c[0]);
        points.push_back(c[1]);
      }
    }
  }
  points.push_back(P(d, d));
  points.push_back(P(100 - d, d));
  points.push_back(P(d, 100 - d));
  points.push_back(P(100 - d, 100 - d));

  vector<pair<P, P>> walls;
  walls.push_back(make_pair(P(0, 0), P(100, 0)));
  walls.push_back(make_pair(P(0, 100 - 0), P(100, 100 - 0)));
  walls.push_back(make_pair(P(0, 0), P(0, 100)));
  walls.push_back(make_pair(P(100 - 0, 0), P(100 - 0, 100)));

  cerr << "circles" << endl;
  REP(i, circles.size()) {
    cerr << circles[i].first.real() << " " << circles[i].first.imag() << " "
         << circles[i].second << endl;
  }
  cerr << "lines" << endl;
  REP(i, lines.size()) {
    cerr << lines[i].first.real() << " " << lines[i].first.imag() << " "
         << lines[i].second.real() << " " << lines[i].second.imag() << endl;
  }
  cerr << "points" << endl;
  REP(i, points.size()) {
    cerr << points[i].real() << " " << points[i].imag() << endl;
  }
  bool can = false;
  REP(i, points.size()) {
    if (LT(points[i].real(), 0) || LT(points[i].imag(), 0) ||
        LT(100, points[i].real()) || LT(100, points[i].imag())) {
      continue;
    }
    bool tmp = true;
    REP(j, n) {
      double d1 = (LE(r, h[j]) ? r : get_dist(r, h[j]));
      if (LT(get_dist(points[i], P(x[j], y[j])), d1)) {
        cerr << i << "-(needle)-" << j << endl;
        cerr << get_dist(points[i], P(x[j], y[j])) << " " << d1 << endl;
        tmp = false;
      }
    }
    REP(j, lines.size()) {
      double d2 = (LE(r, w) ? r : get_dist(r, w));
      if (LT(distance_l_p(walls[j].first, walls[j].second, points[i]), d2)) {
        cerr << i << "-(wall)-" << j << endl;
        cerr << distance_l_p(walls[j].first, walls[j].second, points[i]) << " "
             << d2 << endl;
        tmp = false;
      }
    }
    if (tmp) {
      can = true;
      break;
    }
  }
  return can;
}
int main() {
  cout.precision(30);
  while (cin >> n >> w, n | w) {
    vi x(n), y(n), h(n);
    REP(i, n) { cin >> x[i] >> y[i] >> h[i]; }

    /*
    ok(x,y,h,100);
    continue;
    */
    double lb = 0, ub = 100000;
    REP(i, 1000) {
      double mid = (lb + ub) / 2;
      if (!ok(x, y, h, mid)) {
        ub = mid;
      } else {
        lb = mid;
      }
    }
    cout << ub << endl;
  }
}