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

#include <algorithm>
#include <complex>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)

typedef long long int ll;
typedef double D;     // ??§?¨?????????????double???long double?????????
typedef complex<D> P; // Point
typedef pair<P, P> L; // Line
typedef vector<P> VP;

const int INF = (1 << 30) - 1;
const D EPS = 1e-9;

#define X real()
#define Y imag()
#define LE(n, m) ((n) < (m) + EPS)
#define GE(n, m) ((n) + EPS > (m))
#define EQ(n, m) (abs((n) - (m)) < EPS)

// ?????????dot(a,b) = |a||b|cos??
D dot(P a, P b) { return (conj(a) * b).X; }
// ?????????cross(a,b) = |a||b|sin??
D cross(P a, P b) { return (conj(a) * b).Y; }
// ?????????????????? a->b->c
int ccw(P a, P b, P c) {
  b -= a;
  c -= a;
  // 3???????????´???????????????
  if (cross(b, c) > EPS)
    return +1; // counter clockwise
  if (cross(b, c) < -EPS)
    return -1; // clockwise
  // 3???????????´???????????????
  if (dot(b, c) < -EPS)
    return +2; // c--a--b on line
  if (norm(b) < norm(c))
    return -2; // a--b--c on line or a==b
  return 0;    // a--c--b on line or a==c or b==c
}
// ????????¨??????
bool isecSS(P a1, P a2, P b1, P b2) {
  return ccw(a1, a2, b1) * ccw(a1, a2, b2) <= 0 &&
         ccw(b1, b2, a1) * ccw(b1, b2, a2) <= 0;
}

P crosspointLL(P a1, P a2, P b1, P b2) {
  D d1 = cross(b2 - b1, b1 - a1);
  D d2 = cross(b2 - b1, a2 - a1);
  return a1 + d1 / d2 * (a2 - a1);
}

class CrossPoint {
public:
  double x, y;
  int o, l;
  bool is_underground;
  CrossPoint(double x, double y, int o, int l) : x(x), y(y), o(o), l(l) {
    is_underground = o ^ l;
  };
};

bool cmp(CrossPoint p1, CrossPoint p2) {
  if (EQ(p1.x, p2.x))
    return p1.y < p2.y;
  else
    return p1.x < p2.x;
}

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

  int T;
  cin >> T;
  while (T--) {
    int xa, ya, xb, yb;
    cin >> xa >> ya >> xb >> yb;

    int N;
    cin >> N;

    vector<CrossPoint> crosspoints;
    for (int i = 0; i < N; i++) {
      int xs, ys, xt, yt, o, l;
      cin >> xs >> ys >> xt >> yt >> o >> l;

      if (isecSS(P(xa, ya), P(xb, yb), P(xs, ys), P(xt, yt))) {
        P p = crosspointLL(P(xa, ya), P(xb, yb), P(xs, ys), P(xt, yt));
        crosspoints.push_back(CrossPoint(p.X, p.Y, o, l));
      }
    }

    if (crosspoints.size()) {
      sort(crosspoints.begin(), crosspoints.end(), cmp);

      int count = 0;
      for (int i = 1; i < crosspoints.size(); i++)
        count +=
            crosspoints[i - 1].is_underground != crosspoints[i].is_underground;
      cout << count << endl;
    } else {
      cout << 0 << endl;
    }
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <complex>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)

typedef long long int ll;
typedef double D;     // ??§?¨?????????????double???long double?????????
typedef complex<D> P; // Point
typedef pair<P, P> L; // Line
typedef vector<P> VP;

const int INF = (1 << 30) - 1;
const D EPS = 1e-9;

#define X real()
#define Y imag()
#define LE(n, m) ((n) < (m) + EPS)
#define GE(n, m) ((n) + EPS > (m))
#define EQ(n, m) (abs((n) - (m)) < EPS)

// ?????????dot(a,b) = |a||b|cos??
D dot(P a, P b) { return (conj(a) * b).X; }
// ?????????cross(a,b) = |a||b|sin??
D cross(P a, P b) { return (conj(a) * b).Y; }
// ?????????????????? a->b->c
int ccw(P a, P b, P c) {
  b -= a;
  c -= a;
  // 3???????????´???????????????
  if (cross(b, c) > EPS)
    return +1; // counter clockwise
  if (cross(b, c) < -EPS)
    return -1; // clockwise
  // 3???????????´???????????????
  if (dot(b, c) < -EPS)
    return +2; // c--a--b on line
  if (norm(b) < norm(c))
    return -2; // a--b--c on line or a==b
  return 0;    // a--c--b on line or a==c or b==c
}
// ??´???a??¨??????b
bool isecLS(P a1, P a2, P b1, P b2) {
  return ccw(a1, a2, b1) * ccw(a1, a2, b2) <= 0;
}

P crosspointLL(P a1, P a2, P b1, P b2) {
  D d1 = cross(b2 - b1, b1 - a1);
  D d2 = cross(b2 - b1, a2 - a1);
  return a1 + d1 / (d1 + d2) * (a2 - a1);
}

class CrossPoint {
public:
  double x, y;
  int o, l;
  bool is_underground;
  CrossPoint(double x, double y, int o, int l) : x(x), y(y), o(o), l(l) {
    is_underground = o ^ l;
  };
};

bool cmp(CrossPoint p1, CrossPoint p2) {
  if (EQ(p1.x, p2.x))
    return p1.y < p2.y;
  else
    return p1.x < p2.x;
}

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

  int T;
  cin >> T;
  while (T--) {
    int xa, ya, xb, yb;
    cin >> xa >> ya >> xb >> yb;

    int N;
    cin >> N;

    vector<CrossPoint> crosspoints;
    for (int i = 0; i < N; i++) {
      int xs, ys, xt, yt, o, l;
      cin >> xs >> ys >> xt >> yt >> o >> l;

      if (isecLS(P(xa, ya), P(xb, yb), P(xs, ys), P(xt, yt))) {
        P p = crosspointLL(P(xa, ya), P(xb, yb), P(xs, ys), P(xt, yt));
        crosspoints.push_back(CrossPoint(p.X, p.Y, o, l));
      }
    }
    sort(crosspoints.begin(), crosspoints.end(), cmp);

    int count = 0;
    bool is_underground = crosspoints[0].is_underground;
    int i = 0;
    while (i < crosspoints.size()) {
      while (i < crosspoints.size() &&
             crosspoints[i].is_underground == is_underground)
        i++;
      if (i < crosspoints.size()) {
        is_underground = crosspoints[i].is_underground;
        count++;
      }
      i++;
    }

    cout << count << endl;
  }

  return 0;
}