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


#include <bits/stdc++.h>
typedef long long LL;
#define SORT(c) sort((c).begin(), (c).end())

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
using namespace std;
vector<pair<pair<double, double>, bool>> cp;
pair<double, double> relv(pair<double, double> a, pair<double, double> b) {
  return make_pair(a.first - b.first, a.second - b.second);
}
double cross(pair<double, double> va, pair<double, double> vb) {
  return va.first * vb.second - va.second * vb.first;
}
void crosspoint(pair<double, double> a, pair<double, double> b,
                pair<double, double> c, pair<double, double> d, bool e) {
  auto ab = relv(a, b), cd = relv(c, d);
  if (!(cross(ab, relv(a, c)) * cross(ab, relv(a, d)) < 0 &&
        cross(cd, relv(c, a)) * cross(cd, relv(c, b)) < 0))
    return;
  double midrat =
      cross(cd, relv(c, a)) / (cross(cd, relv(c, a)) + cross(cd, relv(b, d)));
  cp.push_back(make_pair(
      make_pair(a.first + midrat * ab.first, a.second + midrat * ab.second),
      e));
}

int main(void) {
  int n;
  cin >> n;
  REP(i, n) {
    int sx, sy, ex, ey;
    cin >> sx >> sy >> ex >> ey;
    pair<double, double> s, e;
    s = make_pair(sx, sy), e = make_pair(ex, ey);
    cp.clear();
    int m;
    cin >> m;
    REP(i, m) {
      int x[2], y[2], p, q;
      cin >> x[0] >> y[0] >> x[1] >> y[1] >> p >> q;
      crosspoint(s, e, make_pair(x[0], y[0]), make_pair(x[1], y[1]), p == q);
    }
    SORT(cp);
    int answer = 0;
    REP(i, (int)cp.size() - 1) if (cp[i].second != cp[i + 1].second)++ answer;
    cout << answer << endl;
  }
}

Error: WA

Fixed submission:


#include <bits/stdc++.h>
typedef long long LL;
#define SORT(c) sort((c).begin(), (c).end())

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
using namespace std;
vector<pair<pair<double, double>, bool>> cp;
pair<double, double> relv(pair<double, double> a, pair<double, double> b) {
  return make_pair(a.first - b.first, a.second - b.second);
}
double cross(pair<double, double> va, pair<double, double> vb) {
  return va.first * vb.second - va.second * vb.first;
}
void crosspoint(pair<double, double> a, pair<double, double> b,
                pair<double, double> c, pair<double, double> d, bool e) {
  auto ab = relv(a, b), cd = relv(c, d);
  if (!(cross(ab, relv(a, c)) * cross(ab, relv(a, d)) < 0 &&
        cross(cd, relv(c, a)) * cross(cd, relv(c, b)) < 0))
    return;
  if ((cross(cd, relv(c, a)) + cross(cd, relv(b, d))) == 0)
    return;
  double midrat =
      cross(cd, relv(c, a)) / (cross(cd, relv(c, a)) + cross(cd, relv(b, d)));
  cp.push_back(make_pair(
      make_pair(a.first + midrat * ab.first, a.second + midrat * ab.second),
      e));
}

int main(void) {
  cout << fixed << setprecision(10) << endl;
  int n;
  cin >> n;
  REP(i, n) {
    int sx, sy, ex, ey;
    cin >> sx >> sy >> ex >> ey;
    pair<double, double> s, e;
    s = make_pair(sx, sy), e = make_pair(ex, ey);
    cp.clear();
    int m;
    cin >> m;
    REP(i, m) {
      int x[2], y[2], p, q;
      cin >> x[0] >> y[0] >> x[1] >> y[1] >> p >> q;
      crosspoint(s, e, make_pair(x[0], y[0]), make_pair(x[1], y[1]), p == q);
    }
    SORT(cp);
    int answer = 0;
    REP(i, cp.size() - 1) if (cp[i].second != cp[i + 1].second)++ answer;
    cout << answer << endl;
  }
}