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



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

using namespace std;

typedef complex<double> P;

#define EPS (1e-9)

double cross(P a, P b) { return (a.real() * b.imag() - a.imag() * b.real()); }

int is_intersected(P a1, P a2, P b1, P b2) {
  return ((cross(a2 - a1, b1 - a1) * cross(a2 - a1, b2 - a1) < EPS) &&
          (cross(b2 - b1, a1 - b1) * cross(b2 - b1, a2 - b1) < EPS));
}

P s[101], t[101];
int c[101], l[101];

struct intersection {
  double t; // parameter t
  int n;    // index of intersected line

  bool operator<(const intersection &a) const { return (t < a.t); }
};

int main() {
  int N;
  cin >> N;
  while (N--) {
    P a, b;
    cin >> a.real() >> a.imag();
    cin >> b.real() >> b.imag();

    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
      cin >> s[i].real() >> s[i].imag();
      cin >> t[i].real() >> t[i].imag();
      cin >> c[i] >> l[i];
    }

    vector<intersection> is;
    for (int i = 0; i < n; i++) {
      if (!is_intersected(a, b, s[i], t[i])) {
        continue;
      }
      if (abs(cross(b - a, t[i] - s[i])) < EPS) {
        continue;
      }

      P x = t[i] - s[i];
      double d1 = abs(cross(x, a - s[i]));
      double d2 = abs(cross(x, b - s[i]));
      double t = d1 / (d1 + d2);

      intersection tmp = {t, i};
      is.push_back(tmp);
    }

    sort(is.begin(), is.end());

    int curr; // current status: up (=1) or down (=0)
    if (is.size() > 0) {
      int idx = is[0].n;
      curr = (c[idx] == 1 ? l[idx] : !l[idx]);
    }

    int cnt = 0;
    for (int i = 1; i < is.size(); i++) {
      int idx = is[i].n;
      if ((c[idx] == 1 && curr != l[idx]) || (c[idx] == 0 && curr == l[idx])) {
        curr = !curr;
        cnt++;
      }
    }

    cout << cnt << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

#define EPS (1e-10)
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))

typedef complex<double> P;
typedef long long ll;

const int MAX_SIZE = 10000;

// àÏ
double dot(P a, P b) { return (a.real() * b.real() + a.imag() * b.imag()); }
// OÏ
double cross(P a, P b) { return (a.real() * b.imag() - a.imag() * b.real()); }
// _cª¼üabãÉ é©È¢©
int is_point_on_line(P a, P b, P c) { return EQ(cross(b - a, c - a), 0.0); }
// 2¼üÌ¼s»è
int is_orthogonal(P a1, P a2, P b1, P b2) {
  return EQ(dot(a1 - a2, b1 - b2), 0.0);
}
// 2¼üÌ½s»è
int is_parallel(P a1, P a2, P b1, P b2) {
  return EQ(cross(a1 - a2, b1 - b2), 0.0);
}
// _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) {
  return (cross(a2 - a1, b1 - a1) * cross(a2 - a1, b2 - a1) < EPS) &&
         (cross(b2 - b1, a1 - b1) * cross(b2 - b1, a2 - b1) < EPS);
}
// a1,a2ð[_Æ·éüªÆb1,b2ð[_Æ·éüªÌð_vZ
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;
}
// a1,a2ðÊé¼üÆb1,b2ðÊé¼üÌð·»è
int is_intersected_l(P a1, P a2, P b1, P b2) {
  return !EQ(cross(a1 - a2, b1 - b2), 0.0);
}
// a1,a2ðÊé¼üÆb1,b2ðÊé¼üÌð_vZ
P intersection_l(P a1, P a2, P b1, P b2) {
  P a = a2 - a1;
  P b = b2 - b1;
  return a1 + a * cross(b, b1 - a1) / cross(b, a);
}
/*
~üÆüªÌð·»è
*/
bool isCircleCrossLine(P a, P b, P c, double r) {
  double d1 = abs(a - c);
  double d2 = abs(b - c);
  if (d1 < r && d2 < r) {
    return false;
  }
  double d = distance_ls_p(a, b, c);
  if (EQ(d, r) || d < r) {
    return true;
  } else
    return false;
}
// Op`ÌàÉ_ª é©Ç¤©
// OÏÌ³ª·×Ä¯¶ÈçàÉ_ è
bool isInTriangle(P p1, P p2, P p3, P s) {
  P a = p1 - s;
  P b = p2 - s;
  P c = p3 - s;
  return ((cross(a, b) > 0 && cross(b, c) > 0 && cross(c, a) > 0) ||
          (cross(a, b) < 0 && cross(b, c) < 0 && cross(c, a) < 0));
}
// Op`ÌÊÏðÀW©çvZ
double calcAreaOfTriangle(P a, P b, P c) {
  return abs((b.real() - a.real()) * (c.imag() - a.imag()) -
             (c.real() - a.real()) * (b.imag() - a.imag())) /
         2;
}
// ^¦çê½~ÌÍÍàÉ_ª¶Ý·é©Ç¤©
bool isContainingDot(P c, double r, P a) {
  if (((c.real() - a.real()) * (c.real() - a.real()) +
           (c.imag() - a.imag()) * (c.imag() - a.imag()) <
       r * r) ||
      EQ((c.real() - a.real()) * (c.real() - a.real()) +
             (c.imag() - a.imag()) * (c.imag() - a.imag()),
         r * r)) {
    return true;
  } else
    return false;
}
// ½p`ÌÊÏö®
double calcPolygonArea(vector<P> p) {
  double sum = 0;
  for (int i = 0; i < p.size(); i++) {
    sum += cross(p[i], p[(i + 1) % (p.size())]);
  }
  return sum / 2;
}

struct edgeInfo {
  P a;
  P b;
  int o;
  int l;
};

P p1;
P p2;

struct Point {
  P p;
  int l;
  int o;

  bool operator<(const Point &pp) const {
    double dist1 = abs(p1 - pp.p);
    double dist2 = abs(p1 - this->p);
    return dist2 < dist1;
  }
};

int main() {
  int data;
  cin >> data;
  while (data--) {
    int x, y;
    cin >> x >> y;
    p1 = P(x, y);
    cin >> x >> y;
    p2 = P(x, y);
    int n;
    cin >> n;
    vector<edgeInfo> edges;
    for (int i = 0; i < n; i++) {
      int xs, ys, xt, yt, o, l;
      cin >> xs >> ys >> xt >> yt >> o >> l;
      edgeInfo e;
      e.a = P(xs, ys);
      e.b = P(xt, yt);
      e.l = l;
      e.o = o;
      edges.push_back(e);
    }
    vector<Point> crossPoint;
    // ð_ðßé
    for (int i = 0; i < n; i++) {
      if (is_intersected_ls(edges[i].a, edges[i].b, p1, p2)) {
        P res = intersection_ls(edges[i].a, edges[i].b, p1, p2);
        Point pp;
        pp.l = edges[i].l;
        pp.o = edges[i].o;
        pp.p = res;
        crossPoint.push_back(pp);
      }
    }
    // ð_ðp1Éß¢ÔÉ\\[g
    sort(crossPoint.begin(), crossPoint.end());

    // »ÝÌ©ªÌHüîñ(1:nã,0:nº)
    int sit;
    if (crossPoint[0].l == 1) {
      if (crossPoint[0].o == 1) {
        sit = 1;
      } else {
        sit = 0;
      }
    } else {
      if (crossPoint[0].o == 1) {
        sit = 0;
      } else {
        sit = 1;
      }
    }
    int cnt = 0;
    // ð_ðp1Éß¢ÔÉ
    for (int i = 1; i < crossPoint.size(); i++) {
      if (crossPoint[i].o == 1) {
        if (crossPoint[i].l == 0) {
          if (sit != 0) {
            sit = 0;
            cnt++;
          }
        } else {
          if (sit != 1) {
            sit = 1;
            cnt++;
          }
        }
      } else {
        if (crossPoint[i].l == 0) {
          if (sit != 1) {
            sit = 1;
            cnt++;
          }
        } else {
          if (sit != 0) {
            sit = 0;
            cnt++;
          }
        }
      }
    }
    cout << cnt << endl;
  }
}