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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

#define EPS (1e-10)
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))

typedef complex<double> P;
typedef pair<P, P> Edge;
typedef long long ll;

const double PI = 4 * atan(1.0);
const int MAX_SIZE = 10000;

// àÏ
double dot(P a, P b) { return (a.real() * b.real() + a.imag() * b.imag()); }
// OÏ
double cross(P a, P b) { return (a.real() * b.imag() - a.imag() * b.real()); }
// _cª¼üabãÉ é©È¢©
int is_point_on_line(P a, P b, P c) { return EQ(cross(b - a, c - a), 0.0); }
// 2¼üÌ¼s»è
int is_orthogonal(P a1, P a2, P b1, P b2) {
  return EQ(dot(a1 - a2, b1 - b2), 0.0);
}
// 2¼üÌ½s»è
int is_parallel(P a1, P a2, P b1, P b2) {
  return EQ(cross(a1 - a2, b1 - b2), 0.0);
}
// _a,bðÊé¼üÆ_cÌÔÌ£
double distance_l_p(P a, P b, P c) {
  return abs(cross(b - a, c - a)) / abs(b - a);
}
// _a,bð[_Æ·éüªÆ_cÆÌ£
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);
}
// a1,a2ð[_Æ·éüªÆb1,b2ð[_Æ·éüªÌð·»è
int is_intersected_ls(P a1, P a2, P b1, P b2) {
  // üªª½sÈêÍdÈÁÄ¢È¢±ÆÉ·é
  if (abs(cross(a2 - a1, b2 - b1)) < EPS) {
    return 0;
  }
  return (cross(a2 - a1, b1 - a1) * cross(a2 - a1, b2 - a1) < EPS) &&
         (cross(b2 - b1, a1 - b1) * cross(b2 - b1, a2 - b1) < EPS);
}
// a1,a2ð[_Æ·éüªÆb1,b2ð[_Æ·éüªÌð_vZ
P intersection_ls(P a1, P a2, P b1, P b2) {
  P b = b2 - b1;
  double d1 = abs(cross(b, a1 - b1));
  double d2 = abs(cross(b, a2 - b1));
  double t = d1 / (d1 + d2);

  return a1 + (a2 - a1) * t;
}
// a1,a2ðÊé¼üÆb1,b2ðÊé¼üÌð·»è
int is_intersected_l(P a1, P a2, P b1, P b2) {
  return !EQ(cross(a1 - a2, b1 - b2), 0.0);
}
// a1,a2ðÊé¼üÆb1,b2ðÊé¼üÌð_vZ
P intersection_l(P a1, P a2, P b1, P b2) {
  P a = a2 - a1;
  P b = b2 - b1;
  return a1 + a * cross(b, b1 - a1) / cross(b, a);
}
// Op`ÌàÉ_ª é©Ç¤©
// OÏÌ³ª·×Ä¯¶ÈçàÉ_ è
bool isInTriangle(P p1, P p2, P p3, P s) {
  P a = p1 - s;
  P b = p2 - s;
  P c = p3 - s;
  return ((cross(a, b) > 0 && cross(b, c) > 0 && cross(c, a) > 0) ||
          (cross(a, b) < 0 && cross(b, c) < 0 && cross(c, a) < 0));
}

// @üxNgðßé
P unitVector(P a) { return P(-a.imag(), a.real()); }
// PÊ@üxNgðßé
P unitNormalVector(P a, P b) {
  P tmp = (b - a);
  P e = P(-tmp.imag(), tmp.real());
  e /= abs(e);
  return e;
}

P ap, bp, cp;
P acp, bcp, ccp;
double ar, br, cr;

P selDot(P &ab1_a, P &ab1_b, P &ab2_a, P &ab2_b, P &ac1_a, P &ac1_b, P &ac2_a,
         P &ac2_b) {
  P inDot = P(-1, -1);
  // »ê¼êÌð_ðßAOp`ÌÉÜÜêéàÌðIÔ
  if (isInTriangle(ap, bp, cp, intersection_l(ab1_a, ab1_b, ac1_a, ac1_b)))
    inDot = intersection_l(ab1_a, ab1_b, ac1_a, ac1_b);
  else if (isInTriangle(ap, bp, cp, intersection_l(ab1_a, ab1_b, ac2_a, ac2_b)))
    inDot = intersection_l(ab1_a, ab1_b, ac2_a, ac2_b);
  else if (isInTriangle(ap, bp, cp, intersection_l(ab2_a, ab2_b, ac2_a, ac2_b)))
    inDot = intersection_l(ab2_a, ab2_b, ac2_a, ac2_b);
  else if (isInTriangle(ap, bp, cp, intersection_l(ab2_a, ab2_b, ac1_a, ac1_b)))
    inDot = intersection_l(ab2_a, ab2_b, ac1_a, ac1_b);
  return inDot;
}

// ~bÆ~cÌÖWð`FbN
bool checkB(double r) {
  P ba1_a, ba1_b, ba2_a, ba2_b;
  P bc1_a, bc1_b, bc2_a, bc2_b;

  P e1 = unitNormalVector(bp, ap);
  P e2 = unitNormalVector(bp, cp);

  ba1_a = e1 * r + bp;
  ba1_b = e1 * r + ap;
  ba2_a = -e1 * r + bp;
  ba2_b = -e1 * r + ap;
  bc1_a = e2 * r + bp;
  bc1_b = e2 * r + cp;
  bc2_a = -e2 * r + bp;
  bc2_b = -e2 * r + cp;
  P inDot = selDot(ba1_a, ba1_b, ba2_a, ba2_b, bc1_a, bc1_b, bc2_a, bc2_b);
  // ðð½·ð_ª¶ÝµÈ¢
  if (EQ(inDot.real(), -1))
    return false;
  bcp = inDot;
  br = r;
  // ~aªOp`ÌOÉÍÝoÄ¢½çA»Ì_Åfalse
  double d1 = distance_l_p(ap, bp, bcp);
  double d2 = distance_l_p(bp, cp, bcp);
  double d3 = distance_l_p(cp, ap, bcp);
  if ((!EQ(d1, br) && d1 < br) || (!EQ(d2, br) && d2 < br) ||
      (!EQ(d3, br) && d3 < br))
    return false;
  // ~aÆdÈé©`FbN
  //  dÈéÈçfalse
  if (abs(acp - bcp) < br + ar)
    return false;
  return true;
}
bool checkC(double r) {
  P ca1_a, ca1_b, ca2_a, ca2_b;
  P cb1_a, cb1_b, cb2_a, cb2_b;

  P e1 = unitNormalVector(cp, ap);
  P e2 = unitNormalVector(cp, bp);

  ca1_a = e1 * r + cp;
  ca1_b = e1 * r + ap;
  ca2_a = -e1 * r + cp;
  ca2_b = -e1 * r + ap;
  cb1_a = e2 * r + cp;
  cb1_b = e2 * r + bp;
  cb2_a = -e2 * r + cp;
  cb2_b = -e2 * r + bp;
  P inDot = selDot(ca1_a, ca1_b, ca2_a, ca2_b, cb1_a, cb1_b, cb2_a, cb2_b);
  // ðð½·ð_ª¶ÝµÈ¢
  if (EQ(inDot.real(), -1))
    return false;
  ccp = inDot;
  cr = r;
  double d1 = distance_l_p(ap, bp, ccp);
  double d2 = distance_l_p(bp, cp, ccp);
  double d3 = distance_l_p(cp, ap, ccp);
  if ((!EQ(d1, cr) && d1 < cr) || (!EQ(d2, cr) && d2 < cr) ||
      (!EQ(d3, cr) && d3 < cr))
    return false;
  // ~aÆdÈé©`FbN
  //  dÈéÈçfalse
  if (abs(ccp - acp) < cr + ar)
    return false;
  return true;
}

// _AÉß¢~ð¼arÅu¯é©`FbN
bool check(double r) {
  P ab1_a, ab1_b;
  P ab2_a, ab2_b;

  P ac1_a, ac1_b;
  P ac2_b, ac2_a;

  P e1 = unitNormalVector(ap, bp);
  P e2 = unitNormalVector(ap, cp);

  ab1_a = e1 * r + ap;
  ab1_b = e1 * r + bp;
  ab2_a = -e1 * r + ap;
  ab2_b = -e1 * r + bp;
  ac1_a = e2 * r + ap;
  ac1_b = e2 * r + cp;
  ac2_a = -e2 * r + ap;
  ac2_b = -e2 * r + cp;

  P inDot = selDot(ab1_a, ab1_b, ab2_a, ab2_b, ac1_a, ac1_b, ac2_a, ac2_b);
  // ðð½·ð_ª¶ÝµÈ¢
  if (EQ(inDot.real(), -1))
    return false;
  acp = inDot;
  ar = r;
  // ~aªOp`ÌOÉÍÝoÄ¢½çA»Ì_Åfalse
  double d1 = distance_l_p(ap, bp, acp);
  double d2 = distance_l_p(ap, cp, acp);
  double d3 = distance_l_p(bp, cp, acp);
  if ((!EQ(d1, ar) && d1 < ar) || (!EQ(d2, ar) && d2 < ar) ||
      (!EQ(d3, ar) && d3 < ar))
    return false;
  // _BÉß¢~Ì¼aÆS_ðßé
  double ub = 1000;
  double lb = 0;
  int loop = 50;
  while (loop--) {
    double mid = (ub + lb) / 2;
    if (checkB(mid))
      lb = mid;
    else
      ub = mid;
  }
  br = ub;
  ub = 1000;
  lb = 0;
  loop = 50;
  while (loop--) {
    double mid = (ub + lb) / 2;
    if (checkC(mid))
      lb = mid;
    else
      ub = mid;
  }
  cr = ub;
  // àµbÆcÌ~ª­ÁÂ©È¢ÈçAfalse
  if (abs(bcp - ccp) > cr + br)
    return false;
  return true;
}

int main() {

  int x1, x2, x3, y1, y2, y3;
  while (cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 &&
         !(x1 == 0 && y1 == 0 && x2 == 0 && y2 == 0 && x3 == 0 && y3 == 0)) {
    ap = P(x1, y1);
    bp = P(x2, y2);
    cp = P(x3, y3);
    double ub = 1000;
    double lb = 0;
    int loop = 50;
    P acp;
    while (loop--) {
      double mid = (ub + lb) / 2;
      if (check(mid))
        lb = mid;
      else
        ub = mid;
    }
    printf("%.10f %.10f %.10f\\n", ar, br, cr);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

#define EPS (1e-10)
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))

typedef complex<double> P;
typedef pair<P, P> Edge;
typedef long long ll;

const double PI = 4 * atan(1.0);
const int MAX_SIZE = 10000;

// àÏ
double dot(P a, P b) { return (a.real() * b.real() + a.imag() * b.imag()); }
// OÏ
double cross(P a, P b) { return (a.real() * b.imag() - a.imag() * b.real()); }
// _cª¼üabãÉ é©È¢©
int is_point_on_line(P a, P b, P c) { return EQ(cross(b - a, c - a), 0.0); }
// 2¼üÌ¼s»è
int is_orthogonal(P a1, P a2, P b1, P b2) {
  return EQ(dot(a1 - a2, b1 - b2), 0.0);
}
// 2¼üÌ½s»è
int is_parallel(P a1, P a2, P b1, P b2) {
  return EQ(cross(a1 - a2, b1 - b2), 0.0);
}
// _a,bðÊé¼üÆ_cÌÔÌ£
double distance_l_p(P a, P b, P c) {
  return abs(cross(b - a, c - a)) / abs(b - a);
}
// _a,bð[_Æ·éüªÆ_cÆÌ£
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);
}
// a1,a2ð[_Æ·éüªÆb1,b2ð[_Æ·éüªÌð·»è
int is_intersected_ls(P a1, P a2, P b1, P b2) {
  // üªª½sÈêÍdÈÁÄ¢È¢±ÆÉ·é
  if (abs(cross(a2 - a1, b2 - b1)) < EPS) {
    return 0;
  }
  return (cross(a2 - a1, b1 - a1) * cross(a2 - a1, b2 - a1) < EPS) &&
         (cross(b2 - b1, a1 - b1) * cross(b2 - b1, a2 - b1) < EPS);
}
// a1,a2ð[_Æ·éüªÆb1,b2ð[_Æ·éüªÌð_vZ
P intersection_ls(P a1, P a2, P b1, P b2) {
  P b = b2 - b1;
  double d1 = abs(cross(b, a1 - b1));
  double d2 = abs(cross(b, a2 - b1));
  double t = d1 / (d1 + d2);

  return a1 + (a2 - a1) * t;
}
// a1,a2ðÊé¼üÆb1,b2ðÊé¼üÌð·»è
int is_intersected_l(P a1, P a2, P b1, P b2) {
  return !EQ(cross(a1 - a2, b1 - b2), 0.0);
}
// a1,a2ðÊé¼üÆb1,b2ðÊé¼üÌð_vZ
P intersection_l(P a1, P a2, P b1, P b2) {
  P a = a2 - a1;
  P b = b2 - b1;
  return a1 + a * cross(b, b1 - a1) / cross(b, a);
}
// Op`ÌàÉ_ª é©Ç¤©
// OÏÌ³ª·×Ä¯¶ÈçàÉ_ è
bool isInTriangle(P p1, P p2, P p3, P s) {
  P a = p1 - s;
  P b = p2 - s;
  P c = p3 - s;
  return ((cross(a, b) > 0 && cross(b, c) > 0 && cross(c, a) > 0) ||
          (cross(a, b) < 0 && cross(b, c) < 0 && cross(c, a) < 0));
}

// @üxNgðßé
P unitVector(P a) { return P(-a.imag(), a.real()); }
// PÊ@üxNgðßé
P unitNormalVector(P a, P b) {
  P tmp = (b - a);
  P e = P(-tmp.imag(), tmp.real());
  e /= abs(e);
  return e;
}

P ap, bp, cp;
P acp, bcp, ccp;
double ar, br, cr;

P selDot(P &ab1_a, P &ab1_b, P &ab2_a, P &ab2_b, P &ac1_a, P &ac1_b, P &ac2_a,
         P &ac2_b) {
  P inDot = P(-1, -1);
  // »ê¼êÌð_ðßAOp`ÌÉÜÜêéàÌðIÔ
  if (isInTriangle(ap, bp, cp, intersection_l(ab1_a, ab1_b, ac1_a, ac1_b)))
    inDot = intersection_l(ab1_a, ab1_b, ac1_a, ac1_b);
  else if (isInTriangle(ap, bp, cp, intersection_l(ab1_a, ab1_b, ac2_a, ac2_b)))
    inDot = intersection_l(ab1_a, ab1_b, ac2_a, ac2_b);
  else if (isInTriangle(ap, bp, cp, intersection_l(ab2_a, ab2_b, ac2_a, ac2_b)))
    inDot = intersection_l(ab2_a, ab2_b, ac2_a, ac2_b);
  else if (isInTriangle(ap, bp, cp, intersection_l(ab2_a, ab2_b, ac1_a, ac1_b)))
    inDot = intersection_l(ab2_a, ab2_b, ac1_a, ac1_b);
  return inDot;
}

// ~bÆ~cÌÖWð`FbN
bool checkB(double r) {
  P ba1_a, ba1_b, ba2_a, ba2_b;
  P bc1_a, bc1_b, bc2_a, bc2_b;

  P e1 = unitNormalVector(bp, ap);
  P e2 = unitNormalVector(bp, cp);

  ba1_a = e1 * r + bp;
  ba1_b = e1 * r + ap;
  ba2_a = -e1 * r + bp;
  ba2_b = -e1 * r + ap;
  bc1_a = e2 * r + bp;
  bc1_b = e2 * r + cp;
  bc2_a = -e2 * r + bp;
  bc2_b = -e2 * r + cp;
  P inDot = selDot(ba1_a, ba1_b, ba2_a, ba2_b, bc1_a, bc1_b, bc2_a, bc2_b);
  // ðð½·ð_ª¶ÝµÈ¢
  if (EQ(inDot.real(), -1))
    return false;
  bcp = inDot;
  br = r;
  // ~aªOp`ÌOÉÍÝoÄ¢½çA»Ì_Åfalse
  double d1 = distance_l_p(ap, bp, bcp);
  double d2 = distance_l_p(bp, cp, bcp);
  double d3 = distance_l_p(cp, ap, bcp);
  if ((!EQ(d1, br) && d1 < br) || (!EQ(d2, br) && d2 < br) ||
      (!EQ(d3, br) && d3 < br))
    return false;
  // ~aÆdÈé©`FbN
  //  dÈéÈçfalse
  if (abs(acp - bcp) < br + ar)
    return false;
  return true;
}
bool checkC(double r) {
  P ca1_a, ca1_b, ca2_a, ca2_b;
  P cb1_a, cb1_b, cb2_a, cb2_b;

  P e1 = unitNormalVector(cp, ap);
  P e2 = unitNormalVector(cp, bp);

  ca1_a = e1 * r + cp;
  ca1_b = e1 * r + ap;
  ca2_a = -e1 * r + cp;
  ca2_b = -e1 * r + ap;
  cb1_a = e2 * r + cp;
  cb1_b = e2 * r + bp;
  cb2_a = -e2 * r + cp;
  cb2_b = -e2 * r + bp;
  P inDot = selDot(ca1_a, ca1_b, ca2_a, ca2_b, cb1_a, cb1_b, cb2_a, cb2_b);
  // ðð½·ð_ª¶ÝµÈ¢
  if (EQ(inDot.real(), -1))
    return false;
  ccp = inDot;
  cr = r;
  double d1 = distance_l_p(ap, bp, ccp);
  double d2 = distance_l_p(bp, cp, ccp);
  double d3 = distance_l_p(cp, ap, ccp);
  if ((!EQ(d1, cr) && d1 < cr) || (!EQ(d2, cr) && d2 < cr) ||
      (!EQ(d3, cr) && d3 < cr))
    return false;
  // ~aÆdÈé©`FbN
  //  dÈéÈçfalse
  if (abs(ccp - acp) < cr + ar)
    return false;
  return true;
}

// _AÉß¢~ð¼arÅu¯é©`FbN
bool check(double r) {
  P ab1_a, ab1_b;
  P ab2_a, ab2_b;

  P ac1_a, ac1_b;
  P ac2_b, ac2_a;

  P e1 = unitNormalVector(ap, bp);
  P e2 = unitNormalVector(ap, cp);

  ab1_a = e1 * r + ap;
  ab1_b = e1 * r + bp;
  ab2_a = -e1 * r + ap;
  ab2_b = -e1 * r + bp;
  ac1_a = e2 * r + ap;
  ac1_b = e2 * r + cp;
  ac2_a = -e2 * r + ap;
  ac2_b = -e2 * r + cp;

  P inDot = selDot(ab1_a, ab1_b, ab2_a, ab2_b, ac1_a, ac1_b, ac2_a, ac2_b);
  // ðð½·ð_ª¶ÝµÈ¢
  if (EQ(inDot.real(), -1))
    return false;
  acp = inDot;
  ar = r;
  // ~aªOp`ÌOÉÍÝoÄ¢½çA»Ì_Åfalse
  double d1 = distance_l_p(ap, bp, acp);
  double d2 = distance_l_p(ap, cp, acp);
  double d3 = distance_l_p(bp, cp, acp);
  if ((!EQ(d1, ar) && d1 < ar) || (!EQ(d2, ar) && d2 < ar) ||
      (!EQ(d3, ar) && d3 < ar))
    return false;
  // _BÉß¢~Ì¼aÆS_ðßé
  double ub = 10000000;
  double lb = 0;
  int loop = 100;
  while (loop--) {
    double mid = (ub + lb) / 2;
    if (checkB(mid))
      lb = mid;
    else
      ub = mid;
  }
  br = ub;
  ub = 10000000;
  lb = 0;
  loop = 100;
  while (loop--) {
    double mid = (ub + lb) / 2;
    if (checkC(mid))
      lb = mid;
    else
      ub = mid;
  }
  cr = ub;
  // àµbÆcÌ~ª­ÁÂ©È¢ÈçAfalse
  if (abs(bcp - ccp) > cr + br)
    return false;
  return true;
}

int main() {

  int x1, x2, x3, y1, y2, y3;
  while (cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 &&
         !(x1 == 0 && y1 == 0 && x2 == 0 && y2 == 0 && x3 == 0 && y3 == 0)) {
    ap = P(x1, y1);
    bp = P(x2, y2);
    cp = P(x3, y3);
    double ub = 10000000;
    double lb = 0;
    int loop = 100;
    P acp;
    while (loop--) {
      double mid = (ub + lb) / 2;
      if (check(mid))
        lb = mid;
      else
        ub = mid;
    }
    printf("%.10f %.10f %.10f\\n", ar, br, cr);
  }
  return 0;
}