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

#include <cmath>
#include <complex>
#include <iostream>
#include <set>
#include <stack>
#include <vector>

enum CCW { FRONT = 0x01, RIGHT = 0x02, BACK = 0x04, LEFT = 0x08, OVER = 0x10 };
enum CIRCLE_RELATION {
  CIRCLE_SAME = 0x01,
  CIRCLE_CONTAIN = 0x02,
  CIRCLE_NO_CROSS = 0x04,
  CIRCLE_ONE_CROSS = 0x08,
  CIRCLE_ONE_INSIDE_CROSS = 0x10,
  CIRCLE_TWO_CROSS = 0x20
};
#define sc second
#define fr first

#define REP(i, n) for (int i = 0; i < (int)(n); ++i)

using namespace std;

typedef double elem;
typedef complex<elem> point, vec;
typedef pair<point, point> line, hline, seg, pp;

const double infty = 1e40;
const double eps = 1.0e-10;
const double pi = acos(-1.0);
point base(0, 0);

// oÍ
ostream &operator<<(ostream &os, const pair<point, point> &p) {
  os << p.fr << "-" << p.sc;
  return os;
}

// lZ
inline elem sq(elem a) { return a * a; }
inline elem cq(elem a) { return a * a * a; }

// pxÏ·
elem rad(elem deg) { return (deg / 180) * pi; }
elem deg(elem rad) { return (rad * 180) / pi; }

// ®¬_Ìô¢AÈÇ
bool eq(elem a, elem b) { return abs(a - b) < eps; }
bool eqv(vec a, vec b) { return eq(abs(b - a), 0); }

// _Iy[^
bool far(point a, point b) { return abs(b - a) > eps; }
bool near(point a, point b) { return abs(b - a) <= eps; }
elem dot(vec a, vec b) { return (a.real() * b.real() + a.imag() * b.imag()); }
elem cross(vec a, vec b) { return (a.real() * b.imag() - a.imag() * b.real()); }

// a©çbÜÅvñèÌpxAàpA]ñ]
elem varg(vec a, vec b) {
  elem ret = arg(a) - arg(b);
  if (ret < 0)
    ret += 2 * pi;
  if (ret > 2 * pi)
    ret -= 2 * pi;
  if (eq(ret, 2 * pi))
    ret = 0;
  return ret;
}
elem varg2(vec a, vec b) {
  elem ret = varg(a, b);
  if (ret > pi)
    return 2 * pi - ret;
  return ret;
}
elem arg(vec a, vec b) { return acos(dot(a, b) / (abs(a) * abs(b))); }
point rot(point p, elem theta) { return p * polar((elem)1.0, theta); }
point rotdeg(point p, elem deg) { return p * polar((elem)1.0, rad(deg)); }
point proj(line l, point p) {
  double t = dot(p - l.first, l.first - l.second) / abs(l.first - l.second);
  return l.first + t * (l.first - l.second);
}
point reflect(line l, point p) { return p + 2.0 * (proj(l, p) - p); }

// ñ_Ô£A¼üÆ_ÌÅZ£AüªÆ_ÌÅZ£
elem dist(point a, point b) { return abs(a - b); }
elem dist_l(line l, point x) {
  return abs(cross(l.sc - l.fr, x - l.fr)) / abs(l.sc - l.fr);
}
elem dist_seg(seg s, point x) {
  if (dot(s.sc - s.fr, x - s.fr) < 0)
    return abs(x - s.fr);
  if (dot(s.fr - s.sc, x - s.sc) < 0)
    return abs(x - s.sc);
  return dist_l(s, x);
}

// PÊxNgA@üxNgAPÊ@üxNg
vec uvec(vec a) { return a / abs(a); }
vec nmr(vec a) { return a * vec(0, -1); }
vec nml(vec a) { return a * vec(0, 1); }
vec unmr(vec a) { return uvec(nmr(a)); }
vec unml(vec a) { return uvec(nml(a)); }

// ¼ðA½s»è
bool orth(point a1, point a2, point b1, point b2) {
  return eq(dot(a2 - a1, b2 - b1), 0);
}
bool orth(vec v1, vec v2) { return eq(dot(v1, v2), 0); }
bool prll(point a1, point a2, point b1, point b2) {
  return eq(cross(a2 - a1, b2 - b1), 0);
}
bool prll(vec v1, vec v2) { return eq(cross(v1, v2), 0); }

// CCW oXg¾ªA¸xÉæé
inline int ccw(const point &a, point b, point x) {
  b -= a;
  x -= a;
  if (eq(cross(b, x), 0.0) && dot(b, x) < 0)
    return BACK;
  if (eq(cross(b, x), 0.0) && abs(b) < abs(x))
    return FRONT;
  if (eq(cross(b, x), 0.0))
    return OVER;
  if (cross(b, x) > 0)
    return LEFT;
  if (cross(b, x) < 0)
    return RIGHT;
}

// üªg£
line expandLine(line l, elem mag) {
  line ret = l;
  vec vf(l.first - l.second);
  vec vs(l.second - l.first);
  ret.first = l.second + mag * vf;
  ret.second = l.first + mag * vs;
  return ret;
}

// üªÌð·»è
inline bool intersectedSS(const seg &a, const seg &b) {
  int cwaf = ccw(a.fr, a.sc, b.fr);
  int cwbf = ccw(b.fr, b.sc, a.fr);
  int cwas = ccw(a.fr, a.sc, b.sc);
  int cwbs = ccw(b.fr, b.sc, a.sc);
  if (cwaf == OVER || cwas == OVER || cwbf == OVER || cwbs == OVER)
    return true;
  return (cwaf | cwas) == (LEFT | RIGHT) && (cwbf | cwbs) == (LEFT | RIGHT);
}

// ¼üÌð·»è
bool intersectedLL(line a, line b) {
  return !eq(cross(a.sc - a.fr, b.sc - b.fr), 0.0);
}

// ð_vZ
point intersectionSS(seg a, seg b) {
  elem d1 = dist_l(b, a.fr);
  elem d2 = dist_l(b, a.sc);
  return a.fr + (d1 / (d1 + d2)) * (a.sc - a.fr);
}
point intersectionLL(line a, line b) {
  vec va = a.sc - a.fr;
  vec vb = b.sc - b.fr;
  return a.fr + va * (cross(vb, b.fr - a.fr) / cross(vb, va));
}

// üªð_êÅ
bool intersectionLL(line a, line b, point &ret) {
  return intersectedLL(a, b) ? ret = intersectionLL(a, b), true : false;
}
bool intersectionLH(line a, hline b, point &ret) {
  point tmp;
  return intersectionLL(a, b, tmp)
             ? (ccw(b.fr, b.sc, tmp) & (OVER | FRONT) ? ret = tmp, true : false)
             : false;
}
bool intersectionLS(line l, seg s, point &ret) {
  point tmp;
  return intersectionLL(l, s, tmp)
             ? (ccw(s.fr, s.sc, tmp) & OVER ? ret = tmp, true : false)
             : false;
}
bool intersectionHH(hline a, hline b, point &ret) {
  point tmp;
  return intersectionLL(a, b, tmp) ? (ccw(a.fr, a.sc, tmp) & (OVER | FRONT) &&
                                          ccw(b.fr, b.sc, tmp) & (OVER | FRONT)
                                      ? ret = tmp,
                                      true : false)
                                   : false;
}
bool intersectionHS(hline a, seg s, point &ret) {
  point tmp;
  return intersectionLS(a, s, tmp)
             ? (ccw(a.fr, a.sc, tmp) & (OVER | FRONT) ? ret = tmp, true : false)
             : false;
}
bool intersectionSS(seg a, seg b, point &ret) {
  return intersectedSS(a, b) ? ret = intersectionSS(a, b), true : false;
}

void make_bisectors(int t, const vector<line> &vl, vector<line> &ret) {
  for (int i = 0; i < (int)vl.size(); ++i) {
    if (i != t) {
      point is;
      if (intersectionLL(vl[t], vl[i], is)) {
        vec va;
        vec vb;

        if (far(is, vl[t].fr)) {
          va = uvec(vl[t].fr - is);
        } else {
          va = uvec(vl[t].sc - is);
        }

        if (far(is, vl[i].fr)) {
          vb = uvec(vl[i].fr - is);
        } else {
          vb = uvec(vl[i].sc - is);
        }

        ret.push_back(line(is, is + va + vb));
        ret.push_back(line(is, is + va - vb));

      } else {
        elem d = 0.5 * dist_l(vl[i], vl[t].fr);
        point is;
        intersectionLL(line(vl[t].fr, vl[t].fr + unmr(vl[t].sc - vl[t].fr)),
                       vl[i], is);

        point cfr = d * uvec(is - vl[t].fr) + vl[t].fr;
        point csc = d * uvec(is - vl[t].fr) + vl[t].sc;
        ret.push_back(line(cfr, csc));
      }
    }
  }
  return;
}
const double MAG = 800;

int main() {
  while (true) {
    int n;
    scanf("%d", &n);
    if (n == 0)
      break;

    vector<line> vl;
    for (int i = 0; i < n; ++i) {
      elem x1, y1, x2, y2;
      scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
      x1 /= MAG;
      y1 /= MAG;
      x2 /= MAG;
      y2 /= MAG;
      point a(x1, y1);
      point b(x2, y2);
      vl.push_back(line(a, b));
    }

    if (n <= 2) {
      puts("Many");
      continue;
    }

    vector<line> bisector1;
    vector<line> bisector2;
    vector<point> res;

    make_bisectors(0, vl, bisector1);
    make_bisectors(1, vl, bisector2);

    for (int i = 0; i < (int)bisector1.size(); ++i) {
      for (int j = 0; j < (int)bisector2.size(); ++j) {
        point is;

        if (prll(bisector1[i].sc - bisector1[i].fr,
                 bisector2[j].sc - bisector2[j].fr))
          continue;
        //(ccw(bisector1[i].fr,bisector1[i].sc,bisector2[j].fr) &
        //(OVER|FRONT|BACK)) &&
        //(ccw(bisector1[i].fr,bisector1[i].sc,bisector2[j].sc) &
        //(OVER|FRONT|BACK)) ) continue;

        if (intersectionLL(bisector1[i], bisector2[j], is)) {
          elem d = dist_l(vl[0], is);
          bool ng = false;
          for (int k = 1; k < n; ++k) {
            if (!eq(d, dist_l(vl[k], is))) {
              ng = true;
              break;
            }
          }
          if (!ng) {
            res.push_back(is);
          }
        }
      }
      if (i == 1)
        break;
    }

    for (int i = 0; i < (int)res.size(); ++i) {
      for (int j = i + 1; j < (int)res.size(); ++j) {
        if (near(res[i], res[j])) {
          res.erase(res.begin() + j);
          i = -1;
          break;
        }
      }
    }

    if (res.size() > 1) {
      puts("Many");
    } else {
      if (res.size() == 1) {
        printf("%.12lf %.12lf\\n", MAG * res[0].real(), MAG * res[0].imag());
      } else {
        puts("None");
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <complex>
#include <iostream>
#include <set>
#include <stack>
#include <vector>

enum CCW { FRONT = 0x01, RIGHT = 0x02, BACK = 0x04, LEFT = 0x08, OVER = 0x10 };
enum CIRCLE_RELATION {
  CIRCLE_SAME = 0x01,
  CIRCLE_CONTAIN = 0x02,
  CIRCLE_NO_CROSS = 0x04,
  CIRCLE_ONE_CROSS = 0x08,
  CIRCLE_ONE_INSIDE_CROSS = 0x10,
  CIRCLE_TWO_CROSS = 0x20
};
#define sc second
#define fr first

#define REP(i, n) for (int i = 0; i < (int)(n); ++i)

using namespace std;

typedef double elem;
typedef complex<elem> point, vec;
typedef pair<point, point> line, hline, seg, pp;

const double infty = 1e40;
const double eps = 1.0e-1;
const double pi = acos(-1.0);
point base(0, 0);

// oÍ
ostream &operator<<(ostream &os, const pair<point, point> &p) {
  os << p.fr << "-" << p.sc;
  return os;
}

// lZ
inline elem sq(elem a) { return a * a; }
inline elem cq(elem a) { return a * a * a; }

// pxÏ·
elem rad(elem deg) { return (deg / 180) * pi; }
elem deg(elem rad) { return (rad * 180) / pi; }

// ®¬_Ìô¢AÈÇ
bool eq(elem a, elem b) { return abs(a - b) < eps; }
bool eqv(vec a, vec b) { return eq(abs(b - a), 0); }

// _Iy[^
bool far(point a, point b) { return abs(b - a) > eps; }
bool near(point a, point b) { return abs(b - a) <= eps; }
elem dot(vec a, vec b) { return (a.real() * b.real() + a.imag() * b.imag()); }
elem cross(vec a, vec b) { return (a.real() * b.imag() - a.imag() * b.real()); }

// a©çbÜÅvñèÌpxAàpA]ñ]
elem varg(vec a, vec b) {
  elem ret = arg(a) - arg(b);
  if (ret < 0)
    ret += 2 * pi;
  if (ret > 2 * pi)
    ret -= 2 * pi;
  if (eq(ret, 2 * pi))
    ret = 0;
  return ret;
}
elem varg2(vec a, vec b) {
  elem ret = varg(a, b);
  if (ret > pi)
    return 2 * pi - ret;
  return ret;
}
elem arg(vec a, vec b) { return acos(dot(a, b) / (abs(a) * abs(b))); }
point rot(point p, elem theta) { return p * polar((elem)1.0, theta); }
point rotdeg(point p, elem deg) { return p * polar((elem)1.0, rad(deg)); }
point proj(line l, point p) {
  double t = dot(p - l.first, l.first - l.second) / abs(l.first - l.second);
  return l.first + t * (l.first - l.second);
}
point reflect(line l, point p) { return p + 2.0 * (proj(l, p) - p); }

// ñ_Ô£A¼üÆ_ÌÅZ£AüªÆ_ÌÅZ£
elem dist(point a, point b) { return abs(a - b); }
elem dist_l(line l, point x) {
  return abs(cross(l.sc - l.fr, x - l.fr)) / abs(l.sc - l.fr);
}
elem dist_seg(seg s, point x) {
  if (dot(s.sc - s.fr, x - s.fr) < 0)
    return abs(x - s.fr);
  if (dot(s.fr - s.sc, x - s.sc) < 0)
    return abs(x - s.sc);
  return dist_l(s, x);
}

// PÊxNgA@üxNgAPÊ@üxNg
vec uvec(vec a) { return a / abs(a); }
vec nmr(vec a) { return a * vec(0, -1); }
vec nml(vec a) { return a * vec(0, 1); }
vec unmr(vec a) { return uvec(nmr(a)); }
vec unml(vec a) { return uvec(nml(a)); }

// ¼ðA½s»è
bool orth(point a1, point a2, point b1, point b2) {
  return eq(dot(a2 - a1, b2 - b1), 0);
}
bool orth(vec v1, vec v2) { return eq(dot(v1, v2), 0); }
bool prll(point a1, point a2, point b1, point b2) {
  return eq(cross(a2 - a1, b2 - b1), 0);
}
bool prll(vec v1, vec v2) { return eq(cross(v1, v2), 0); }

// CCW oXg¾ªA¸xÉæé
inline int ccw(const point &a, point b, point x) {
  b -= a;
  x -= a;
  if (eq(cross(b, x), 0.0) && dot(b, x) < 0)
    return BACK;
  if (eq(cross(b, x), 0.0) && abs(b) < abs(x))
    return FRONT;
  if (eq(cross(b, x), 0.0))
    return OVER;
  if (cross(b, x) > 0)
    return LEFT;
  if (cross(b, x) < 0)
    return RIGHT;
}

// üªg£
line expandLine(line l, elem mag) {
  line ret = l;
  vec vf(l.first - l.second);
  vec vs(l.second - l.first);
  ret.first = l.second + mag * vf;
  ret.second = l.first + mag * vs;
  return ret;
}

// üªÌð·»è
inline bool intersectedSS(const seg &a, const seg &b) {
  int cwaf = ccw(a.fr, a.sc, b.fr);
  int cwbf = ccw(b.fr, b.sc, a.fr);
  int cwas = ccw(a.fr, a.sc, b.sc);
  int cwbs = ccw(b.fr, b.sc, a.sc);
  if (cwaf == OVER || cwas == OVER || cwbf == OVER || cwbs == OVER)
    return true;
  return (cwaf | cwas) == (LEFT | RIGHT) && (cwbf | cwbs) == (LEFT | RIGHT);
}

// ¼üÌð·»è
bool intersectedLL(line a, line b) {
  return !eq(cross(a.sc - a.fr, b.sc - b.fr), 0.0);
}

// ð_vZ
point intersectionSS(seg a, seg b) {
  elem d1 = dist_l(b, a.fr);
  elem d2 = dist_l(b, a.sc);
  return a.fr + (d1 / (d1 + d2)) * (a.sc - a.fr);
}
point intersectionLL(line a, line b) {
  vec va = a.sc - a.fr;
  vec vb = b.sc - b.fr;
  return a.fr + va * (cross(vb, b.fr - a.fr) / cross(vb, va));
}

// üªð_êÅ
bool intersectionLL(line a, line b, point &ret) {
  return intersectedLL(a, b) ? ret = intersectionLL(a, b), true : false;
}
bool intersectionLH(line a, hline b, point &ret) {
  point tmp;
  return intersectionLL(a, b, tmp)
             ? (ccw(b.fr, b.sc, tmp) & (OVER | FRONT) ? ret = tmp, true : false)
             : false;
}
bool intersectionLS(line l, seg s, point &ret) {
  point tmp;
  return intersectionLL(l, s, tmp)
             ? (ccw(s.fr, s.sc, tmp) & OVER ? ret = tmp, true : false)
             : false;
}
bool intersectionHH(hline a, hline b, point &ret) {
  point tmp;
  return intersectionLL(a, b, tmp) ? (ccw(a.fr, a.sc, tmp) & (OVER | FRONT) &&
                                          ccw(b.fr, b.sc, tmp) & (OVER | FRONT)
                                      ? ret = tmp,
                                      true : false)
                                   : false;
}
bool intersectionHS(hline a, seg s, point &ret) {
  point tmp;
  return intersectionLS(a, s, tmp)
             ? (ccw(a.fr, a.sc, tmp) & (OVER | FRONT) ? ret = tmp, true : false)
             : false;
}
bool intersectionSS(seg a, seg b, point &ret) {
  return intersectedSS(a, b) ? ret = intersectionSS(a, b), true : false;
}

int main() {
  while (true) {
    int n;
    scanf("%d", &n);
    if (n == 0)
      break;

    vector<line> vl;
    for (int i = 0; i < n; ++i) {
      elem x1, y1, x2, y2;
      scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
      point a(x1, y1);
      point b(x2, y2);
      vl.push_back(line(a, b));
    }
    vector<line> bisector;
    vector<point> res;

    for (int i = 0; i < n; ++i) {
      for (int j = i + 1; j < n; ++j) {
        point is;
        if (intersectionLL(vl[i], vl[j], is)) {
          vec va;
          vec vb;

          if (far(is, vl[i].fr)) {
            va = uvec(vl[i].fr - is);
          } else {
            va = uvec(vl[i].sc - is);
          }

          if (far(is, vl[j].fr)) {
            vb = uvec(vl[j].fr - is);
          } else {
            vb = uvec(vl[j].sc - is);
          }

          bisector.push_back(line(is, is + va + vb));
          bisector.push_back(line(is, is + va - vb));

        } else {
          elem d = 0.5 * dist_l(vl[j], vl[i].fr);
          point is;
          intersectionLL(line(vl[i].fr, vl[i].fr + unmr(vl[i].sc - vl[i].fr)),
                         vl[j], is);

          point cfr = d * uvec(is - vl[i].fr) + vl[i].fr;
          point csc = d * uvec(is - vl[i].fr) + vl[i].sc;
          bisector.push_back(line(cfr, csc));
        }
      }
    }

    for (int i = 0; i < (int)bisector.size(); ++i) {
      // cout << bisector[i] << endl;
      for (int j = i + 1; j < (int)bisector.size(); ++j) {
        point is;
        if (intersectionLL(bisector[i], bisector[j], is)) {
          elem d = dist_l(vl[0], is);
          bool ng = false;
          for (int k = 1; k < n; ++k) {
            if (!eq(d, dist_l(vl[k], is))) {
              ng = true;
              break;
            }
          }
          if (!ng) {
            res.push_back(is);
          }
        }
      }
    }

    for (int i = 0; i < (int)res.size(); ++i) {
      for (int j = i + 1; j < (int)res.size(); ++j) {
        if (near(res[i], res[j])) {
          res.erase(res.begin() + j);
          i = -1;
          break;
        }
      }
    }

    // cout << res.size() << endl;
    if (res.size() > 1 || n <= 2) {
      puts("Many");
    } else {
      if (res.size() == 1) {
        printf("%.12lf %.12lf\\n", res[0].real(), res[0].imag());
      } else {
        puts("None");
      }
    }
  }
  return 0;
}