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

#include <algorithm>
#include <array>
#include <cassert>
#include <complex>
#include <iostream>
#include <tuple>
#include <vector>
using namespace std;

const double EPS = 1e-10;
template <class T> bool eq(const T &a, const T &b) { return abs(a - b) < EPS; }

typedef complex<double> point;
double cross(const point &a, const point &b) { return imag(conj(a) * b); }
double dot(const point &a, const point &b) { return real(conj(a) * b); }
int ccw(point a, point b, point 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;    // a--c--b on line or a----bc
}

typedef array<point, 2> line;
bool intersectSS(const line &s, const line &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;
}
point crosspoint(const line &l, const line &m) {
  double A = cross(l[1] - l[0], m[1] - m[0]);
  double B = cross(l[1] - l[0], l[1] - m[0]);
  if (eq(A, 0.) && eq(B, 0.))
    return m[0]; // same line
  if (eq(A, 0.))
    assert(false); // !!!PRECONDITION NOT SATISFIED!!!
  return m[0] + B / A * (m[1] - m[0]);
}

line read_line() {
  line res;
  for (auto &p : res) {
    int x, y;
    cin >> x >> y;
    p = point(x, y);
  }
  return res;
}

int solve() {
  line new_road = read_line();
  int n;
  cin >> n;
  typedef tuple<point, bool, bool> pinfo;
  vector<pinfo> points;
  for (int i = 0; i < n; i++) {
    line l = read_line();
    bool own, ground;
    cin >> own >> ground;
    if (intersectSS(new_road, l)) {
      points.emplace_back(crosspoint(new_road, l), own, ground);
    }
  }
  //	cout << points.size() << endl;
  sort(points.begin(), points.end(),
       [&new_road](const pinfo &a, const pinfo &b) {
         return abs(get<0>(a) - new_road[0]) < abs(get<0>(b) - new_road[0]);
       });
  if (points.empty())
    return 0;
  bool cur = get<1>(points[0]) ? get<2>(points[0]) : !get<2>(points[0]);
  int cnt = 0;
  for (int i = 1; i < points.size(); i++) {
    if ((get<1>(points[i]) && cur != get<2>(points[i])) ||
        (!get<1>(points[i]) && cur == get<2>(points[i]))) {
      cur = !cur;
      cnt++;
      //			cout << i << endl;
    }
  }
  return cnt;
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cout << solve() << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <array>
#include <cassert>
#include <complex>
#include <iostream>
#include <tuple>
#include <vector>
using namespace std;

const double EPS = 1e-10;
template <class T> bool eq(const T &a, const T &b) { return abs(a - b) < EPS; }

typedef complex<double> point;
double cross(const point &a, const point &b) { return imag(conj(a) * b); }
double dot(const point &a, const point &b) { return real(conj(a) * b); }
int ccw(point a, point b, point 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;    // a--c--b on line or a----bc
}

typedef array<point, 2> line;
bool intersectSS(const line &s, const line &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;
}
point crosspoint(const line &l, const line &m) {
  double A = cross(l[1] - l[0], m[1] - m[0]);
  double B = cross(l[1] - l[0], l[1] - m[0]);
  if (eq(A, 0.) && eq(B, 0.))
    return m[0]; // same line
  if (eq(A, 0.))
    assert(false); // !!!PRECONDITION NOT SATISFIED!!!
  return m[0] + B / A * (m[1] - m[0]);
}

line read_line() {
  line res;
  for (auto &p : res) {
    int x, y;
    cin >> x >> y;
    p = point(x, y);
  }
  return res;
}

int solve() {
  line new_road = read_line();
  int n;
  cin >> n;
  typedef tuple<point, bool, bool> pinfo;
  vector<pinfo> points;
  for (int i = 0; i < n; i++) {
    line l = read_line();
    bool own, ground;
    cin >> own >> ground;
    if (intersectSS(new_road, l)) {
      points.emplace_back(crosspoint(new_road, l), own, ground);
    }
  }
  //	cout << points.size() << endl;
  sort(points.begin(), points.end(),
       [&new_road](const pinfo &a, const pinfo &b) {
         return abs(get<0>(a) - new_road[0]) < abs(get<0>(b) - new_road[0]);
       });
  bool cur = get<1>(points[0]) ? get<2>(points[0]) : !get<2>(points[0]);
  int cnt = 0;
  for (int i = 1; i < points.size(); i++) {
    if ((get<1>(points[i]) && cur != get<2>(points[i])) ||
        (!get<1>(points[i]) && cur == get<2>(points[i]))) {
      cur = !cur;
      cnt++;
      //			cout << i << endl;
    }
  }
  return cnt;
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cout << solve() << endl;
  }
  return 0;
}