Problem p01128 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <complex>
#include <iostream>
#include <utility>
#include <vector>
#define X real()
#define Y imag()
#define EPS (1e-10)
using namespace std;
typedef complex<double> P;
typedef pair<P, P> L;

namespace std {
bool operator<(const P &a, const P &b) {
  return a.X != b.X ? a.X < b.X : a.Y < b.Y;
}
} // namespace std

double dot(P a, P b) { return a.X * b.X + a.Y * b.Y; }

double cross(P a, P b) { return a.X * b.Y - a.Y * b.X; }

P intersection(L a, L b) {
  //??´???a??¨??´???b?????????
  return a.first + cross(b.second - b.first, a.first - b.first) /
                       (-cross(b.second - b.first, a.second - b.first) +
                        cross(b.second - b.first, a.first - b.first)) *
                       (a.second - a.first);
}

int ccw(P a, P b, P c) {
  b -= a;
  c -= a;
  if (cross(b, c) > EPS)
    return +1;
  if (cross(b, c) < -EPS)
    return -1;
  if (dot(b, c) < 0)
    return +2;
  if (abs(b - a) + EPS < abs(c - a))
    return -2;
  return 0;
}

bool is_cross(L a, L b) {
  if (ccw(a.first, a.second, b.first) * ccw(a.first, a.second, b.second) <= 0 &&
      ccw(b.first, b.second, a.first) * ccw(b.first, b.second, a.second) <= 0)
    return true;
  return false;
}

int main() {
  int N;
  cin >> N;
  double x_s, y_s, x_t, y_t;
  for (int i = 0; i < N; ++i) {
    cin >> x_s >> y_s >> x_t >> y_t;
    P a = P(x_s, y_s), b = P(x_t, y_t);
    L S = L(a, b);
    int n, o, l;
    cin >> n;
    vector<pair<P, int>> cp;
    for (int i = 0; i < n; ++i) {
      cin >> x_s >> y_s >> x_t >> y_t >> o >> l;
      L T = L(P(x_s, y_s), P(x_t, y_t));
      if (is_cross(S, T))
        cp.push_back(make_pair(intersection(S, T), o ^ l));
    }
    sort(cp.begin(), cp.end());
    int t = cp[0].second, ans = 0;
    for (int i = 1; i < (int)cp.size(); ++i) {
      if (t != cp[i].second) {
        ++ans;
        t = cp[i].second;
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <complex>
#include <iostream>
#include <utility>
#include <vector>
#define X real()
#define Y imag()
#define EPS (1e-10)
using namespace std;
typedef complex<double> P;
typedef pair<P, P> L;

namespace std {
bool operator<(const P &a, const P &b) {
  return a.X != b.X ? a.X < b.X : a.Y < b.Y;
}
} // namespace std

double dot(P a, P b) { return a.X * b.X + a.Y * b.Y; }

double cross(P a, P b) { return a.X * b.Y - a.Y * b.X; }

P intersection(L a, L b) {
  //??´???a??¨??´???b?????????
  return a.first + cross(b.second - b.first, a.first - b.first) /
                       (-cross(b.second - b.first, a.second - b.first) +
                        cross(b.second - b.first, a.first - b.first)) *
                       (a.second - a.first);
}

int ccw(P a, P b, P c) {
  b -= a;
  c -= a;
  if (cross(b, c) > EPS)
    return +1;
  if (cross(b, c) < -EPS)
    return -1;
  if (dot(b, c) < 0)
    return +2;
  if (abs(b - a) + EPS < abs(c - a))
    return -2;
  return 0;
}

bool is_cross(L a, L b) {
  if (ccw(a.first, a.second, b.first) * ccw(a.first, a.second, b.second) <= 0 &&
      ccw(b.first, b.second, a.first) * ccw(b.first, b.second, a.second) <= 0)
    return true;
  return false;
}

int main() {
  int N;
  cin >> N;
  double x_s, y_s, x_t, y_t;
  for (int i = 0; i < N; ++i) {
    cin >> x_s >> y_s >> x_t >> y_t;
    P a = P(x_s, y_s), b = P(x_t, y_t);
    L S = L(a, b);
    int n, o, l;
    cin >> n;
    vector<pair<P, int>> cp;
    for (int i = 0; i < n; ++i) {
      cin >> x_s >> y_s >> x_t >> y_t >> o >> l;
      L T = L(P(x_s, y_s), P(x_t, y_t));
      if (is_cross(S, T))
        cp.push_back(make_pair(intersection(S, T), o ^ l));
    }
    sort(cp.begin(), cp.end());
    if (!cp.size()) {
      cout << 0 << endl;
      continue;
    }
    int t = cp[0].second, ans = 0;
    for (int i = 1; i < (int)cp.size(); ++i) {
      if (t != cp[i].second) {
        ++ans;
        t = cp[i].second;
      }
    }
    cout << ans << endl;
  }
  return 0;
}