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

#include <bits/stdc++.h>
using namespace std;

typedef ostringstream OSS;
typedef istringstream ISS;

typedef long long LL;
typedef pair<int, int> PII;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<string> VS;
typedef vector<VS> VVS;
typedef vector<bool> VB;
typedef vector<VB> VVB;
typedef vector<PII> VPII;

#define fst first
#define snd second
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(x) (x).begin(), (x).end()
#define RANGE(x, y, maxX, maxY)                                                \\
  (0 <= (x) && 0 <= (y) && (x) < (maxX) && (y) < (maxY))
#define DUMP(x) cerr << #x << " = " << (x) << endl

template <typename T> inline T fromString(const string &s) {
  T res;
  ISS iss(s);
  iss >> res;
  return res;
};
template <typename T> inline string toString(const T &a) {
  OSS oss;
  oss << a;
  return oss.str();
};

const int INF = 0x3f3f3f3f;
const LL INFL = 0x3f3f3f3f3f3f3f3fLL;
const int DX[] = {1, 0, -1, 0}, DY[] = {0, -1, 0, 1};
#include <algorithm>
#include <cmath>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

const double EPS = 1e-9;

double add(double a, double b) {
  if (abs(a + b) < EPS * (abs(a) + abs(b)))
    return 0;
  return a + b;
}

struct P {
  double x, y;
  P(){};
  P(double x_, double y_) : x(x_), y(y_) {}

  P operator+(P p) { return P(add(x, p.x), add(y, p.y)); }

  P operator-(P p) { return P(add(x, -p.x), add(y, -p.y)); }

  P operator*(double d) { return P(x * d, y * d); }

  P operator/(double d) { return P(x / d, y / d); }

  bool operator<(const P &p) const { return x < p.x + EPS; }

  // ??????
  double dot(P p) { return add(x * p.x, y * p.y); }

  // ??????
  double det(P p) { return add(x * p.y, -y * p.x); }
};

// ??´?????¨??´????????????
P intersection(P p1, P p2, P q1, P q2) {
  return p1 + (p2 - p1) * ((q2 - q1).det(q1 - p1) / (q2 - q1).det(p2 - p1));
}

// ???????????????????????????
bool on_seg(P p1, P p2, P q) {
  return (p1 - q).det(p2 - q) == 0 && (p1 - q).dot(p2 - q) <= 0;
}

//////////////////////

struct Line {
  P s, t;
  bool owner;
  bool layer;

  bool operator<(const Line &l) const { return s < l.s; }
};

void solve() {
  // input
  P a, b;
  cin >> a.x >> a.y >> b.x >> b.y;

  int n;
  cin >> n;

  vector<Line> lines(n);
  for (auto &l : lines) {
    cin >> l.s.x >> l.s.y >> l.t.x >> l.t.y >> l.owner >> l.layer;
  }

  // ??????????±???????
  vector<pair<P, Line>> ps;
  for (auto &l : lines) {
    P p = intersection(a, b, l.s, l.t);
    if (on_seg(a, b, p) && on_seg(l.s, l.t, p)) {
      ps.EB(p, l);
    }
  }

  sort(ALL(ps));

  // ??´???????????????
  int cnt = 0;

  if (!ps.size()) {
    cout << cnt << endl;
    return;
  }

  bool current = ps[0].snd.layer ? false : true;
  if (ps[0].snd.owner) {
    current = !current;
  }

  for (int i = 1; i < (int)ps.size(); i++) {
    bool expect = ps[i].snd.layer ? false : true;
    if (ps[i].snd.owner) {
      expect = !expect;
    }

    if (expect != current) {
      current = expect;
      cnt++;
    }
  }

  // ??????
  cout << cnt << endl;
}

int main(void) {
  int T;
  cin >> T;

  for (int i = 0; i < T; i++) {
    solve();
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef ostringstream OSS;
typedef istringstream ISS;

typedef long long LL;
typedef pair<int, int> PII;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<string> VS;
typedef vector<VS> VVS;
typedef vector<bool> VB;
typedef vector<VB> VVB;
typedef vector<PII> VPII;

#define fst first
#define snd second
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(x) (x).begin(), (x).end()
#define RANGE(x, y, maxX, maxY)                                                \\
  (0 <= (x) && 0 <= (y) && (x) < (maxX) && (y) < (maxY))
#define DUMP(x) cerr << #x << " = " << (x) << endl

template <typename T> inline T fromString(const string &s) {
  T res;
  ISS iss(s);
  iss >> res;
  return res;
};
template <typename T> inline string toString(const T &a) {
  OSS oss;
  oss << a;
  return oss.str();
};

const int INF = 0x3f3f3f3f;
const LL INFL = 0x3f3f3f3f3f3f3f3fLL;
const int DX[] = {1, 0, -1, 0}, DY[] = {0, -1, 0, 1};
#include <algorithm>
#include <cmath>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

const double EPS = 1e-9;

double add(double a, double b) {
  if (abs(a + b) < EPS * (abs(a) + abs(b)))
    return 0;
  return a + b;
}

struct P {
  double x, y;
  P(){};
  P(double x_, double y_) : x(x_), y(y_) {}

  P operator+(P p) { return P(add(x, p.x), add(y, p.y)); }

  P operator-(P p) { return P(add(x, -p.x), add(y, -p.y)); }

  P operator*(double d) { return P(x * d, y * d); }

  P operator/(double d) { return P(x / d, y / d); }

  bool operator<(const P &p) const { return x < p.x + EPS; }

  // ??????
  double dot(P p) { return add(x * p.x, y * p.y); }

  // ??????
  double det(P p) { return add(x * p.y, -y * p.x); }
};

// ??´?????¨??´????????????
P intersection(P p1, P p2, P q1, P q2) {
  return p1 + (p2 - p1) * ((q2 - q1).det(q1 - p1) / (q2 - q1).det(p2 - p1));
}

// ???????????????????????????
bool on_seg(P p1, P p2, P q) {
  return (p1 - q).det(p2 - q) == 0 && (p1 - q).dot(p2 - q) <= 0;
}

//////////////////////

struct Line {
  P s, t;
  bool owner;
  bool layer;

  bool operator<(const Line &l) const { return s < l.s; }
};

void solve() {
  // input
  P a, b;
  cin >> a.x >> a.y >> b.x >> b.y;

  int n;
  cin >> n;

  vector<Line> lines(n);
  for (auto &l : lines) {
    cin >> l.s.x >> l.s.y >> l.t.x >> l.t.y >> l.owner >> l.layer;
  }

  // ??????????±???????
  vector<pair<P, Line>> ps;
  for (auto &l : lines) {
    P p = intersection(a, b, l.s, l.t);
    if (on_seg(a, b, p) && on_seg(l.s, l.t, p)) {
      ps.EB(p, l);
    }
  }

  sort(ALL(ps));

  // ??´???????????????
  int cnt = 0;

  bool current = ps[0].snd.layer ? false : true;
  if (ps[0].snd.owner) {
    current = !current;
  }

  for (int i = 1; i < (int)ps.size(); i++) {
    bool expect = ps[i].snd.layer ? false : true;
    if (ps[i].snd.owner) {
      expect = !expect;
    }

    if (expect != current) {
      current = expect;
      cnt++;
    }
  }

  // ??????
  cout << cnt << endl;
}

int main(void) {
  int T;
  cin >> T;

  for (int i = 0; i < T; i++) {
    //		cout << "case: " << i << endl;
    solve();
    //		cout << endl;
  }

  return 0;
}