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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

using ld = long double;
using P = std::complex<ld>;
using G = std::vector<P>;
const ld pi = std::acos(-1);
const ld eps = 1e-10;
const ld inf = 1e12;

ld cross(const P &a, const P &b) {
  return a.real() * b.imag() - a.imag() * b.real();
}
ld dot(const P &a, const P &b) {
  return a.real() * b.real() + a.imag() * b.imag();
}

/*
CCW

-- BEHIND -- [a -- ON -- b] --- FRONT --

CW
*/
enum CCW_RESULT { CCW = +1, CW = -1, BEHIND = +2, FRONT = -2, ON = 0 };
int ccw(P a, P b, P c) {
  b -= a;
  c -= a;
  if (cross(b, c) > eps)
    return CCW; // counter clockwise
  if (cross(b, c) < -eps)
    return CW; // clockwise
  if (dot(b, c) < 0)
    return BEHIND; // c--a--b on line
  if (norm(b) < norm(c))
    return FRONT; // a--b--c on line
  return ON;
}

namespace std {
bool operator<(const P &a, const P &b) {
  return std::abs(real(a) - real(b)) > eps ? real(a) < real(b)
                                           : imag(a) < imag(b);
}
} // namespace std

struct L : public std::vector<P> {
  L(const P &a = P(), const P &b = P()) : std::vector<P>(2) {
    begin()[0] = a;
    begin()[1] = b;
  }

  // Ax + By + C = 0
  L(ld A, ld B, ld C) {
    if (std::abs(A) < eps && std::abs(B) < eps) {
      abort();
    } else if (std::abs(A) < eps) {
      *this = L(P(0, -C / B), P(1, -C / B));
    } else if (std::abs(B) < eps) {
      *this = L(P(-C / A, 0), P(-C / A, 1));
    } else {
      *this = L(P(0, -C / B), P(-C / A, 0));
    }
  }
};

P crosspointLL(const L &l, const L &m) {
  ld A = cross(l[1] - l[0], m[1] - m[0]);
  ld B = cross(l[1] - l[0], l[1] - m[0]);
  if (std::abs(A) < eps)
    P(1e9, 1e9); // !!!PRECONDITION NOT SATISFIED!!!
  return m[0] + B / A * (m[1] - m[0]);
}

L l[100];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  while (cin >> n, n) {
    for (int i = 0; i < n; i++) {
      int x1, y1, x2, y2;
      cin >> x1 >> y1 >> x2 >> y2;
      l[i] = {P(x1, y1), P(x2, y2)};
    }

    int ans = 1;
    for (int i = 0; i < n; i++) {
      vector<P> p;
      for (int j = 0; j < i; j++) {
        P cp = crosspointLL(l[i], l[j]);
        if (abs(cp.real()) >= 100 - 1e-9 || abs(cp.imag()) >= 100 - 1e-9)
          continue;
        bool ok = true;
        for (int k = 0; k < p.size(); k++) {
          if (abs(p[k] - cp) < 1e-10) {
            ok = false;
            break;
          }
        }
        if (ok) {
          p.push_back(cp);
        }
      }
      ans += p.size() + 1;
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

using ld = long double;
using P = std::complex<ld>;
using G = std::vector<P>;
const ld pi = std::acos(-1);
const ld eps = 1e-10;
const ld inf = 1e12;

ld cross(const P &a, const P &b) {
  return a.real() * b.imag() - a.imag() * b.real();
}
ld dot(const P &a, const P &b) {
  return a.real() * b.real() + a.imag() * b.imag();
}

/*
CCW

-- BEHIND -- [a -- ON -- b] --- FRONT --

CW
*/
enum CCW_RESULT { CCW = +1, CW = -1, BEHIND = +2, FRONT = -2, ON = 0 };
int ccw(P a, P b, P c) {
  b -= a;
  c -= a;
  if (cross(b, c) > eps)
    return CCW; // counter clockwise
  if (cross(b, c) < -eps)
    return CW; // clockwise
  if (dot(b, c) < 0)
    return BEHIND; // c--a--b on line
  if (norm(b) < norm(c))
    return FRONT; // a--b--c on line
  return ON;
}

namespace std {
bool operator<(const P &a, const P &b) {
  return std::abs(real(a) - real(b)) > eps ? real(a) < real(b)
                                           : imag(a) < imag(b);
}
} // namespace std

struct L : public std::vector<P> {
  L(const P &a = P(), const P &b = P()) : std::vector<P>(2) {
    begin()[0] = a;
    begin()[1] = b;
  }

  // Ax + By + C = 0
  L(ld A, ld B, ld C) {
    if (std::abs(A) < eps && std::abs(B) < eps) {
      abort();
    } else if (std::abs(A) < eps) {
      *this = L(P(0, -C / B), P(1, -C / B));
    } else if (std::abs(B) < eps) {
      *this = L(P(-C / A, 0), P(-C / A, 1));
    } else {
      *this = L(P(0, -C / B), P(-C / A, 0));
    }
  }
};

P crosspointLL(const L &l, const L &m) {
  ld A = cross(l[1] - l[0], m[1] - m[0]);
  ld B = cross(l[1] - l[0], l[1] - m[0]);
  if (std::abs(A) < eps)
    P(1e9, 1e9); // !!!PRECONDITION NOT SATISFIED!!!
  return m[0] + B / A * (m[1] - m[0]);
}

L l[100];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  while (cin >> n, n) {
    for (int i = 0; i < n; i++) {
      int x1, y1, x2, y2;
      cin >> x1 >> y1 >> x2 >> y2;
      l[i] = {P(x1, y1), P(x2, y2)};
    }

    vector<P> p;
    int ans = 1;
    for (int i = 0; i < n; i++) {
      ans++;
      for (int j = 0; j < i; j++) {
        P cp = crosspointLL(l[i], l[j]);
        if (abs(cp.real()) > 100 || abs(cp.imag()) > 100)
          continue;
        bool same = false;
        for (auto pp : p) {
          if (norm(cp - pp) < 1e-20) {
            same = true;
            break;
          }
        }
        if (!same) {
          p.push_back(cp);
          ans++;
        }
      }
    }
    cout << ans << endl;
  }
}