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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cout << *i << " ";
  cout << endl;
}

typedef long long ll;
#define X real()
#define Y imag()

typedef double D;
typedef complex<D> P;
typedef pair<P, P> L;
typedef vector<P> Pol;

const D INF = 1e40;
const D EPS = 1e-5;
const D PI = acos(-1);

int sign(D r) { return (r < -EPS) ? -1 : (r > EPS) ? 1 : 0; }
bool eq(D a, D b) { return abs(b - a) < EPS; }
bool near(P a, P b) { return abs(b - a) < EPS; }

D dot(P a, P b) { return a.X * b.X + a.Y * b.Y; }
D cross(P a, P b) { return a.X * b.Y - a.Y * b.X; }
P vec(L a) { return a.second - a.first; }

P pLL(L a, L b) {
  return a.first +
         vec(a) * (cross(vec(b), b.first - a.first) / cross(vec(b), vec(a)));
}
int main() {
  int N;
  while (cin >> N && N) {
    vector<L> l;
    REP(i, N) {
      double a, b, c, d;
      cin >> a >> b >> c >> d;
      l.push_back(L(P(a, b), P(c, d)));
    }
    int ans = 1;
    for (int i = 0; i < N; i++) {
      int cnt = 1;
      vector<P> crs;
      for (int j = 0; j < i; j++) {
        if (!sign(cross(vec(l[i]), vec(l[j]))))
          continue;
        P p = pLL(l[i], l[j]);
        if (!(abs(p.X) < 100 - EPS && abs(p.Y) < 100 - EPS))
          continue;
        bool ok = true;
        for (int k = 0; k < crs.size(); k++) {
          if (eq(crs[k].X, p.X) && eq(crs[k].Y, p.Y)) {
            ok = false;
            break;
          }
        }
        if (ok) {
          crs.push_back(p);
          cnt++;
        }
      }
      ans += cnt;
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cout << *i << " ";
  cout << endl;
}

typedef long long ll;
#define X real()
#define Y imag()

typedef long double D;
typedef complex<D> P;
typedef pair<P, P> L;
typedef vector<P> Pol;

const D INF = 1e40;
const D EPS = 1e-8;
const D PI = acos(-1);

int sign(D r) { return (r < -EPS) ? -1 : (r > EPS) ? 1 : 0; }
bool eq(D a, D b) { return abs(b - a) < EPS; }
bool near(P a, P b) { return abs(b - a) < EPS; }

D dot(P a, P b) { return a.X * b.X + a.Y * b.Y; }
D cross(P a, P b) { return a.X * b.Y - a.Y * b.X; }
P vec(L a) { return a.second - a.first; }

P pLL(L a, L b) {
  return a.first +
         vec(a) * (cross(vec(b), b.first - a.first) / cross(vec(b), vec(a)));
}
int main() {
  int N;
  while (cin >> N && N) {
    vector<L> l;
    REP(i, N) {
      double a, b, c, d;
      cin >> a >> b >> c >> d;
      l.push_back(L(P(a, b), P(c, d)));
    }
    vector<P> crs;
    int ans = 1;
    for (int i = 0; i < N; i++) {
      int cnt = 1;
      for (int j = 0; j < i; j++) {
        if (!sign(cross(vec(l[i]), vec(l[j]))))
          continue;
        P p = pLL(l[i], l[j]);
        if (!(abs(p.X) < 100 - EPS && abs(p.Y) < 100 - EPS))
          continue;
        bool ok = true;
        for (int k = 0; k < crs.size(); k++) {
          if (near(p, crs[k])) {
            ok = false;
            break;
          }
        }
        if (ok) {
          crs.push_back(p);
          cnt++;
        }
      }
      ans += cnt;
    }
    cout << ans << endl;
  }
  return 0;
}