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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;

inline double add(double a, double b) {
  return abs(a + b) < (1e-10) * (abs(a) + abs(b)) ? 0.0 : a + b;
}

struct vec {
  long double x, y;
  vec operator-(vec b) { return (vec){add(x, -b.x), add(y, -b.y)}; }
  vec operator+(vec b) { return (vec){add(x, b.x), add(y, b.y)}; }
  vec operator*(long double d) { return (vec){x * d, y * d}; }
  bool operator==(vec b) { return x == b.x && y == b.y; }
  bool operator!=(vec b) { return x != b.x || y != b.y; }
  bool operator<(const vec &r) const {
    return this->x == r.x ? this->y < r.y : this->x < r.x;
  }
  long double dot(vec v) { return add(x * v.x, y * v.y); }
  long double cross(vec v) { return add(x * v.y, -y * v.x); }
  long double norm() { return sqrt(x * x + y * y); }
};

const double eps = 0.0000001;

int ccw(vec &a, vec &b, vec &c) {
  vec ab = b - a, ac = c - a;
  long double o = ab.cross(ac);
  if (o > 0)
    return 1; // CCW
  if (o < 0)
    return -1; // CW
  if (ab.dot(ac) < 0) {
    return 2; // C-A-B
  } else {
    if (ab.dot(ab) < ac.dot(ac)) {
      return -2; // A-B-C
    } else {
      return 0; // A-C-B
    }
  }
}

vec interSection(vec &p0, vec &p1, vec &p2, vec &p3) {
  vec p23 = p3 - p2, p02 = p2 - p0, p01 = p1 - p0;
  double k = p23.cross(p02) / p23.cross(p01);
  return p0 + p01 * k;
}

int isInterSection(vec &p0, vec &p1, vec &p2,
                   vec &p3) { //??????p[0]p[1]??¨p[2]p[3]
  switch (ccw(p0, p1, p2) * ccw(p0, p1, p3)) {
  case -1:
  case -4:
    break;
  case 0:
    return ~(ccw(p0, p1, p2) | ccw(p0, p1, p3))
               ? 2
               : -1; //?§???§??\\??????????????´???
  default:
    return 0;
  }
  switch (ccw(p2, p3, p0) * ccw(p2, p3, p1)) {
  case -1:
  case -4:
  case 0:
    break;
  default:
    return 0;
  }
  return 1;
}

typedef pair<vec, int> P;

struct Section {
  vec v1, v2;   //?§??????¨??????
  vector<P> cp; //??????(????????????????????¨?????????)
};

int main(void) {
  int N;
  Section line;
  vector<Section> S;
  cin >> N;
  while (N--) {
    S.clear();
    line.cp.clear();
    cin >> line.v1.x >> line.v1.y >> line.v2.x >> line.v2.y;
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
      vec v1, v2;
      int o, l;
      cin >> v1.x >> v1.y >> v2.x >> v2.y >> o >> l;
      if (isInterSection(v1, v2, line.v1, line.v2)) {
        line.cp.push_back(
            P(interSection(v1, v2, line.v1, line.v2), o ? l : l ^ 1));
      }
    }
    sort(line.cp.begin(), line.cp.end());
    int c = 0, h = line.cp[0].second;
    for (int i = 1; i < line.cp.size(); i++) {
      if (h != line.cp[i].second) {
        c++;
        h = line.cp[i].second;
      }
    }
    cout << c << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;

inline double add(double a, double b) {
  return abs(a + b) < (1e-10) * (abs(a) + abs(b)) ? 0.0 : a + b;
}

struct vec {
  long double x, y;
  vec operator-(vec b) { return (vec){add(x, -b.x), add(y, -b.y)}; }
  vec operator+(vec b) { return (vec){add(x, b.x), add(y, b.y)}; }
  vec operator*(long double d) { return (vec){x * d, y * d}; }
  bool operator==(vec b) { return x == b.x && y == b.y; }
  bool operator!=(vec b) { return x != b.x || y != b.y; }
  bool operator<(const vec &r) const {
    return this->x == r.x ? this->y < r.y : this->x < r.x;
  }
  long double dot(vec v) { return add(x * v.x, y * v.y); }
  long double cross(vec v) { return add(x * v.y, -y * v.x); }
  long double norm() { return sqrt(x * x + y * y); }
};

const double eps = 0.0000001;

int ccw(vec &a, vec &b, vec &c) {
  vec ab = b - a, ac = c - a;
  long double o = ab.cross(ac);
  if (o > 0)
    return 1; // CCW
  if (o < 0)
    return -1; // CW
  if (ab.dot(ac) < 0) {
    return 2; // C-A-B
  } else {
    if (ab.dot(ab) < ac.dot(ac)) {
      return -2; // A-B-C
    } else {
      return 0; // A-C-B
    }
  }
}

vec interSection(vec &p0, vec &p1, vec &p2, vec &p3) {
  vec p23 = p3 - p2, p02 = p2 - p0, p01 = p1 - p0;
  double k = p23.cross(p02) / p23.cross(p01);
  return p0 + p01 * k;
}

int isInterSection(vec &p0, vec &p1, vec &p2,
                   vec &p3) { //??????p[0]p[1]??¨p[2]p[3]
  switch (ccw(p0, p1, p2) * ccw(p0, p1, p3)) {
  case -1:
  case -4:
    break;
  case 0:
    return ~(ccw(p0, p1, p2) | ccw(p0, p1, p3))
               ? 2
               : -1; //?§???§??\\??????????????´???
  default:
    return 0;
  }
  switch (ccw(p2, p3, p0) * ccw(p2, p3, p1)) {
  case -1:
  case -4:
  case 0:
    break;
  default:
    return 0;
  }
  return 1;
}

typedef pair<vec, int> P;

struct Section {
  vec v1, v2;   //?§??????¨??????
  vector<P> cp; //??????(????????????????????¨?????????)
};

int main(void) {
  int N;
  vector<Section> S;
  cin >> N;
  while (N--) {
    S.clear();
    Section line;
    cin >> line.v1.x >> line.v1.y >> line.v2.x >> line.v2.y;
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
      vec v1, v2;
      int o, l;
      cin >> v1.x >> v1.y >> v2.x >> v2.y >> o >> l;
      if (isInterSection(v1, v2, line.v1, line.v2)) {
        line.cp.push_back(
            P(interSection(v1, v2, line.v1, line.v2), o ? l : l ^ 1));
      }
    }
    sort(line.cp.begin(), line.cp.end());
    int c = 0, h = line.cp[0].second;
    for (int i = 1; i < line.cp.size(); i++) {
      if (h != line.cp[i].second) {
        c++;
        h = line.cp[i].second;
      }
    }
    cout << c << endl;
  }
  return 0;
}