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

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

#define DBG cerr << '!' << endl;
#define REP(i, n) for (int(i) = (0); (i) < (n); ++i)
#define rep(i, s, g) for (int(i) = (s); (i) < (g); ++i)
#define rrep(i, s, g) for (int(i) = (s); i >= (g); --(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n)                                                           \\
  {                                                                            \\
    for (int i = 0; i < (n); i++)                                              \\
      cerr << v[i] << ' ';                                                     \\
    cerr << endl << endl;                                                      \\
  }
#define SHOW2d(v, i, j)                                                        \\
  {                                                                            \\
    for (int aaa = 0; aaa < i; aaa++) {                                        \\
      for (int bbb = 0; bbb < j; bbb++)                                        \\
        cerr << v[aaa][bbb] << ' ';                                            \\
      cerr << endl;                                                            \\
    }                                                                          \\
    cerr << endl;                                                              \\
  }
#define ALL(v) v.begin(), v.end()

#define EPS 1e-5
#define INF 1e8

using namespace std;

typedef long long ll;
typedef vector<int> iv;
typedef vector<iv> iiv;
typedef vector<string> sv;

typedef complex<double> point;
long double r;

struct straight {
  point p1, p2;
  straight() {}
  straight(point a, point b) : p1(a), p2(b) {}
};

point GetCrossPoint(straight s1, straight s2) {
  long double x1 = s1.p1.real(), y1 = s1.p1.imag(), x2 = s1.p2.real(),
              y2 = s1.p2.imag();
  long double x3 = s2.p1.real(), y3 = s2.p1.imag(), x4 = s2.p2.real(),
              y4 = s2.p2.imag();

  if ((y2 - y1) * (x4 - x3) == (y4 - y3) * (x2 - x1)) {
    point ret(INF, INF);
    return ret;
  }

  long double x13 = x3 - x1;
  long double y13 = y3 - y1;
  long double z = (x2 - x1) * (y4 - y3) - (y2 - y1) * (x4 - x3);
  r = ((y4 - y3) * x13 - (x4 - x3) * y13) / z;

  double x = x1 + r * (x2 - x1);
  double y = y1 + r * (y2 - y1);
  point ret(x, y);
  return ret;
}

int main() {
  int N;
  cin >> N;
  while (N--) {

    double xa, xb, ya, yb;
    cin >> xa >> ya >> xb >> yb;
    straight par(point(xa, ya), point(xb, yb));
    int n;
    cin >> n;

    vector<pair<double, int>> v;

    REP(i, n) {
      double sx, sy, gx, gy;
      int o, l;
      cin >> sx >> sy >> gx >> gy >> o >> l;

      auto cross = GetCrossPoint(par, straight(point(sx, sy), point(gx, gy)));
      auto tmpx = cross.real();
      auto tmpy = cross.imag();

      if (tmpx == INF)
        continue;

      if (((((tmpx - sx > -EPS) && (gx - tmpx > -EPS)) ||
            ((sx - tmpx > -EPS) && (tmpx - gx > -EPS))) &&
           (((tmpy - sy > -EPS) && (gy - tmpy > -EPS)) ||
            ((sy - tmpy > -EPS) && (tmpy - gy > -EPS)))) &&
          ((((tmpx - xa > -EPS) && (xb - tmpx > -EPS)) ||
            ((xa - tmpx > -EPS) && (tmpx - xb > -EPS))) &&
           (((tmpy - ya > -EPS) && (yb - tmpy > -EPS)) ||
            ((ya - tmpy > -EPS) && (tmpy - yb > -EPS))))) {
        v.PB(MP(r, ((o + l) % 2)));
      }
    }

    sort(ALL(v));

    if (v.size() == 0) {
      cout << 0 << endl;
      continue;
    }

    int ans = 0;
    int now = v[0].SE;

    REP(i, v.size()) {
      if (v[i].SE != now) {
        now = v[i].SE;
        ans++;
      }
    }

    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

#define DBG cerr << '!' << endl;
#define REP(i, n) for (int(i) = (0); (i) < (n); ++i)
#define rep(i, s, g) for (int(i) = (s); (i) < (g); ++i)
#define rrep(i, s, g) for (int(i) = (s); i >= (g); --(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n)                                                           \\
  {                                                                            \\
    for (int i = 0; i < (n); i++)                                              \\
      cerr << v[i] << ' ';                                                     \\
    cerr << endl << endl;                                                      \\
  }
#define SHOW2d(v, i, j)                                                        \\
  {                                                                            \\
    for (int aaa = 0; aaa < i; aaa++) {                                        \\
      for (int bbb = 0; bbb < j; bbb++)                                        \\
        cerr << v[aaa][bbb] << ' ';                                            \\
      cerr << endl;                                                            \\
    }                                                                          \\
    cerr << endl;                                                              \\
  }
#define ALL(v) v.begin(), v.end()

#define EPS 1e-9
#define INF 1e10

using namespace std;

typedef long long ll;
typedef vector<int> iv;
typedef vector<iv> iiv;
typedef vector<string> sv;

typedef complex<double> point;

struct straight {
  point p1, p2;
  straight() {}
  straight(point a, point b) : p1(a), p2(b) {}
};

point GetCrossPoint(straight s1, straight s2) {
  long double x1 = s1.p1.real(), y1 = s1.p1.imag(), x2 = s1.p2.real(),
              y2 = s1.p2.imag();
  long double x3 = s2.p1.real(), y3 = s2.p1.imag(), x4 = s2.p2.real(),
              y4 = s2.p2.imag();

  if ((y2 - y1) * (x4 - x3) == (y4 - y3) * (x2 - x1)) {
    point ret(INF, INF);
    return ret;
  }

  long double x13 = x3 - x1;
  long double y13 = y3 - y1;
  long double z = (x2 - x1) * (y4 - y3) - (y2 - y1) * (x4 - x3);
  if (z == 0)
    return (point(INF, INF));
  long double r = ((y4 - y3) * x13 - (x4 - x3) * y13) / z;

  double x = x1 + r * (x2 - x1);
  double y = y1 + r * (y2 - y1);
  point ret(x, y);
  return ret;
}

int main() {
  int N;
  cin >> N;
  while (N--) {

    double xa, xb, ya, yb;
    cin >> xa >> ya >> xb >> yb;
    straight par(point(xa, ya), point(xb, yb));
    int n;
    cin >> n;

    vector<pair<double, int>> v;

    REP(i, n) {
      double sx, sy, gx, gy;
      int o, l;
      cin >> sx >> sy >> gx >> gy >> o >> l;

      auto cross = GetCrossPoint(par, straight(point(sx, sy), point(gx, gy)));
      auto tmpx = cross.real();
      auto tmpy = cross.imag();

      if (tmpx == INF)
        continue;
      if ((((tmpx - sx > -EPS) && (gx - tmpx > -EPS)) ||
           ((sx - tmpx > -EPS) && (tmpx - gx > -EPS))) &&
          (((tmpy - sy > -EPS) && (gy - tmpy > -EPS)) ||
           ((sy - tmpy > -EPS) && (tmpy - gy > -EPS)))) {
        v.PB(MP(tmpx, (o + l) % 2));
      }
    }

    sort(ALL(v));

    int ans = 0;
    int now = v[0].SE;

    REP(i, n) {
      if (v[i].SE != now) {
        now = v[i].SE;
        ans++;
      }
    }

    cout << ans << endl;
  }
  return 0;
}