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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
#define REP(i, a, n) for (int i = (a); i < (n); i++)
using namespace std;
typedef pair<double, int> pdi;

struct point {
  int x, y;
};
struct line {
  point p, q;
};

int T, N, O[110], L[110];
line A, S[110];

bool intersect(line l1, line l2) {
  double ax = l1.p.x, ay = l1.p.y;
  double bx = l1.q.x, by = l1.q.y;
  double cx = l2.p.x, cy = l2.p.y;
  double dx = l2.q.x, dy = l2.q.y;
  double ta = (cx - dx) * (ay - cy) + (cy - dy) * (cx - ax);
  double tb = (cx - dx) * (by - cy) + (cy - dy) * (cx - bx);
  double tc = (ax - bx) * (cy - ay) + (ay - by) * (ax - cx);
  double td = (ax - bx) * (dy - ay) + (ay - by) * (ax - dx);
  return tc * td < 0 && ta * tb < 0;
}

double distance(line l, point p) {
  double ux = l.q.x - l.p.x;
  double uy = l.q.y - l.p.y;
  double vx = p.x - l.p.x;
  double vy = p.y - l.p.y;
  double cross = abs(ux * vy - uy * vx);
  double ul = sqrt(ux * ux + uy * uy);
  return cross / ul;
}

int main(void) {
  cin >> T;
  while (T--) {
    cin >> A.p.x >> A.p.y >> A.q.x >> A.q.y;
    cin >> N;
    REP(i, 0, N)
        cin >> S[i].p.x >> S[i].p.y >> S[i].q.x >> S[i].q.y >> O[i] >> L[i];

    vector<pdi> E;
    REP(i, 0, N) if (intersect(A, S[i])) {
      double da = distance(S[i], A.p);
      double db = distance(S[i], A.q);
      double d = da / (da + db);
      E.push_back(pdi(d, i));
    }

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

    vector<int> g;
    REP(i, 0, E.size()) {
      int k = E[i].second;
      g.push_back(O[k] == 1 ? L[k] : (L[k] ^ 1));
    }

    int ans = 0;
    REP(i, 1, (g.size())) if (g[i - 1] != g[i]) ans++;
    cout << ans << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
#define REP(i, a, n) for (int i = (a); i < (n); i++)
using namespace std;
typedef pair<double, int> pdi;

struct point {
  int x, y;
};
struct line {
  point p, q;
};

int T, N, O[110], L[110];
line A, S[110];

bool intersect(line l1, line l2) {
  double ax = l1.p.x, ay = l1.p.y;
  double bx = l1.q.x, by = l1.q.y;
  double cx = l2.p.x, cy = l2.p.y;
  double dx = l2.q.x, dy = l2.q.y;
  double ta = (cx - dx) * (ay - cy) + (cy - dy) * (cx - ax);
  double tb = (cx - dx) * (by - cy) + (cy - dy) * (cx - bx);
  double tc = (ax - bx) * (cy - ay) + (ay - by) * (ax - cx);
  double td = (ax - bx) * (dy - ay) + (ay - by) * (ax - dx);
  return tc * td < 0 && ta * tb < 0;
}

double distance(line l, point p) {
  double ux = l.q.x - l.p.x;
  double uy = l.q.y - l.p.y;
  double vx = p.x - l.p.x;
  double vy = p.y - l.p.y;
  double cross = abs(ux * vy - uy * vx);
  double ul = sqrt(ux * ux + uy * uy);
  return cross / ul;
}

int main(void) {
  cin >> T;
  while (T--) {
    cin >> A.p.x >> A.p.y >> A.q.x >> A.q.y;
    cin >> N;
    REP(i, 0, N)
        cin >> S[i].p.x >> S[i].p.y >> S[i].q.x >> S[i].q.y >> O[i] >> L[i];

    vector<pdi> E;
    REP(i, 0, N) if (intersect(A, S[i])) {
      double da = distance(S[i], A.p);
      double db = distance(S[i], A.q);
      double d = da / (da + db);
      E.push_back(pdi(d, i));
    }

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

    vector<int> g;
    REP(i, 0, E.size()) {
      int k = E[i].second;
      g.push_back(O[k] == 1 ? L[k] : (L[k] ^ 1));
    }

    int ans = 0;
    REP(i, 0, g.size() - 1) if (g[i] != g[i + 1]) ans++;
    cout << ans << endl;
  }

  return 0;
}