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

#include <bits/stdc++.h>

using namespace std;

struct INIT {
  INIT() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed;
    cout << setprecision(10);
  }
} init;

#define rep(i, n) for (auto i = (n)*0; i < n; i++)
#define range(it, v) for (auto &it : v)

#define IL inline
typedef double D;
typedef bool B;
typedef complex<double> P;
#define T tuple
typedef T<P, P> L;
typedef T<P, P> LS;
typedef T<P, D> C;
typedef vector<P> Poly;
const D eps = 1e-8;
#define X real()
#define Y imag()
IL B comp(const P &l, const P &r) {
  return (l.X == r.X) ? l.Y < r.Y : l.X < r.X;
}
IL D dot(P a, P b) { return a.X * b.X + a.Y * b.Y; }
IL D cross(P a, P b) { return a.X * b.Y - a.Y * b.X; }

IL B ccw(P a, P b, P c) {
  c -= b, b -= a;
  return cross(b, c) > eps;
}
IL B cww(P a, P b, P c) {
  c -= b, b -= a;
  return cross(b, c) > -eps;
}

IL D signed_distance_P_L(P p, L l) {
  P a, b, c = p;
  tie(a, b) = l;
  return cross(b - a, c - a) / abs(b - a);
}
IL B isintersected_L_LS(L l, LS ls) {
  P a, b, c, d;
  tie(a, b) = l;
  tie(c, d) = ls;
  return cww(a, b, c) == ccw(b, a, d) || cww(a, b, d) == ccw(b, a, c);
}
IL P cross_point_L_L(L p, L q) {
  P a, b;
  tie(a, b) = p;
  D d1 = signed_distance_P_L(a, q);
  D d2 = signed_distance_P_L(b, q);
  return (a * d2 - b * d1) / (d2 - d1);
}
IL Poly convex_hull(Poly p) {
  int n = p.size();
  if (n == 0)
    return Poly();
  Poly res(2 * n);
  int k = 0;
  sort(p.begin(), p.end(), comp);
  rep(i, n) {
    while (k > 1 && !ccw(res[k - 2], res[k - 1], p[i]))
      k--;
    res[k++] = p[i];
  }
  int t = k;
  for (int i = n - 2; i >= 0; i--) {
    while (k > t && !ccw(res[k - 2], res[k - 1], p[i]))
      k--;
    res[k++] = p[i];
  }
  res.resize(k - 1);
  return res;
}
IL D convex_area(Poly &l) {
  D res = 0;
  int n = l.size();
  for (int i = 2; i < n; i++)
    res += abs(cross(l[i - 1] - l[0], l[i] - l[i - 1])) / 2.0;
  return res;
}

int main() {
  int n;
  cin >> n;
  Poly xy(n);
  rep(i, n) {
    double x, y;
    cin >> x >> y;
    xy[i] = P(x, y);
  }
  int q;
  cin >> q;
  while (q--) {
    double x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    P p1(x1, y1), p2(x2, y2);
    Poly pp;
    range(it, xy) if (cww(p1, p2, it)) pp.push_back(it);
    rep(i, n) {
      int j = (i + 1) % n;
      if (isintersected_L_LS(L(p1, p2), LS(xy[i], xy[j])))
        pp.push_back(cross_point_L_L(L(p1, p2), LS(xy[i], xy[j])));
    }
    // range(it,pp)cout<<it<<endl;cout<<endl;
    auto ch = convex_hull(pp);
    // range(it,ch)cout<<it<<endl;cout<<endl;
    cout << convex_area(ch) << endl;
  }
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

struct INIT {
  INIT() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed;
    cout << setprecision(10);
  }
} init;

#define rep(i, n) for (auto i = (n)*0; i < n; i++)
#define range(it, v) for (auto &it : v)

#define IL inline
typedef double D;
typedef bool B;
typedef complex<double> P;
#define T tuple
typedef T<P, P> L;
typedef T<P, P> LS;
typedef T<P, D> C;
typedef vector<P> Poly;
const D eps = 1e-8;
#define X real()
#define Y imag()
IL B comp(const P &l, const P &r) {
  return (l.X == r.X) ? l.Y < r.Y : l.X < r.X;
}
IL D dot(P a, P b) { return a.X * b.X + a.Y * b.Y; }
IL D cross(P a, P b) { return a.X * b.Y - a.Y * b.X; }

IL B ccw(P a, P b, P c) {
  c -= b, b -= a;
  return cross(b, c) > eps;
}
IL B cww(P a, P b, P c) {
  c -= b, b -= a;
  return cross(b, c) > -eps;
}

IL D signed_distance_P_L(P p, L l) {
  P a, b, c = p;
  tie(a, b) = l;
  if (a == b)
    while (true)
      ;
  return cross(b - a, c - a) / abs(b - a);
}
IL B isintersected_L_LS(L l, LS ls) {
  P a, b, c, d;
  tie(a, b) = l;
  tie(c, d) = ls;
  return cww(a, b, c) == cww(b, a, d) || cww(a, b, d) == cww(b, a, c);
}
IL P cross_point_L_L(L p, L q) {
  P a, b;
  tie(a, b) = p;
  D d1 = signed_distance_P_L(a, q);
  D d2 = signed_distance_P_L(b, q);
  return (a * d2 - b * d1) / (d2 - d1);
}
IL Poly convex_hull(Poly p) {
  int n = p.size();
  Poly res(2 * n);
  int k = 0;
  sort(p.begin(), p.end(), comp);
  rep(i, n) {
    while (k > 1 && !ccw(res[k - 2], res[k - 1], p[i]))
      k--;
    res[k++] = p[i];
  }
  int t = k;
  for (int i = n - 2; i >= 0; i--) {
    while (k > t && !ccw(res[k - 2], res[k - 1], p[i]))
      k--;
    res[k++] = p[i];
  }
  res.resize(k - 1);
  return res;
}
IL D convex_area(Poly &l) {
  D res = 0;
  int n = l.size();
  for (int i = 2; i < n; i++)
    res += abs(cross(l[i - 1] - l[0], l[i] - l[i - 1])) / 2.0;
  return res;
}

int main() {
  int n;
  cin >> n;
  Poly xy(n);
  rep(i, n) {
    double x, y;
    cin >> x >> y;
    xy[i] = P(x, y);
  }
  int q;
  cin >> q;
  while (q--) {
    double x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    P p1(x1, y1), p2(x2, y2);
    Poly pp;
    range(it, xy) if (cww(p1, p2, it)) pp.push_back(it);
    rep(i, n) {
      int j = (i + 1) % n;
      if (isintersected_L_LS(L(p1, p2), LS(xy[i], xy[j])))
        pp.push_back(cross_point_L_L(L(p1, p2), LS(xy[i], xy[j])));
    }
    // range(it,pp)cout<<it;cout<<endl;
    auto ch = convex_hull(pp);
    // range(it,ch)cout<<it;cout<<endl;
    cout << convex_area(ch) << endl;
  }
}