Problem p02302 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

template <class T> struct Point {
  typedef Point P;
  T x, y;
  explicit Point(T x = 0, T y = 0) : x(x), y(y) {}
  bool operator<(P p) const { return tie(x, y) < tie(p.x, p.y); }
  bool operator==(P p) const { return tie(x, y) == tie(p.x, p.y); }
  P operator+(P p) const { return P(x + p.x, y + p.y); }
  P operator-(P p) const { return P(x - p.x, y - p.y); }
  P operator*(T d) const { return P(x * d, y * d); }
  P operator/(T d) const { return P(x / d, y / d); }
  T dot(P p) const { return x * p.x + y * p.y; }
  T cross(P p) const { return x * p.y - y * p.x; }
  T cross(P a, P b) const { return (a - *this).cross(b - *this); }
  T dist2() const { return x * x + y * y; }
  double dist() const { return sqrt((double)dist2()); }
  // angle to x-axis in interval [-pi, pi]
  double angle() const { return atan2(y, x); }
  P unit() const { return *this / dist(); } // makes dist()=1
  P perp() const { return P(-y, x); }       // rotates +90 degrees
  P normal() const { return perp().unit(); }
  // returns point rotated 'a' radians ccw around the origin
  P rotate(double a) const {
    return P(x * cos(a) - y * sin(a), x * sin(a) + y * cos(a));
  }
};

using P = Point<double>;

template <class P>
int lineIntersection(const P &s1, const P &e1, const P &s2, const P &e2, P &r) {
  if ((e1 - s1).cross(e2 - s2)) { // if not parallell
    r = s2 - (e2 - s2) * (e1 - s1).cross(s2 - s1) / (e1 - s1).cross(e2 - s2);
    return 1;
  } else
    return -((e1 - s1).cross(s2 - s1) == 0 || s2 == e2);
}

vector<P> polygonCut(const vector<P> &poly, P s, P e) {
  vector<P> res;
  rep(i, 0, sz(poly)) {
    P cur = poly[i], prev = i ? poly[i - 1] : poly.back();
    bool side = s.cross(e, cur) < 0;
    if (side != (s.cross(e, prev) < 0)) {
      res.emplace_back();
      lineIntersection(s, e, cur, prev, res.back());
    }
    if (side)
      res.push_back(cur);
  }
  return res;
}

template <class T> T polygonArea2(vector<Point<T>> &v) {
  T a = v.back().cross(v[0]);
  rep(i, 0, sz(v) - 1) a += v[i].cross(v[i + 1]);
  return a;
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cout << fixed << setprecision(15);
  int n;
  cin >> n;
  vector<P> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i].x >> a[i].y;
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    P p1, p2;
    cin >> p1.x >> p1.y >> p2.x >> p2.y;
    vector<P> poly = polygonCut(a, p2, p1);
    cout << polygonArea2(poly) / 2.0 << '\n';
  }
}


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

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

template <class T> struct Point {
  typedef Point P;
  T x, y;
  explicit Point(T x = 0, T y = 0) : x(x), y(y) {}
  bool operator<(P p) const { return tie(x, y) < tie(p.x, p.y); }
  bool operator==(P p) const { return tie(x, y) == tie(p.x, p.y); }
  P operator+(P p) const { return P(x + p.x, y + p.y); }
  P operator-(P p) const { return P(x - p.x, y - p.y); }
  P operator*(T d) const { return P(x * d, y * d); }
  P operator/(T d) const { return P(x / d, y / d); }
  T dot(P p) const { return x * p.x + y * p.y; }
  T cross(P p) const { return x * p.y - y * p.x; }
  T cross(P a, P b) const { return (a - *this).cross(b - *this); }
  T dist2() const { return x * x + y * y; }
  double dist() const { return sqrt((double)dist2()); }
  // angle to x-axis in interval [-pi, pi]
  double angle() const { return atan2(y, x); }
  P unit() const { return *this / dist(); } // makes dist()=1
  P perp() const { return P(-y, x); }       // rotates +90 degrees
  P normal() const { return perp().unit(); }
  // returns point rotated 'a' radians ccw around the origin
  P rotate(double a) const {
    return P(x * cos(a) - y * sin(a), x * sin(a) + y * cos(a));
  }
};

using P = Point<double>;

template <class P>
int lineIntersection(const P &s1, const P &e1, const P &s2, const P &e2, P &r) {
  if ((e1 - s1).cross(e2 - s2)) { // if not parallell
    r = s2 - (e2 - s2) * (e1 - s1).cross(s2 - s1) / (e1 - s1).cross(e2 - s2);
    return 1;
  } else
    return -((e1 - s1).cross(s2 - s1) == 0 || s2 == e2);
}

vector<P> polygonCut(const vector<P> &poly, P s, P e) {
  vector<P> res;
  rep(i, 0, sz(poly)) {
    P cur = poly[i], prev = i ? poly[i - 1] : poly.back();
    bool side = s.cross(e, cur) < 0;
    if (side != (s.cross(e, prev) < 0)) {
      res.emplace_back();
      lineIntersection(s, e, cur, prev, res.back());
    }
    if (side)
      res.push_back(cur);
  }
  return res;
}

template <class T> T polygonArea2(vector<Point<T>> &v) {
  if (v.empty())
    return 0;
  T a = v.back().cross(v[0]);
  rep(i, 0, sz(v) - 1) a += v[i].cross(v[i + 1]);
  return a;
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cout << fixed << setprecision(15);
  int n;
  cin >> n;
  vector<P> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i].x >> a[i].y;
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    P p1, p2;
    cin >> p1.x >> p1.y >> p2.x >> p2.y;
    vector<P> poly = polygonCut(a, p2, p1);
    cout << polygonArea2(poly) / 2.0 << '\n';
  }
}