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

#include <complex>
#include <iostream>
#include <vector>

using namespace std;

typedef complex<double> P;
typedef pair<P, P> L;
const double EPS = 1e-5;
double cross(P a, P b) { return imag(conj(a) * b); }

bool lineCross(L l, L m) {
  return abs(imag((l.first - l.second) / (m.first - m.second))) < EPS;
}

P ssCrosspoint(L a, L b) {
  double A = cross(a.second - a.first, b.second - b.first);
  double B = cross(a.second - a.first, a.second - b.first);
  return b.first + B / A * (b.second - b.first);
}

vector<L> distLine(L l, L m) {
  vector<L> res;
  if (lineCross(l, m)) {
    P a = (l.first + m.first) / 2.0;
    P b = a + (l.second - l.first) / abs(l.second - l.first);
    res.push_back(make_pair(a, b));
  } else {
    P a = ssCrosspoint(l, m);
    if (abs(l.first - a) < EPS)
      swap(l.first, l.second);
    P dirA = (l.first - a) / abs(l.first - a);
    if (abs(m.first - a) < EPS)
      swap(m.first, m.second);
    P dirB = (m.first - a) / abs(m.first - a);
    P b = a + (dirA + dirB) / abs(dirA + dirB);
    res.push_back(make_pair(a, b));
    res.push_back(make_pair(a, (b - a) * P(0, 1) + a));
  }
  return res;
}

bool onLine(L a, P p) {
  return abs(imag((p - a.first) / (a.second - a.first))) < EPS;
}

int main() {
  int n;
  while (cin >> n, n) {
    vector<pair<P, P>> vp;
    for (int i = 0; i < n; i++) {
      double a, b, c, d;
      cin >> a >> b >> c >> d;
      { vp.push_back(make_pair(P(a, b), P(c, d))); }
    }

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

    vector<P> vpt;
    vector<L> vlA = distLine(vp[0], vp[1]), vlB = distLine(vp[0], vp[2]);
    for (int i = 0; i < vlA.size(); i++) {
      for (int j = 0; j < vlB.size(); j++) {
        if (lineCross(vlA[i], vlB[j]))
          continue;
        vpt.push_back(ssCrosspoint(vlA[i], vlB[j]));
      }
    }
    if (!vpt.empty()) {
      vector<P> tmp;
      tmp.push_back(vpt[0]);
      for (int i = 1; i < vpt.size(); i++) {
        bool flag = false;
        for (int j = 0; j < tmp.size(); j++)
          if (abs(tmp[j] - vpt[i]) < EPS)
            flag = true;
        if (!flag)
          tmp.push_back(vpt[i]);
      }
      vpt = tmp;
    }
    for (int i = 0; i < 1; i++) {
      for (int j = 3; j < n; j++) {
        vector<P> nvp;
        for (int k = 0; k < vpt.size(); k++) {
          vector<L> vL = distLine(vp[i], vp[j]);
          bool flag = false;
          for (int l = 0; l < vL.size(); l++) {
            if (onLine(vL[l], vpt[k])) {
              flag = true;
            }
          }
          if (flag)
            nvp.push_back(vpt[k]);
        }
        vpt = nvp;
      }
    }
    if (vpt.empty()) {
      puts("None");
    } else if (vpt.size() > 1) {
      puts("Many");
    } else {
      printf("%.5lf %.5lf\\n", real(vpt[0]), imag(vpt[0]));
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <complex>
#include <iostream>
#include <vector>

using namespace std;

typedef complex<double> P;
typedef pair<P, P> L;
const double EPS = 1e-5;
double cross(P a, P b) { return imag(conj(a) * b); }

bool lineCross(L l, L m) {
  return abs(imag((l.first - l.second) / (m.first - m.second))) < EPS;
}

P ssCrosspoint(L a, L b) {
  double A = cross(a.second - a.first, b.second - b.first);
  double B = cross(a.second - a.first, a.second - b.first);
  return b.first + B / A * (b.second - b.first);
}

vector<L> distLine(L l, L m) {
  vector<L> res;
  if (lineCross(l, m)) {
    P a = (l.first + m.first) / 2.0;
    P b = a + (l.second - l.first) / abs(l.second - l.first);
    res.push_back(make_pair(a, b));
  } else {
    P a = ssCrosspoint(l, m);
    if (abs(l.first - a) < EPS)
      swap(l.first, l.second);
    P dirA = (l.first - a) / abs(l.first - a);
    if (abs(m.first - a) < EPS)
      swap(m.first, m.second);
    P dirB = (m.first - a) / abs(m.first - a);
    P b = a + (dirA + dirB) / abs(dirA + dirB);
    res.push_back(make_pair(a, b));
    res.push_back(make_pair(a, (b - a) * P(0, 1) + a));
  }
  return res;
}

bool onLine(L a, P p) {
  return abs(imag((p - a.first) / (a.second - a.first))) < EPS;
}

int main() {
  int n;
  while (cin >> n, n) {
    if (n < 3) {
      puts("Many");
      continue;
    }
    vector<pair<P, P>> vp;
    for (int i = 0; i < n; i++) {
      double a, b, c, d;
      cin >> a >> b >> c >> d;
      { vp.push_back(make_pair(P(a, b), P(c, d))); }
    }
    vector<P> vpt;
    vector<L> vlA = distLine(vp[0], vp[1]), vlB = distLine(vp[0], vp[2]);
    for (int i = 0; i < vlA.size(); i++) {
      for (int j = 0; j < vlB.size(); j++) {
        if (lineCross(vlA[i], vlB[j]))
          continue;
        vpt.push_back(ssCrosspoint(vlA[i], vlB[j]));
      }
    }
    if (!vpt.empty()) {
      vector<P> tmp;
      tmp.push_back(vpt[0]);
      for (int i = 1; i < vpt.size(); i++) {
        bool flag = false;
        for (int j = 0; j < tmp.size(); j++)
          if (abs(tmp[j] - vpt[i]) < EPS)
            flag = true;
        if (!flag)
          tmp.push_back(vpt[i]);
      }
      vpt = tmp;
    }
    for (int i = 0; i < 1; i++) {
      for (int j = 3; j < n; j++) {
        vector<P> nvp;
        for (int k = 0; k < vpt.size(); k++) {
          vector<L> vL = distLine(vp[i], vp[j]);
          bool flag = false;
          for (int l = 0; l < vL.size(); l++) {
            if (onLine(vL[l], vpt[k])) {
              flag = true;
            }
          }
          if (flag)
            nvp.push_back(vpt[k]);
        }
        vpt = nvp;
      }
    }
    if (vpt.empty()) {
      puts("None");
    } else if (vpt.size() > 1) {
      puts("Many");
    } else {
      printf("%.5lf %.5lf\\n", real(vpt[0]), imag(vpt[0]));
    }
  }
  return 0;
}