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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <map>
#include <vector>

using namespace std;

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

const ldb eps = 1e-10;
const ldb pi = acos(-1);
int sig(ldb a) { return (a < -eps) ? -1 : (a > eps) ? 1 : 0; }
bool eq(ldb a, ldb b) { return abs(a - b) < eps; }
ldb dot(P a, P b) { return (a.real() * b.real() + a.imag() * b.imag()); }
ldb cross(P a, P b) { return (a.real() * b.imag() - a.imag() * b.real()); }

struct L {
  P a, b;
  L() {}
  L(P a, P b) : a(a), b(b) {}
  ldb len() { return abs(a - b); }
  P vec() { return b - a; }
};

P iP2(L a, L b) {
  P c = b.b - b.a;
  ldb d1 = abs(cross(c, a.a - b.a));
  ldb d2 = abs(cross(c, a.b - b.a));
  ldb t = d1 / (d1 + d2);
  return a.a + (a.b - a.a) * t;
}

enum CCW { FRONT = 1, RIGHT = 2, BACK = 4, LEFT = 8, ON = 16 };
int ccw(L s, P p) {
  P a = s.vec();
  p -= s.a;
  ldb cr = cross(a, p);
  if (eq(cr, 0)) {
    if (sig(dot(a, p)) < 0)
      return BACK;
    if (sig(norm(a) - norm(p)) < 0)
      return FRONT;
    return ON;
  }
  return (cr > 0) ? LEFT : RIGHT;
}

bool iI2_1(L a, L b) {
  return ((ccw(a, b.a) | ccw(a, b.b)) & (ccw(b, a.a) | ccw(b, a.b))) ==
         (LEFT | RIGHT);
}

bool cmp(P a, P b) {
  return make_pair(a.real(), a.imag()) < make_pair(b.real(), b.imag());
}

int main(void) {
  int n;
  for (;;) {
    scanf("%d", &n);
    if (n == 0)
      break;
    vector<L> ls;
    int x1, y1, x2, y2;
    int res = 1;
    for (int i = 0; i < n; i++) {
      scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
      P a(x1, y1), b(x2, y2);
      L l(a, b);
      int inc = 0;
      vector<P> v;
      for (int j = 0; j < int(ls.size()); j++) {
        if (!iI2_1(l, ls[j]))
          continue;
        P c = iP2(l, ls[j]);
        bool ex = false;
        for (int k = 0; k < int(v.size()); k++) {
          if (abs(v[k] - c) < eps)
            ex = true;
        }
        if (!ex) {
          v.push_back(c);
          inc += 1;
        }
      }
      res += inc + 1;
      ls.push_back(l);
    }
    printf("%d\\n", res);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <vector>

using namespace std;

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

const ldb eps = 1e-10;
const ldb pi = acos(-1);
int sig(ldb a) { return (a < -eps) ? -1 : (a > eps) ? 1 : 0; }
bool eq(ldb a, ldb b) { return abs(a - b) < eps; }
ldb dot(P a, P b) { return (a.real() * b.real() + a.imag() * b.imag()); }
ldb cross(P a, P b) { return (a.real() * b.imag() - a.imag() * b.real()); }

struct L {
  P a, b;
  L() {}
  L(P a, P b) : a(a), b(b) {}
  ldb len() { return abs(a - b); }
  P vec() { return b - a; }
};

P iP2(L a, L b) {
  P c = b.b - b.a;
  ldb d1 = abs(cross(c, a.a - b.a));
  ldb d2 = abs(cross(c, a.b - b.a));
  ldb t = d1 / (d1 + d2);
  return a.a + (a.b - a.a) * t;
}

enum CCW { FRONT = 1, RIGHT = 2, BACK = 4, LEFT = 8, ON = 16 };
int ccw(L s, P p) {
  P a = s.vec();
  p -= s.a;
  ldb cr = cross(a, p);
  if (eq(cr, 0)) {
    if (sig(dot(a, p)) < 0)
      return BACK;
    if (sig(norm(a) - norm(p)) < 0)
      return FRONT;
    return ON;
  }
  return (cr > 0) ? LEFT : RIGHT;
}

bool iI2_1(L a, L b) {
  return ((ccw(a, b.a) | ccw(a, b.b)) & (ccw(b, a.a) | ccw(b, a.b))) ==
         (LEFT | RIGHT);
}

int main(void) {
  int n;
  for (;;) {
    scanf("%d", &n);
    if (n == 0)
      break;
    vector<P> ps;
    vector<L> ls;
    int x1, y1, x2, y2;
    int res = 1;
    for (int i = 0; i < n; i++) {
      scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
      P a(x1, y1), b(x2, y2);
      L l(a, b);
      int sz = ps.size();
      int inc = 0;
      for (int j = 0; j < int(ls.size()); j++) {
        if (!iI2_1(l, ls[j]))
          continue;
        P c = iP2(l, ls[j]);
        bool ex = false;
        for (int k = 0; k < sz; k++) {
          if (abs(ps[k] - c) < eps)
            ex = true;
        }
        if (!ex) {
          ps.push_back(c);
          inc += 1;
        }
      }
      res += inc + 1;
      ls.push_back(l);
    }
    printf("%d\\n", res);
  }
  return 0;
}