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

#include <algorithm>
#include <complex>
#include <iostream>
#include <tuple>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define clr(n) memset(n, 0, sizeof(n))

typedef complex<double> P;
typedef pair<P, P> L;

const double EPS = 1e-9;
const int INF = 1e9 + 10;

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

double cross(P a, P b) { return (conj(a) * b).Y; }
double dist(P a, P b) {
  return (a.X - b.X) * (a.X - b.X) + (a.Y - b.Y) * (a.Y - b.Y);
}
bool isPointOnLine(P a, P b, P c) {
  return abs(a - c) + abs(c - b) < abs(a - b) + EPS;
}
P crosspointLL(P a1, P a2, P b1, P b2) {
  double d1 = cross(b2 - b1, b1 - a1);
  double d2 = cross(b2 - b1, a2 - a1);
  P ret;
  if (EQ(d1, 0) && EQ(d2, 0)) {
    ret = a1;
  } else if (EQ(d2, 0))
    return P(INF, INF);
  ret = a1 + d1 / d2 * (a2 - a1);
  if (isPointOnLine(b1, b2, ret) && isPointOnLine(a1, a2, ret)) {
    return ret;
  } else {
    return P(INF, INF);
  }
}
typedef tuple<P, bool, bool> pbb;

int dp[102][2];

signed main() {

  int T;
  cin >> T;
  rep(_, T) {
    int xa, ya, xb, yb, n;
    cin >> xa >> ya >> xb >> yb;
    P A(xa, ya), B(xb, yb);
    cin >> n;
    vector<pbb> vp;
    rep(i, n) {
      int xs, ys, xt, yt, o, l;
      cin >> xs >> ys >> xt >> yt >> o >> l;
      P s(xs, ys), t(xt, yt);
      if (crosspointLL(s, t, A, B) != P(INF, INF)) {
        vp.push_back(pbb(crosspointLL(s, t, A, B), o, l));
      }
    }
    sort(vp.begin(), vp.end(), [&](pbb &t0, pbb &t1) {
      return dist(A, get<0>(t0)) < dist(A, get<0>(t1));
    });
    rep(i, 102) rep(j, 2) dp[i][j] = INF;
    dp[0][0] = dp[0][1] = 0;
    rep(i, vp.size()) {
      int o = get<1>(vp[i]), l = get<2>(vp[i]);
      rep(j, 2) {
        if (o == 1) {
          if (j == l) {
            dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
          } else {
            dp[i + 1][1 - j] = min(dp[i + 1][1 - j], dp[i][j] + 1);
          }
        } else if (o == 0) {
          if (j == l) {
            dp[i + 1][1 - j] = min(dp[i + 1][1 - j], dp[i][j] + 1);
          } else {
            dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
          }
        }
      }
    }
    int ans = INF;
    rep(j, 2) { ans = min(dp[vp.size()][j], ans); }
    cout << ans << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <ccomplex>
#include <cstring>
#include <iostream>
#include <tuple>
#include <vector>

#define For(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) For(i, 0, n)
#define clr(n) memset(n, 0, sizeof n)

using namespace std;

typedef long double d;
typedef complex<d> P;
typedef pair<P, P> L;
const d EPS = 1e-9;

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

d dot(P a, P b) { return (conj(a) * b).X; }

d cross(P a, P b) { return (conj(a) * b).Y; }

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) < -EPS)
    return 2;
  if (norm(b) < norm(c))
    return -2;
  return 0;
}

bool isecLP(P a1, P a2, P b) { return abs(ccw(a1, a2, b)) != 1; }

bool isecLL(P a1, P a2, P b1, P b2) {
  return !isecLP(a2 - a1, b2 - b1, 0) || isecLP(a1, b1, b2);
}

bool isecLL(L l1, L l2) {
  return isecLL(l1.first, l1.second, l2.first, l2.second);
}

const int INF = 1e9;

P crosspointLL(P a1, P a2, P b1, P b2) {
  d d1 = cross(b2 - b1, b1 - a1);
  d d2 = cross(b2 - b1, a2 - a1);
  if (EQ(d1, 0) && EQ(d2, 0))
    return a1;
  if (EQ(d2, 0))
    return P(INF, INF);
  return a1 + d1 / d2 * (a2 - a1);
}
P crosspointLL(L l1, L l2) {
  return crosspointLL(l1.first, l1.second, l2.first, l2.second);
}

typedef tuple<P, int, int> T;

int main() {

  int N;
  cin >> N;
  rep(_, N) {
    d xa, ya, xb, yb;
    cin >> xa >> ya >> xb >> yb;
    L sl(P(xa, ya), P(xb, yb));

    int n;
    cin >> n;

    vector<L> vL;
    vector<int> vo;
    vector<int> vl;

    rep(i, n) {
      d xs, ys, xt, yt, o, l;
      cin >> xs >> ys >> xt >> yt >> o >> l;

      vL.push_back(L(P(xs, ys), P(xt, yt)));
      vo.push_back(o);
      vl.push_back(l);
    }

    vector<T> vp;

    rep(i, n) {
      if (isecLL(sl, vL[i])) {
        vp.push_back(T(crosspointLL(sl, vL[i]), vo[i], vl[i]));
      }
    }

    bool f = false;
    while (!f) {
      f = true;
      rep(i, vp.size() - 1) {
        auto p1 = get<0>(vp[i]);
        auto p2 = get<0>(vp[i + 1]);
        d dist1 = (p1.X - sl.first.X) * (p1.X - sl.first.X) +
                  (p1.Y - sl.first.Y) * (p1.Y - sl.first.Y);
        d dist2 = (p2.X - sl.first.X) * (p2.X - sl.first.X) +
                  (p2.Y - sl.first.Y) * (p2.Y - sl.first.Y);

        if (dist1 > dist2) {
          swap(vp[i], vp[i + 1]);
          f = false;
        }
      }
    }

    int ans = INF;
    rep(i, 2) {
      int val = 0;
      int sf = i;
      rep(j, vp.size()) {
        if (get<1>(vp[j]) == 1) {
          if (get<2>(vp[j]) != sf) {
            val++;
            sf = 1 - sf;
          }
        } else {
          if (get<2>(vp[j]) == sf) {
            val++;
            sf = 1 - sf;
          }
        }
      }
      ans = min(val, ans);
    }
    cout << ans << endl;
  }

  return 0;
}