Problem p01128 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = a; i < b; i++)

const double EPS = 1e-8;
const double INF = 1e12;
typedef complex<double> P;
namespace std {
bool operator<(const P &a, const P &b) {
  return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);
}
} // namespace std
double cross(const P &a, const P &b) { return imag(conj(a) * b); }
double dot(const P &a, const P &b) { return real(conj(a) * b); }

struct L : public vector<P> {
  L() {}
  L(const P &a, const P &b) {
    push_back(a);
    push_back(b);
  }
};

int ccw(P a, P b, P c) {
  b -= a;
  c -= a;
  if (cross(b, c) > 0)
    return +1; // counter clockwise
  if (cross(b, c) < 0)
    return -1; // clockwise
  if (dot(b, c) < 0)
    return +2; // c--a--b on line
  if (norm(b) < norm(c))
    return -2; // a--b--c on line
  return 0;
}

bool intersectSS(const L &s, const L &t) {
  return ccw(s[0], s[1], t[0]) * ccw(s[0], s[1], t[1]) <= 0 &&
         ccw(t[0], t[1], s[0]) * ccw(t[0], t[1], s[1]) <= 0;
}

P crosspoint(const L &l, const L &m) {
  double A = cross(l[1] - l[0], m[1] - m[0]);
  double B = cross(l[1] - l[0], l[1] - m[0]);
  if (abs(A) < EPS && abs(B) < EPS)
    return m[0]; // same line
  if (abs(A) < EPS)
    assert(false); // !!!PRECONDITION NOT SATISFIED!!!
  return m[0] + B / A * (m[1] - m[0]);
}

L ab;
int n;
L st[100];
int o[100], l[100];

void init() {
  double x, y;

  cin >> x >> y;
  P a(x, y);
  cin >> x >> y;
  P b(x, y);
  ab = L(a, b);

  cin >> n;

  rep(i, 0, n) {
    cin >> x >> y;
    P s(x, y);
    cin >> x >> y;
    P t(x, y);
    st[i] = L(s, t);
    cin >> o[i] >> l[i];
  }
}

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

  int dn;
  cin >> dn;
  rep(_dn, 0, dn) {
    init();

    vector<pair<double, int>> v;
    rep(i, 0, n) {
      if (!intersectSS(ab, st[i]))
        continue;

      P c = crosspoint(ab, st[i]);
      double d = abs(c - ab[0]);

      int p;
      if (o[i] == 0)
        p = 1 - l[i];
      else
        p = l[i];

      v.push_back(make_pair(d, p));
    }

    sort(v.begin(), v.end());

    int ans = 0;
    rep(i, 0, v.size() - 1) {
      if (v[i].second != v[i + 1].second)
        ans++;
    }
    cout << ans << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = a; i < b; i++)

const double EPS = 1e-8;
const double INF = 1e12;
typedef complex<double> P;
namespace std {
bool operator<(const P &a, const P &b) {
  return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);
}
} // namespace std
double cross(const P &a, const P &b) { return imag(conj(a) * b); }
double dot(const P &a, const P &b) { return real(conj(a) * b); }

struct L : public vector<P> {
  L() {}
  L(const P &a, const P &b) {
    push_back(a);
    push_back(b);
  }
};

int ccw(P a, P b, P c) {
  b -= a;
  c -= a;
  if (cross(b, c) > 0)
    return +1; // counter clockwise
  if (cross(b, c) < 0)
    return -1; // clockwise
  if (dot(b, c) < 0)
    return +2; // c--a--b on line
  if (norm(b) < norm(c))
    return -2; // a--b--c on line
  return 0;
}

bool intersectSS(const L &s, const L &t) {
  return ccw(s[0], s[1], t[0]) * ccw(s[0], s[1], t[1]) <= 0 &&
         ccw(t[0], t[1], s[0]) * ccw(t[0], t[1], s[1]) <= 0;
}

P crosspoint(const L &l, const L &m) {
  double A = cross(l[1] - l[0], m[1] - m[0]);
  double B = cross(l[1] - l[0], l[1] - m[0]);
  if (abs(A) < EPS && abs(B) < EPS)
    return m[0]; // same line
  if (abs(A) < EPS)
    assert(false); // !!!PRECONDITION NOT SATISFIED!!!
  return m[0] + B / A * (m[1] - m[0]);
}

L ab;
int n;
L st[100];
int o[100], l[100];

void init() {
  double x, y;

  cin >> x >> y;
  P a(x, y);
  cin >> x >> y;
  P b(x, y);
  ab = L(a, b);

  cin >> n;

  rep(i, 0, n) {
    cin >> x >> y;
    P s(x, y);
    cin >> x >> y;
    P t(x, y);
    st[i] = L(s, t);
    cin >> o[i] >> l[i];
  }
}

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

  int dn;
  cin >> dn;
  rep(_dn, 0, dn) {
    init();

    vector<pair<double, int>> v;
    rep(i, 0, n) {
      if (!intersectSS(ab, st[i]))
        continue;

      P c = crosspoint(ab, st[i]);
      double d = abs(c - ab[0]);

      int p;
      if (o[i] == 0)
        p = 1 - l[i];
      else
        p = l[i];

      v.push_back(make_pair(d, p));
    }

    if (v.size() == 0) {
      cout << 0 << endl;
      continue;
    }

    sort(v.begin(), v.end());

    int ans = 0;
    rep(i, 0, v.size() - 1) {
      if (v[i].second != v[i + 1].second)
        ans++;
    }
    cout << ans << endl;
  }
}