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

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

#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcount

typedef long double D;
typedef complex<D> P;

#define X real()
#define Y imag()

const D eps = 1e-8;
const D inf = 1e12;
const D PI = acos(-1);

D cross(P a, P b) { return (conj(a) * b).Y; }
struct L : public vector<P> { // line and segment
  L(const P &a, const P &b) {
    push_back(a);
    push_back(b);
  }
};
P projection(L l, P p) {
  P b = l[1] - l[0], c = p - l[0];
  return l[0] + b * (c / b).X;
}
D distanceLP(L l, P p) { return abs(p - projection(l, p)); }
P crosspoint(L l, L m) {
  D A = cross(l[1] - l[0], m[1] - m[0]);
  D B = cross(l[1] - l[0], l[1] - m[0]);
  if (abs(A) < eps && abs(B) < eps)
    return m[0]; // same line
  if (abs(A) < eps)
    assert(false); // !!!PRECONDITION NOT SATISFIED!!!
  return m[0] + B / A * (m[1] - m[0]);
}

P p[3], q[3], e[3], x[3];
D r[3];
P g;
P cp;

bool ok() {
  r[0] = distanceLP(L(p[0], p[1]), x[0]);
  repl(i, 1, 3) {
    D lb = 0, ub = abs(cp - p[i]);
    rep(hoge, 80) {
      D mid = (ub + lb) / 2.0;
      x[i] = p[i] + e[i] * mid;
      r[i] = distanceLP(L(p[i], p[(i + 1) % 3]), x[i]);
      D d0i = abs(x[i] - x[0]);
      if (d0i > r[0] + r[i])
        lb = mid;
      else
        ub = mid;
    }
  }
  D d12 = abs(x[1] - x[2]);
  return d12 > r[1] + r[2];
}

int main() {
  while (1) {
    bool done = true;
    rep(i, 3) {
      int x, y;
      cin >> x >> y;
      if (x != 0 || y != 0)
        done = false;
      p[i] = P(x, y);
    }
    if (done)
      break;

    D absA = abs(p[2] - p[1]);
    D absB = abs(p[2] - p[0]);
    D absC = abs(p[1] - p[0]);
    q[0] = (p[1] * absB + p[2] * absC) / (absB + absC);
    q[1] = (p[0] * absA + p[2] * absC) / (absA + absC);
    q[2] = (p[0] * absA + p[1] * absB) / (absA + absB);
    cp = crosspoint(L(p[1], q[1]), L(p[2], q[2]));
    rep(i, 3) e[i] = (q[i] - p[i]) / abs(q[i] - p[i]);
    D lb = 0, ub = abs(cp - p[0]);
    rep(hoge, 80) {
      D mid = (lb + ub) / 2.0;
      x[0] = p[0] + e[0] * mid;
      if (ok())
        ub = mid;
      else
        lb = mid;
    }
    printf("%.5Lf %.5Lf %.5Lf\\n", r[0], r[1], r[2]);
  }
  return 0;
}


Error: TLE

Fixed submission:

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

#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcount

typedef long double D;
typedef complex<D> P;

#define X real()
#define Y imag()

const D eps = 1e-8;
const D inf = 1e12;
const D PI = acos(-1);

D cross(P a, P b) { return (conj(a) * b).Y; }
struct L : public vector<P> { // line and segment
  L(const P &a, const P &b) {
    push_back(a);
    push_back(b);
  }
};
P projection(L l, P p) {
  P b = l[1] - l[0], c = p - l[0];
  return l[0] + b * (c / b).X;
}
D distanceLP(L l, P p) { return abs(p - projection(l, p)); }
P crosspoint(L l, L m) {
  D A = cross(l[1] - l[0], m[1] - m[0]);
  D B = cross(l[1] - l[0], l[1] - m[0]);
  if (abs(A) < eps && abs(B) < eps)
    return m[0]; // same line
  if (abs(A) < eps)
    assert(false); // !!!PRECONDITION NOT SATISFIED!!!
  return m[0] + B / A * (m[1] - m[0]);
}

P p[3], q[3], e[3], x[3];
D r[3];
P g;
P cp;

bool ok() {
  r[0] = distanceLP(L(p[0], p[1]), x[0]);
  repl(i, 1, 3) {
    D lb = 0, ub = abs(cp - p[i]);
    rep(hoge, 100) {
      D mid = (ub + lb) / 2.0;
      x[i] = p[i] + e[i] * mid;
      r[i] = distanceLP(L(p[i], p[(i + 1) % 3]), x[i]);
      D d0i = abs(x[i] - x[0]);
      if (d0i > r[0] + r[i])
        lb = mid;
      else
        ub = mid;
    }
  }
  D d12 = abs(x[1] - x[2]);
  return d12 > r[1] + r[2];
}

int main() {
  while (1) {
    bool done = true;
    rep(i, 3) {
      int x, y;
      cin >> x >> y;
      if (x != 0 || y != 0)
        done = false;
      p[i] = P(x, y);
    }
    if (done)
      break;

    D absA = abs(p[2] - p[1]);
    D absB = abs(p[2] - p[0]);
    D absC = abs(p[1] - p[0]);
    q[0] = (p[1] * absB + p[2] * absC) / (absB + absC);
    q[1] = (p[0] * absA + p[2] * absC) / (absA + absC);
    q[2] = (p[0] * absA + p[1] * absB) / (absA + absB);
    cp = crosspoint(L(p[1], q[1]), L(p[2], q[2]));
    rep(i, 3) e[i] = (q[i] - p[i]) / abs(q[i] - p[i]);
    D lb = 0, ub = abs(cp - p[0]);
    rep(hoge, 100) {
      D mid = (lb + ub) / 2.0;
      x[0] = p[0] + e[0] * mid;
      if (ok())
        ub = mid;
      else
        lb = mid;
    }
    printf("%.5Lf %.5Lf %.5Lf\\n", r[0], r[1], r[2]);
  }
  return 0;
}