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

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

#define fi first
#define se second
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define repr(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb(s) push_back(s)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x " = " << x << endl
#define print(x) cout << x << endl
#define maxch(x, y) x = max(x, y)
#define minch(x, y) x = min(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt(x) bitset<32>(x).count()

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<double, double> PDD;
typedef pair<P, int> PPI;
typedef pair<int, P> PIP;
typedef pair<ll, ll> PL;
typedef pair<P, ll> PPL;
typedef set<int> S;

#define INF INT_MAX / 3
#define MAX_N 1000000001

PDD operator-(const PDD &a, const PDD &b) {
  return PDD(a.fi - b.fi, a.se - b.se);
}

double len(PDD p) { return sqrt(p.fi * p.fi + p.se * p.se); }

double dot(PDD a, PDD b) { return a.fi * b.fi + a.se * b.se; }

double cross(PDD a, PDD b) { return a.fi * b.se - a.se * b.fi; }

double distance(PDD p1, PDD p2, PDD q1, PDD q2) {
  if (cross(p2 - p1, q1 - p1) * cross(p2 - p1, q2 - p1) < 0 &&
      cross(q2 - q1, p1 - q1) * cross(q2 - q1, p2 - q1) < 0) {
    return 0;
  } else {
    double mini = INF;
    if (dot(p2 - p1, q1 - p1) < 1e-7)
      minch(mini, len(q1 - p1));
    else if (dot(p1 - p2, q1 - p2) < 1e-7)
      minch(mini, len(q1 - p2));
    else
      minch(mini, abs(cross(p2 - p1, q1 - p1)) / len(p2 - p1));

    if (dot(p2 - p1, q2 - p1) < 1e-7)
      minch(mini, len(q2 - p1));
    else if (dot(p1 - p2, q2 - p2) < 1e-7)
      minch(mini, len(q2 - p2));
    else
      minch(mini, abs(cross(p2 - p1, q2 - p1)) / len(p2 - p1));

    if (dot(q2 - q1, p1 - q1) < 1e-7)
      minch(mini, len(p1 - q1));
    else if (dot(q1 - q2, p1 - q2) < 1e-7)
      minch(mini, len(p1 - q2));
    else
      minch(mini, abs(cross(q2 - q1, p1 - q1)) / len(q2 - q1));

    if (dot(q2 - q1, p2 - q1) < 1e-7)
      minch(mini, len(p2 - q1));
    else if (dot(q1 - q2, p2 - q2) < 1e-7)
      minch(mini, len(p2 - q2));
    else
      minch(mini, abs(cross(q2 - q1, p2 - q1)) / len(q2 - q1));

    return mini;
  }
}

int main() {
  int n;
  while (cin >> n, n) {
    PDD s, e;
    cin >> s.fi >> s.se >> e.fi >> e.se;

    double ans = INF;
    bool ok = true;
    rep(i, n) {
      PDD mi, ma;
      double h;
      cin >> mi.fi >> mi.se >> ma.fi >> ma.se >> h;
      double mini = INF;
      minch(mini, distance(s, e, mi, PDD(ma.fi, mi.se)));
      minch(mini, distance(s, e, mi, PDD(mi.fi, ma.se)));
      minch(mini, distance(s, e, ma, PDD(ma.fi, mi.se)));
      minch(mini, distance(s, e, ma, PDD(mi.fi, ma.se)));
      minch(mini, distance(s, e, mi, ma));

      if (mini < h) {
        minch(ans, mini);
      } else {
        double high = 1000, low = mini, mid;
        rep(j, 100) {
          mid = (high + low) / 2;
          if ((mid - h) * (mid - h) + mini * mini >= mid * mid)
            low = mid;
          else
            high = mid;
        }
        minch(ans, low);
      }
    }

    printf("%.10f\\n", ans);
  }

  return 0;
}

Error: TLE

Fixed submission:

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

#define fi first
#define se second
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define repr(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb(s) push_back(s)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x " = " << x << endl
#define print(x) cout << x << endl
#define maxch(x, y) x = max(x, y)
#define minch(x, y) x = min(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt(x) bitset<32>(x).count()

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<double, double> PDD;
typedef pair<P, int> PPI;
typedef pair<int, P> PIP;
typedef pair<ll, ll> PL;
typedef pair<P, ll> PPL;
typedef set<int> S;

#define INF INT_MAX / 3
#define MAX_N 1000000001

PDD operator-(const PDD &a, const PDD &b) {
  return PDD(a.fi - b.fi, a.se - b.se);
}

double len(PDD p) { return sqrt(p.fi * p.fi + p.se * p.se); }

double dot(PDD a, PDD b) { return a.fi * b.fi + a.se * b.se; }

double cross(PDD a, PDD b) { return a.fi * b.se - a.se * b.fi; }

double distance(PDD p1, PDD p2, PDD q1, PDD q2) {
  if (cross(p2 - p1, q1 - p1) * cross(p2 - p1, q2 - p1) < 0 &&
      cross(q2 - q1, p1 - q1) * cross(q2 - q1, p2 - q1) < 0) {
    return 0;
  } else {
    double mini = INF;
    if (dot(p2 - p1, q1 - p1) < 1e-7)
      minch(mini, len(q1 - p1));
    else if (dot(p1 - p2, q1 - p2) < 1e-7)
      minch(mini, len(q1 - p2));
    else
      minch(mini, abs(cross(p2 - p1, q1 - p1)) / len(p2 - p1));

    if (dot(p2 - p1, q2 - p1) < 1e-7)
      minch(mini, len(q2 - p1));
    else if (dot(p1 - p2, q2 - p2) < 1e-7)
      minch(mini, len(q2 - p2));
    else
      minch(mini, abs(cross(p2 - p1, q2 - p1)) / len(p2 - p1));

    if (dot(q2 - q1, p1 - q1) < 1e-7)
      minch(mini, len(p1 - q1));
    else if (dot(q1 - q2, p1 - q2) < 1e-7)
      minch(mini, len(p1 - q2));
    else
      minch(mini, abs(cross(q2 - q1, p1 - q1)) / len(q2 - q1));

    if (dot(q2 - q1, p2 - q1) < 1e-7)
      minch(mini, len(p2 - q1));
    else if (dot(q1 - q2, p2 - q2) < 1e-7)
      minch(mini, len(p2 - q2));
    else
      minch(mini, abs(cross(q2 - q1, p2 - q1)) / len(q2 - q1));

    return mini;
  }
}

int main() {
  int n;
  while (cin >> n, n) {
    PDD s, e;
    cin >> s.fi >> s.se >> e.fi >> e.se;

    double ans = INF;
    bool ok = true;
    rep(i, n) {
      PDD mi, ma;
      double h;
      cin >> mi.fi >> mi.se >> ma.fi >> ma.se >> h;
      double mini = INF;
      minch(mini, distance(s, e, mi, PDD(ma.fi, mi.se)));
      minch(mini, distance(s, e, mi, PDD(mi.fi, ma.se)));
      minch(mini, distance(s, e, ma, PDD(ma.fi, mi.se)));
      minch(mini, distance(s, e, ma, PDD(mi.fi, ma.se)));
      minch(mini, distance(s, e, mi, ma));
      if (mini == 0) {
        ok = false;
        break;
      } else {
        if (mini < h) {
          minch(ans, mini);
        } else {
          double high = 1000, low = mini, mid;
          rep(j, 100) {
            mid = (high + low) / 2;
            if ((mid - h) * (mid - h) + mini * mini >= mid * mid)
              low = mid;
            else
              high = mid;
          }
          minch(ans, low);
        }
      }
    }

    if (ok) {
      printf("%.10f\\n", ans);
    } else {
      printf("0");
    }
  }

  return 0;
}