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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define SQ(x) ((x) * (x))
#define Mod(x, mod) (((x)+(mod)%(mod))
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define MP make_pair
#define PB push_back
#define Fi first
#define Se second
#define MOD 1000000007

typedef complex<double> P;
typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;
const double EPS = 1e-9;
const double INF = 1e12;
// ??????(dot product) : ab = |a||b|cos theta
double dot(P a, P b) { return (a.real() * b.real() + a.imag() * b.imag()); }
// ??????(cross product) : axb = |a||b|sin theta
double cross(P a, P b) { return (a.real() * b.imag() - a.imag() * b.real()); }

// 2??´????????´?????????  dot(a, b) = 0
int is_orthogonal(P a1, P a2, P b1, P b2) {
  return EQ(dot(a1 - a2, b1 - b2), 0.0);
}
// 2??´?????????????????? cross(a, b) = 0
int is_parallel(P a1, P a2, P b1, P b2) {
  return EQ(cross(a1 - a2, b1 - b2), 0.0);
}

// ???c?????´???a,b????????????????????????
int is_point_on_line(P a, P b, P c) { return EQ(cross(b - a, c - a), 0.0); }

// ???c?????????a,b????????????????????????
int is_point_on_line_seg(P a, P b, P c) {
  // |a - c| + |c - b| <= |a - b|???????????????
  return (abs(a - c) + abs(c - b) < abs(a - b) + EPS);
}

// ???a,b???????????´?????¨???c??¨????????¢
double distance_l_p(P a, P b, P c) {
  return abs(cross(b - a, c - a)) / abs(b - a);
}

// ???a,b???????????¨??????????????¨???c??¨????????¢
double distance_ls_p(P a, P b, P c) {
  if (dot(b - a, c - a) < EPS)
    return abs(c - a);
  if (dot(a - b, c - b) < EPS)
    return abs(c - b);
  return abs(cross(b - a, c - a)) / abs(b - a);
}

// a1,a2???????????¨??????????????¨b1,b2???????????¨???????????????????????????
int is_intersected_ls(P a1, P a2, P b1, P b2, double c) {
  return (cross(a2 - a1, b1 - a1) * cross(a2 - a1, b2 - a1) < c) &&
         (cross(b2 - b1, a1 - b1) * cross(b2 - b1, a2 - b1) < c);
}

// a1,a2???????????¨??????????????¨b1,b2???????????¨????????¨??????????????????????¨????
P intersection_ls(P a1, P a2, P b1, P b2) {
  P b = b2 - b1;
  double d1 = abs(cross(b, a1 - b1));
  double d2 = abs(cross(b, a2 - b1));
  double t = d1 / (d1 + d2);

  return a1 + (a2 - a1) * t;
}

typedef pair<P, pi> S;
P a(0, 0), b(0, 0);

bool comp(const S &ls, const S &rs) {
  P l = ls.first, r = rs.first;
  return abs(l - a) < abs(r - a);
}

int in[8];

int solve() {
  rep(i, 4) cin >> in[i];
  a = P(in[0], in[1]), b = P(in[2], in[3]);
  int N;
  cin >> N;
  vector<S> V;
  rep(i, N) {
    rep(j, 6) cin >> in[j];
    P c(in[0], in[1]), d(in[2], in[3]);
    if (is_intersected_ls(a, b, c, d, 0)) {
      P p = intersection_ls(a, b, c, d);
      V.push_back(make_pair(p, pi(in[4], in[5])));
    }
  }

  if (V.size() == 0)
    return 0;
  //  cout << V.size() << endl;
  sort(V.begin(), V.end(), comp);
  int res = 0;
  int pos = (~(V[0].second.first ^ V[0].second.second)) & 1;
  for (int i = 1; i < V.size(); i++) {
    int o = V[i].second.first & 1, l = V[i].second.second & 1;
    res += (pos != ((~(o ^ l)) & 1));
    pos = (~(o ^ l)) & 1;
  }
  return res;
}

int main() {
  int T;
  cin >> T;
  rep(i, T) cout << solve() << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define SQ(x) ((x) * (x))
#define Mod(x, mod) (((x)+(mod)%(mod))
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define MP make_pair
#define PB push_back
#define Fi first
#define Se second
#define MOD 1000000007

typedef complex<double> P;
typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;
const double EPS = 1e-8;
const double INF = 1e12;
// ??????(dot product) : ab = |a||b|cos theta
double dot(P a, P b) { return (a.real() * b.real() + a.imag() * b.imag()); }
// ??????(cross product) : axb = |a||b|sin theta
double cross(P a, P b) { return (a.real() * b.imag() - a.imag() * b.real()); }

// 2??´????????´?????????  dot(a, b) = 0
int is_orthogonal(P a1, P a2, P b1, P b2) {
  return EQ(dot(a1 - a2, b1 - b2), 0.0);
}
// 2??´?????????????????? cross(a, b) = 0
int is_parallel(P a1, P a2, P b1, P b2) {
  return EQ(cross(a1 - a2, b1 - b2), 0.0);
}

// ???c?????´???a,b????????????????????????
int is_point_on_line(P a, P b, P c) { return EQ(cross(b - a, c - a), 0.0); }

// ???c?????????a,b????????????????????????
int is_point_on_line_seg(P a, P b, P c) {
  // |a - c| + |c - b| <= |a - b|???????????????
  return (abs(a - c) + abs(c - b) < abs(a - b) + EPS);
}

// ???a,b???????????´?????¨???c??¨????????¢
double distance_l_p(P a, P b, P c) {
  return abs(cross(b - a, c - a)) / abs(b - a);
}

// ???a,b???????????¨??????????????¨???c??¨????????¢
double distance_ls_p(P a, P b, P c) {
  if (dot(b - a, c - a) < EPS)
    return abs(c - a);
  if (dot(a - b, c - b) < EPS)
    return abs(c - b);
  return abs(cross(b - a, c - a)) / abs(b - a);
}

// a1,a2???????????¨??????????????¨b1,b2???????????¨???????????????????????????
int is_intersected_ls(P a1, P a2, P b1, P b2, double c) {
  return (cross(a2 - a1, b1 - a1) * cross(a2 - a1, b2 - a1) < c) &&
         (cross(b2 - b1, a1 - b1) * cross(b2 - b1, a2 - b1) < c);
}

// a1,a2???????????¨??????????????¨b1,b2???????????¨????????¨??????????????????????¨????
P intersection_ls(P a1, P a2, P b1, P b2) {
  P b = b2 - b1;
  double d1 = abs(cross(b, a1 - b1));
  double d2 = abs(cross(b, a2 - b1));
  double t = d1 / (d1 + d2);

  return a1 + (a2 - a1) * t;
}

typedef pair<P, pi> S;

bool comp(const S &ls, const S &rs) {
  P l = ls.first, r = rs.first;
  return l.real() == r.real() ? l.imag() < r.imag() : l.real() < r.real();
}

int solve() {
  int in[6];
  rep(i, 4) cin >> in[i];
  P a(in[0], in[1]), b(in[2], in[3]);
  int N;
  cin >> N;
  vector<S> V;
  rep(i, N) {
    rep(j, 6) cin >> in[j];
    P c(in[0], in[1]), d(in[2], in[3]);
    if (is_intersected_ls(a, b, c, d, EPS)) {
      P p = intersection_ls(a, b, c, d);
      V.push_back(make_pair(p, pi(in[4], in[5])));
    }
  }
  sort(V.begin(), V.end(), comp);
  int res = 0;
  unsigned int pos = (~(V[0].second.first ^ V[0].second.second)) & 1;
  //  cout << pos << endl;
  for (int i = 1; i < V.size(); i++) {
    int o = V[i].second.first, l = V[i].second.second;
    //    cout << o << " " << l << endl;
    res += (pos != ((~(o ^ l)) & 1));
    pos = (~(o ^ l)) & 1;
    //    cout << pos << endl;
  }
  return res;
}

int main() {
  int T;
  cin >> T;
  rep(i, T) cout << solve() << endl;
  return 0;
}