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

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, m, n) for (int i = m; i < n; i++)
const double EPS = 1E-10;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;
typedef complex<double> xy_t;
const int INF = 1 << 30;

double cross(xy_t a, xy_t b) { return imag(conj(a) * b); }

double dot(xy_t a, xy_t b) { return real(conj(a) * b); }

xy_t A, B, C;

double limit;
xy_t r1, r2[2];
double db, dc;

void setR(xy_t s, xy_t p0, xy_t p1, double R) {
  xy_t p = p0 - s;
  xy_t q = p1 - s;
  p /= abs(p);
  q /= abs(q);
  xy_t r = p + q;
  r /= abs(r);
  double d = abs(cross(r, p));
  r1 = s + r * (R / d);
}

double getR2(xy_t s, xy_t p0, xy_t p1, double R, int id) {
  xy_t p = p0 - s;
  xy_t q = p1 - s;
  p /= abs(p);
  q /= abs(q);
  xy_t r = p + q;
  r /= abs(r);
  double d = abs(cross(r, p));

  double ub = limit;
  double lb = 0;

  for (int i = 0; i < 100; i++) {
    double mb = (ub + lb) / 2.0;
    r2[id] = s + r * (mb / d);
    if (abs(r2[id] - r1) > mb + R)
      lb = mb;
    else
      ub = mb;
  }

  return lb;
}

bool ok(double mb) {

  setR(A, B, C, mb);
  db = getR2(B, A, C, mb, 0);
  dc = getR2(C, A, B, mb, 1);
  //  cout << (b + c) << " " << abs(r2[0] - r2[1]) << endl;
  return (db + dc) > abs(r2[0] - r2[1]);
}

int main() {
  int a, b, c, d, e, f;
  while (cin >> a >> b >> c >> d >> e >> f && (a || b || c || d || e || f)) {
    A = xy_t(a, b);
    B = xy_t(c, d);
    C = xy_t(e, f);

    double S = abs(cross(B - A, C - A)) / 2.0;
    limit = S * 2 / (abs(B - A) + abs(C - A) + abs(C - B));

    double ub = limit;
    double lb = 0;
    rep(i, 100) {
      double mb = (ub + lb) / 2.0;
      if (ok(mb))
        lb = mb;
      else
        ub = mb;
    }
    cout << fixed << setprecision(9) << lb << " " << db << " " << dc << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, m, n) for (int i = m; i < n; i++)
const double EPS = 1E-10;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;
typedef complex<double> xy_t;
const int INF = 1 << 30;

double cross(xy_t a, xy_t b) { return imag(conj(a) * b); }

double dot(xy_t a, xy_t b) { return real(conj(a) * b); }

xy_t A, B, C;

double limit;
xy_t r1, r2[2];
double db, dc;

void setR(xy_t s, xy_t p0, xy_t p1, double R) {
  xy_t p = p0 - s;
  xy_t q = p1 - s;
  p /= abs(p);
  q /= abs(q);
  xy_t r = p + q;
  r /= abs(r);
  double d = abs(cross(r, p));
  r1 = s + r * (R / d);
}

double getR2(xy_t s, xy_t p0, xy_t p1, double R, int id) {
  xy_t p = p0 - s;
  xy_t q = p1 - s;
  p /= abs(p);
  q /= abs(q);
  xy_t r = p + q;
  r /= abs(r);
  double d = abs(cross(r, p));

  double ub = limit;
  double lb = 0;

  for (int i = 0; i < 300; i++) {
    double mb = (ub + lb) / 2.0;
    r2[id] = s + r * (mb / d);
    if (abs(r2[id] - r1) > mb + R)
      lb = mb;
    else
      ub = mb;
  }

  return lb;
}

bool ok(double mb) {

  setR(A, B, C, mb);
  db = getR2(B, A, C, mb, 0);
  dc = getR2(C, A, B, mb, 1);
  //  cout << (b + c) << " " << abs(r2[0] - r2[1]) << endl;
  return (db + dc) > abs(r2[0] - r2[1]);
}

int main() {
  int a, b, c, d, e, f;
  while (cin >> a >> b >> c >> d >> e >> f && (a || b || c || d || e || f)) {
    A = xy_t(a, b);
    B = xy_t(c, d);
    C = xy_t(e, f);

    double S = abs(cross(B - A, C - A)) / 2.0;
    limit = S * 2 / (abs(B - A) + abs(C - A) + abs(C - B));

    double ub = limit;
    double lb = 0;
    rep(i, 300) {
      double mb = (ub + lb) / 2.0;
      if (ok(mb))
        lb = mb;
      else
        ub = mb;
    }
    cout << fixed << setprecision(9) << lb << " " << db << " " << dc << endl;
  }
  return 0;
}