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

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

#define REP2(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define REP(i, n) REP2(i, 0, n)
#define ALL(S) (S).begin(), (S).end()
typedef long long ll;

typedef complex<double> xy_t;
typedef pair<xy_t, xy_t> line_t;
typedef pair<xy_t, double> circle_t;
typedef vector<xy_t> poly_t;

namespace std {
bool operator<(const xy_t &a, const xy_t &b) {
  return a.real() == b.real() ? a.imag() < b.imag() : a.real() < b.real();
}
} // namespace std

const double EPS = 1e-9;

inline bool equal(double a, double b) { return abs(a - b) < EPS; }

istream &operator>>(istream &is, xy_t &p) {
  double x, y;
  is >> x >> y;
  p = xy_t(x, y);
  return is;
}

istream &operator>>(istream &is, line_t &l) {
  return is >> l.first >> l.second;
}

inline double cross(const xy_t &a, const xy_t &b) { return imag(conj(a) * b); }

inline double dot(const xy_t &a, const xy_t &b) { return real(conj(a) * b); }

inline xy_t rotate(const xy_t &a, double theta) {
  return a * polar(1.0, theta);
}

inline xy_t direction(const line_t &line) { return line.second - line.first; }

int ccw(const xy_t &a, xy_t b, xy_t c) {
  b -= a;
  c -= a;
  if (cross(b, c) > +EPS)
    return 1;
  if (cross(b, c) < -EPS)
    return -1;
  if (dot(b, c) < -EPS)
    return 2;
  if (norm(b) < norm(c))
    return -2;
  return 0;
}

int ccw(const line_t &l, const xy_t &a) { return ccw(l.first, l.second, a); }

line_t bisector(const xy_t &a, const xy_t &b) {
  xy_t v = rotate(b - a, M_PI / 2);
  xy_t m = (a + b) * 0.5;
  return line_t(m, m + v);
};

xy_t crosspoint(const line_t &l, const line_t &m) {
  xy_t l_vec = direction(l);
  xy_t m_vec = direction(m);
  double A = cross(l_vec, m_vec);
  assert(!equal(A, 0));
  return m.first + cross(l_vec, l.second - m.first) / A * m_vec;
}

inline size_t next_index(const poly_t &poly, size_t curr) {
  return (curr + 1) % poly.size();
}

inline size_t prev_index(const poly_t &poly, size_t curr) {
  return (curr + poly.size() - 1) % poly.size();
}

poly_t convex_cut(const poly_t &poly, const line_t &line) {
  poly_t new_poly;

  for (size_t i = 0; i < poly.size(); i++) {
    xy_t a = poly[i];
    xy_t b = poly[next_index(poly, i)];

    if (ccw(line, a) != -1)
      new_poly.push_back(a);

    if (ccw(line, a) * ccw(line, b) < 0) {
      new_poly.push_back(crosspoint(line_t(a, b), line));
    }
  }
  return new_poly;
}

double signed_area(const poly_t &poly) {
  double res = 0;
  for (size_t i = 0; i < poly.size(); i++) {
    res += cross(poly[i], poly[next_index(poly, i)]);
  }
  return res * 0.5;
}

double area(const poly_t &poly) { return abs(signed_area(poly)); }

int main() {
  ios::sync_with_stdio(false);
  int N, M;
  while (cin >> N >> M && N + M) {
    poly_t poly(N);
    REP(i, N) { cin >> poly[i]; }

    vector<xy_t> L(M);
    REP(i, M) { cin >> L[i]; }

    REP(i, M) {
      poly_t pi = poly;

      REP(j, M) if (i != j) { pi = convex_cut(pi, bisector(L[i], L[j])); }

      cout << fixed << setprecision(20) << area(pi) << endl;
    }
  }
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

#define REP2(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define REP(i, n) REP2(i, 0, n)
#define ALL(S) (S).begin(), (S).end()
typedef long long ll;

typedef complex<double> xy_t;
typedef pair<xy_t, xy_t> line_t;
typedef pair<xy_t, double> circle_t;
typedef vector<xy_t> poly_t;

namespace std {
bool operator<(const xy_t &a, const xy_t &b) {
  return a.real() == b.real() ? a.imag() < b.imag() : a.real() < b.real();
}
} // namespace std

const double EPS = 1e-9;

inline bool equal(double a, double b) { return abs(a - b) < EPS; }

istream &operator>>(istream &is, xy_t &p) {
  double x, y;
  is >> x >> y;
  p = xy_t(x, y);
  return is;
}

istream &operator>>(istream &is, line_t &l) {
  return is >> l.first >> l.second;
}

inline double cross(const xy_t &a, const xy_t &b) { return imag(conj(a) * b); }

inline double dot(const xy_t &a, const xy_t &b) { return real(conj(a) * b); }

inline xy_t rotate(const xy_t &a, double theta) {
  return a * polar(1.0, theta);
}

inline xy_t direction(const line_t &line) { return line.second - line.first; }

int ccw(const xy_t &a, xy_t b, xy_t c) {
  b -= a;
  c -= a;
  if (cross(b, c) > 0)
    return 1;
  if (cross(b, c) < 0)
    return -1;
  if (dot(b, c) < 0)
    return 2;
  if (norm(b) < norm(c))
    return -2;
  return 0;
}

int ccw(const line_t &l, const xy_t &a) { return ccw(l.first, l.second, a); }

line_t bisector(const xy_t &a, const xy_t &b) {
  xy_t v = rotate(b - a, M_PI / 2);
  xy_t m = (a + b) * 0.5;
  return line_t(m, m + v);
};

xy_t crosspoint(const line_t &l, const line_t &m) {
  xy_t l_vec = direction(l);
  xy_t m_vec = direction(m);
  double A = cross(l_vec, m_vec);
  assert(!equal(A, 0));
  return m.first + cross(l_vec, l.second - m.first) / A * m_vec;
}

inline size_t next_index(const poly_t &poly, size_t curr) {
  return (curr + 1) % poly.size();
}

inline size_t prev_index(const poly_t &poly, size_t curr) {
  return (curr + poly.size() - 1) % poly.size();
}

poly_t convex_cut(const poly_t &poly, const line_t &line) {
  poly_t new_poly;

  for (size_t i = 0; i < poly.size(); i++) {
    xy_t a = poly[i];
    xy_t b = poly[next_index(poly, i)];

    if (ccw(line, a) != -1)
      new_poly.push_back(a);

    if (ccw(line, a) * ccw(line, b) == -1) {
      new_poly.push_back(crosspoint(line_t(a, b), line));
    }
  }
  return new_poly;
}

double signed_area(const poly_t &poly) {
  double res = 0;
  for (size_t i = 0; i < poly.size(); i++) {
    res += cross(poly[i], poly[next_index(poly, i)]);
  }
  return res * 0.5;
}

double area(const poly_t &poly) { return abs(signed_area(poly)); }

int main() {
  ios::sync_with_stdio(false);
  int N, M;
  while (cin >> N >> M && N + M) {
    poly_t poly(N);
    REP(i, N) { cin >> poly[i]; }

    vector<xy_t> L(M);
    REP(i, M) { cin >> L[i]; }

    REP(i, M) {
      poly_t pi = poly;

      REP(j, M) if (i != j) { pi = convex_cut(pi, bisector(L[i], L[j])); }

      cout << area(pi) << endl;
    }
  }
  return 0;
}