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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 100000000.0
#define EPS 1e-10
#define MOD 1000000007
using namespace std;
typedef complex<double> P;

int n;
double sx, sy, ex, ey;
double mx[50], my[50], Mx[50], My[50], h[50];

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 f(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);
}

double fx(double x1, double x2, double y, double z) {
  double r = x1, l = x2, mr, ml;
  rep(i, 50) {
    mr = (r * 2 + l) / 3;
    ml = (r + l * 2) / 3;
    if (f(P(sx, sy), P(ex, ey), P(mr, y)) < f(P(sx, sy), P(ex, ey), P(ml, y))) {
      l = ml;
    } else
      r = mr;
  }
  double ret = f(P(sx, sy), P(ex, ey), P(r, y));
  return sqrt(ret * ret + z * z);
}

double fy(double x, double y1, double y2, double z) {
  double r = y1, l = y2, mr, ml;
  rep(i, 50) {
    mr = (r * 2 + l) / 3;
    ml = (r + l * 2) / 3;
    if (f(P(sx, sy), P(ex, ey), P(x, mr)) < f(P(sx, sy), P(ex, ey), P(x, ml))) {
      l = ml;
    } else
      r = mr;
  }
  double ret = f(P(sx, sy), P(ex, ey), P(x, r));
  return sqrt(ret * ret + z * z);
}

void solve() {
  cin >> sx >> sy >> ex >> ey;
  rep(i, n) cin >> mx[i] >> my[i] >> Mx[i] >> My[i] >> h[i];
  rep(i, n) {
    if (mx[i] <= sx && sx <= Mx[i] && my[i] <= sy && sy <= My[i]) {
      puts("0.000");
      return;
    }
    if (mx[i] <= ex && ex <= Mx[i] && my[i] <= ey && ey <= My[i]) {
      puts("0.000");
      return;
    }
  }
  double s = 0.0, e = 10000.0, mid;
  rep(u, 50) {
    mid = (s + e) / 2;
    double dist = INF;
    rep(i, n) {
      double z = mid - h[i];
      if (z < 0)
        z = 0.0;
      dist = min(dist, fx(mx[i], Mx[i], my[i], z));
      dist = min(dist, fx(mx[i], Mx[i], My[i], z));
      dist = min(dist, fy(mx[i], my[i], My[i], z));
      dist = min(dist, fy(Mx[i], my[i], My[i], z));
    }
    if (dist < mid) {
      e = mid;
    } else
      s = mid;
  }
  printf("%.9f\\n", s);
}

int main() {
  while (cin >> n) {
    if (n == 0)
      break;
    solve();
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 100000000.0
#define EPS 1e-10
#define MOD 1000000007
using namespace std;
typedef pair<int, int> P;

int n;
double sx, sy, ex, ey;
double mx[50], my[50], Mx[50], My[50], h[50];

double f(double x, double y) {
  double r = 0, l = 1, mr, ml;
  rep(i, 30) {
    mr = (r * 2 + l) / 3;
    ml = (r + l * 2) / 3;
    if ((sx + (ex - sx) * mr - x) * (sx + (ex - sx) * mr - x) +
            (sy + (ey - sy) * mr - y) * (sy + (ey - sy) * mr - y) <
        (sx + (ex - sx) * ml - x) * (sx + (ex - sx) * ml - x) +
            (sy + (ey - sy) * ml - y) * (sy + (ey - sy) * ml - y)) {
      l = ml;
    } else
      r = mr;
  }
  return (sx + (ex - sx) * r - x) * (sx + (ex - sx) * r - x) +
         (sy + (ey - sy) * r - y) * (sy + (ey - sy) * r - y);
}

double F(double x, double y, double z) {
  double r = 0, l = 1, mr, ml;
  rep(i, 30) {
    mr = (r * 2 + l) / 3;
    ml = (r + l * 2) / 3;
    if ((sx + (ex - sx) * mr - x) * (sx + (ex - sx) * mr - x) +
            (sy + (ey - sy) * mr - y) * (sy + (ey - sy) * mr - y) <
        (sx + (ex - sx) * ml - x) * (sx + (ex - sx) * ml - x) +
            (sy + (ey - sy) * ml - y) * (sy + (ey - sy) * ml - y)) {
      l = ml;
    } else
      r = mr;
  }
  return sqrt((sx + (ex - sx) * r - x) * (sx + (ex - sx) * r - x) +
              (sy + (ey - sy) * r - y) * (sy + (ey - sy) * r - y) + z * z);
}

double fx(double x1, double x2, double y, double z) {
  double r = x1, l = x2, mr, ml;
  rep(i, 30) {
    mr = (r * 2 + l) / 3;
    ml = (r + l * 2) / 3;
    if (f(mr, y) < f(ml, y)) {
      l = ml;
    } else
      r = mr;
  }
  return F(r, y, z);
}

double fy(double x, double y1, double y2, double z) {
  double r = y1, l = y2, mr, ml;
  rep(i, 30) {
    mr = (r * 2 + l) / 3;
    ml = (r + l * 2) / 3;
    if (f(x, mr) < f(x, ml)) {
      l = ml;
    } else
      r = mr;
  }
  return F(x, r, z);
}

void solve() {
  cin >> sx >> sy >> ex >> ey;
  rep(i, n) cin >> mx[i] >> my[i] >> Mx[i] >> My[i] >> h[i];
  rep(i, n) {
    if (mx[i] <= sx && sx <= Mx[i] && my[i] <= sy && sy <= My[i]) {
      puts("0.000");
      return;
    }
    if (mx[i] <= ex && ex <= Mx[i] && my[i] <= ey && ey <= My[i]) {
      puts("0.000");
      return;
    }
  }
  double s = 0.0, e = 10000.0, mid;
  rep(u, 30) {
    mid = (s + e) / 2;
    double dist = INF;
    rep(i, n) {
      double z = mid - h[i];
      if (z < 0)
        z = 0.0;
      dist = min(dist, fx(mx[i], Mx[i], my[i], z));
      dist = min(dist, fx(mx[i], Mx[i], My[i], z));
      dist = min(dist, fy(mx[i], my[i], My[i], z));
      dist = min(dist, fy(Mx[i], my[i], My[i], z));
    }
    if (dist < mid) {
      e = mid;
    } else
      s = mid;
  }
  printf("%.9f\\n", s);
}

int main() {
  while (cin >> n) {
    if (n == 0)
      break;
    solve();
  }
}