Problem p01134 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toStr(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define REP(i, n) FOR(i, 0, (n)-1)
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INF = INT_MAX / 10;

#include <complex>
typedef complex<double> P;

#define EQ(x, y) (abs((x) - (y)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))

double cross(P a, P b) { return a.real() * b.imag() - a.imag() * b.real(); }

int is_point_on_ls(P a, P b, P c) {
  return (abs(a - c) + abs(c - b) < abs(a - b) + EPS);
}

int is_intersection_ls(P a1, P a2, P b1, P b2) {
  if ((cross(a2 - a1, b1 - a1) * cross(a2 - a1, b2 - a1) < -EPS) &&
      (cross(b2 - b1, a1 - b1) * cross(b2 - b1, a2 - b1) < -EPS))
    return true;
  else
    return false;
}

P intersection_ls(P a1, P a2, P b1, P b2) {
  P b = b2 - b1;
  double d1 = abs(cross(b, a1 - b1));
  double d2 = abs(cross(b, a2 - b1));
  double t = d1 / (d1 + d2);

  return a1 + (a2 - a1) * t;
}

typedef vector<P> vp;

int main() {
  int n;
  while (cin >> n, n) {
    vector<vp> ps(n, vp(2));
    double x, y;
    REP(i, n) {
      REP(j, 2) {
        cin >> x >> y;
        ps[i][j] = P(x, y);
      }
    }

    int ans = 1;
    REP(i, n) {
      ans++;
      vp cp;
      REP(j, i) {
        if (is_intersection_ls(ps[i][0], ps[i][1], ps[j][0], ps[j][1])) {
          P p = intersection_ls(ps[i][0], ps[i][1], ps[j][0], ps[j][1]);
          int size = cp.size();
          bool exist = false;
          REP(k, size) {
            if (EQV(p, cp[k])) {
              exist = true;
              break;
            }
          }
          if (!exist) {
            cp.push_back(p);
            ans++;
          }
        }
      }
    }

    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toStr(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define REP(i, n) FOR(i, 0, (n)-1)
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INF = INT_MAX / 10;

#include <complex>
typedef complex<double> P;
typedef vector<P> vp;

#define EQ(x, y) (abs((x) - (y)) < EPS)
#define LT(x, y) ((x) - (y) <= -EPS)
#define LE(x, y) ((x) - (y) < +EPS)

double cross(P a, P b) { return (a.real() * b.imag() - a.imag() * b.real()); }

int is_point_on_ls(P a, P b, P c) {
  return (abs(a - c) + abs(c - b) < abs(a - b) + EPS);
}

int is_intersected_ls(P a1, P a2, P b1, P b2) {
  if ((cross(a2 - a1, b1 - a1) * cross(a2 - a1, b2 - a1) < -EPS) &&
      (cross(b2 - b1, a1 - b1) * cross(b2 - b1, a2 - b1) < -EPS))
    return true;
  return is_point_on_ls(a1, a2, b1) || is_point_on_ls(a1, a2, b2) ||
         is_point_on_ls(b1, b2, a1) || is_point_on_ls(b1, b2, a2);
}

P intersection_ls(P a1, P a2, P b1, P b2) {
  P b = b2 - b1;
  double d1 = abs(cross(b, a1 - b1));
  double d2 = abs(cross(b, a2 - b1));
  double t = d1 / (d1 + d2);

  return a1 + (a2 - a1) * t;
}

typedef vector<double> vd;

int main() {
  int n;
  while (cin >> n, n) {
    vector<vd> xy(n, vd(4));
    vp a(n), b(n);
    REP(i, n) {
      REP(j, 4) { cin >> xy[i][j]; }
    }

    int ans = 1;
    REP(i, n) {
      int cp = 1;
      vp points;
      P a1(xy[i][0], xy[i][1]), a2(xy[i][2], xy[i][3]);
      REP(j, i) {
        P b1(xy[j][0], xy[j][1]), b2(xy[j][2], xy[j][3]);
        if (is_intersected_ls(a1, a2, b1, b2)) {
          P po = intersection_ls(a1, a2, b1, b2);
          int size = points.size();
          if (size == 0) {
            points.push_back(po);
            cp++;
          } else {
            REP(k, size) {
              if (!(abs(po - points[k]) < EPS)) {
                points.push_back(po);
                cp++;
              }
            }
          }
        }
      }
      ans += cp;
    }

    cout << ans << endl;
  }

  return 0;
}