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

#include <bits/stdc++.h>
#define f first
#define s second
#define mp make_pair
#define pi M_PI
#define inf 1e14
#define eps (1e-5)
#define MAX 200000
#define equals(a, b) (fabs((a) - (b)) < eps)
using namespace std;

class Point {
public:
  double x, y;
  Point(double x = 0, double y = 0) : x(x), y(y) {}

  Point operator+(Point p) { return Point(x + p.x, y + p.y); }
  Point operator-(Point p) { return Point(x - p.x, y - p.y); }
  Point operator*(double k) { return Point(x * k, y * k); }
  Point operator/(double k) { return Point(x / k, y / k); }
  bool operator<(Point p) const { return (x != p.x ? x < p.x : y < p.y); }
  bool operator==(Point p) const {
    return fabs(x - p.x) < eps && fabs(y - p.y) < eps;
  }

  double abs() { return sqrt(norm()); }
  double norm() { return (x * x + y * y); }
};
typedef Point Vector;
typedef vector<Point> Polygon;

class Segment {
public:
  Point p1, p2;
  Segment(Point p1 = Point(), Point p2 = Point()) : p1(p1), p2(p2) {}
};
typedef Segment Line;

double norm(Vector a) { return (a.x * a.x + a.y * a.y); }
double abs(Vector a) { return sqrt(norm(a)); }
double dot(Vector a, Vector b) { return (a.x * b.x + a.y * b.y); }
double cross(Vector a, Vector b) { return (a.x * b.y - a.y * b.x); }

bool isParallel(Segment s, Segment t) {
  return equals(cross(s.p1 - s.p2, t.p1 - t.p2), 0.0);
}

int ccw(Point p0, Point p1, Point p2) {
  Vector a = p1 - p0;
  Vector b = p2 - p0;
  if (cross(a, b) > eps)
    return 1;
  if (cross(a, b) < -eps)
    return -1;
  if (dot(a, b) < -eps)
    return 2;
  if (a.norm() < b.norm())
    return -2;
  return 0;
}

bool intersect(Point p1, Point p2, Point p3, Point p4) {
  return (ccw(p1, p2, p3) * ccw(p1, p2, p4) <= 0 &&
          ccw(p3, p4, p1) * ccw(p3, p4, p2) <= 0);
}

bool intersect(Segment s1, Segment s2) {
  return intersect(s1.p1, s1.p2, s2.p1, s2.p2);
}

Point getCrossPointLL(Line a, Line b) {
  double A = cross(a.p2 - a.p1, b.p2 - b.p1);
  double B = cross(a.p2 - a.p1, a.p2 - b.p1);
  if (abs(A) < eps || abs(B) < eps)
    return b.p1;
  return b.p1 + (b.p2 - b.p1) * (B / A);
}

double getPolygonArea(Polygon p) {
  double area = 0.0;
  int n = p.size();
  for (int i = 0; i < n; i++)
    area += cross(p[i % n], p[(i + 1) % n]);
  return area / 2;
}

typedef vector<vector<int>> Graph;

Graph SegmentArrangement(vector<Segment> v, vector<Point> &ps) {
  for (int i = 0; i < v.size(); i++) {
    ps.push_back(v[i].p1);
    ps.push_back(v[i].p2);
    for (int j = i + 1; j < v.size(); j++) {
      if (intersect(v[i], v[j]))
        ps.push_back(getCrossPointLL(v[i], v[j]));
    }
  }
  sort(ps.begin(), ps.end());
  ps.erase(unique(ps.begin(), ps.end()), ps.end());
  Graph g(ps.size());
  for (int i = 0; i < v.size(); i++) {
    vector<pair<double, int>> list;
    for (int j = 0; j < ps.size(); j++)
      if (ccw(v[i].p1, v[i].p2, ps[j]) == 0)
        list.push_back(mp(norm(v[i].p1 - ps[j]), j));
    sort(list.begin(), list.end());
    for (int j = 0; j + 1 < list.size(); j++) {
      int a = list[j].s, b = list[j + 1].s;
      g[a].push_back(b);
      g[b].push_back(a);
    }
  }
  return g;
}

bool merge_if_able(Segment &s, Segment t) {
  if (!isParallel(s, t))
    return false;
  if (abs(ccw(s.p1, s.p2, t.p1)) == 1 || abs(ccw(s.p1, s.p2, t.p2)) == 1)
    return false;
  if (ccw(s.p1, s.p2, t.p1) == -2 || ccw(t.p1, t.p2, s.p1) == -2)
    return false;
  s = Segment(min(s.p1, t.p1), max(s.p2, t.p2));
  return true;
}

void merge(vector<Segment> &v) {
  for (int i = 0; i < v.size(); i++) {
    if (v[i].p2 < v[i].p1)
      swap(v[i].p2, v[i].p1);
  }
  for (int i = 0; i < v.size(); i++) {
    for (int j = i + 1; j < v.size(); j++) {
      if (merge_if_able(v[i], v[j])) {
        v[j--] = v.back();
        v.pop_back();
      }
    }
  }
}

Graph e;
vector<Segment> vs;
vector<Point> vp;
Polygon p;
int start = -1;
double ans = 0.0;

double getAngle(Vector a, Vector b) {
  double tmp = dot(a, b) / (abs(a) * abs(b));
  if (tmp < -1.0)
    tmp = -1.0;
  if (1.0 < tmp)
    tmp = 1.0;
  double r = acos(tmp) * 180.0 / pi;
  if (cross(a, b) < -eps)
    r = 360.0 - r;
  return r;
}

void dfs(int now, int before) {
  if (now == start)
    return;
  vector<pair<double, int>> vd;
  for (int i = 0; i < e[now].size(); i++) {
    int next = e[now][i];
    double ang = getAngle(vp[before] - vp[now], vp[next] - vp[now]);
    if (next == before)
      ang = 361;
    vd.push_back(mp(ang, next));
  }
  sort(vd.begin(), vd.end());
  p.push_back(vp[now]);
  dfs(vd[0].s, now);
  return;
}

int main() {
  int n, a, b, c, d;

  cin >> n;
  cin >> a >> b;
  for (int i = 1; i < n; i++) {
    cin >> c >> d;
    vs.push_back(Segment(Point(a, b), Point(c, d)));
    a = c;
    b = d;
  }
  merge(vs);
  e = SegmentArrangement(vs, vp);
  Point s(inf, inf);
  for (int i = 0; i < vp.size(); i++)
    s = min(s, vp[i]);
  for (int i = 0; i < vp.size(); i++)
    if (vp[i] == s)
      start = i;
  for (int i = 0; i < e[start].size(); i++) {
    p.clear();
    p.push_back(vp[start]);
    dfs(e[start][i], start);
    double res = getPolygonArea(p);
    if (0.0 <= res)
      ans += res;
  }
  printf("%.10f\\n", ans);
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define f first
#define s second
#define mp make_pair
#define pi M_PI
#define inf 1 << 30
#define eps (1e-13)
#define MAX 10000
#define equals(a, b) (fabs((a) - (b)) < eps)
using namespace std;

class Point {
public:
  double x, y;
  Point(double x = 0, double y = 0) : x(x), y(y) {}

  Point operator+(Point p) { return Point(x + p.x, y + p.y); }
  Point operator-(Point p) { return Point(x - p.x, y - p.y); }
  Point operator*(double k) { return Point(x * k, y * k); }
  Point operator/(double k) { return Point(x / k, y / k); }
  bool operator<(Point p) const { return (y != p.y ? y < p.y : x < p.x); }
  bool operator==(Point p) const {
    return fabs(x - p.x) < eps && fabs(y - p.y) < eps;
  }

  double abs() { return sqrt(norm()); }
  double norm() { return (x * x + y * y); }
};
typedef Point Vector;
typedef vector<Point> Polygon;

class Segment {
public:
  Point p1, p2;
  Segment(Point p1 = Point(), Point p2 = Point()) : p1(p1), p2(p2) {}
};
typedef Segment Line;

double norm(Vector a) { return (a.x * a.x + a.y * a.y); }
double abs(Vector a) { return sqrt(norm(a)); }
double dot(Vector a, Vector b) { return (a.x * b.x + a.y * b.y); }
double cross(Vector a, Vector b) { return (a.x * b.y - a.y * b.x); }

bool isParallel(Segment s, Segment t) {
  return equals(cross(s.p1 - s.p2, t.p1 - t.p2), 0.0);
}

int ccw(Point p0, Point p1, Point p2) {
  Vector a = p1 - p0;
  Vector b = p2 - p0;
  if (cross(a, b) > eps)
    return 1;
  if (cross(a, b) < -eps)
    return -1;
  if (dot(a, b) < -eps)
    return 2;
  if (a.norm() < b.norm())
    return -2;
  return 0;
}

bool intersect(Point p1, Point p2, Point p3, Point p4) {
  return (ccw(p1, p2, p3) * ccw(p1, p2, p4) <= 0 &&
          ccw(p3, p4, p1) * ccw(p3, p4, p2) <= 0);
}

bool intersect(Segment s1, Segment s2) {
  return intersect(s1.p1, s1.p2, s2.p1, s2.p2);
}

Point getCrossPointLL(Line a, Line b) {
  double A = cross(a.p2 - a.p1, b.p2 - b.p1);
  double B = cross(a.p2 - a.p1, a.p2 - b.p1);
  if (abs(A) < eps || abs(B) < eps)
    return b.p1;
  return b.p1 + (b.p2 - b.p1) * (B / A);
}

double getPolygonArea(Polygon p) {
  double area = 0.0;
  int n = p.size();
  for (int i = 0; i < n; i++)
    area += cross(p[i % n], p[(i + 1) % n]);
  return area / 2;
}

double arg(Vector p) { return atan2(p.y, p.x); }

vector<Point> arg_sort(vector<Point> v, Point base) {
  vector<pair<double, Point>> tmp;
  vector<Point> res;
  for (int i = 0; i < v.size(); i++) {
    double targ = arg(v[i] - base);
    if (targ < 0.0)
      targ += 2 * pi;
    tmp.push_back(mp(targ, v[i]));
  }
  sort(tmp.begin(), tmp.end());
  for (int i = 0; i < tmp.size(); i++)
    res.push_back(tmp[i].s);
  return res;
}

typedef vector<vector<int>> Graph;

Graph SegmentArrangement(vector<Segment> v, vector<Point> &ps) {
  for (int i = 0; i < v.size(); i++) {
    ps.push_back(v[i].p1);
    ps.push_back(v[i].p2);
    for (int j = i + 1; j < v.size(); j++) {
      if (intersect(v[i], v[j]))
        ps.push_back(getCrossPointLL(v[i], v[j]));
    }
  }
  sort(ps.begin(), ps.end());
  ps.erase(unique(ps.begin(), ps.end()), ps.end());
  Graph g(ps.size());
  for (int i = 0; i < v.size(); i++) {
    vector<pair<double, int>> list;
    for (int j = 0; j < ps.size(); j++)
      if (ccw(v[i].p1, v[i].p2, ps[j]) == 0)
        list.push_back(mp(norm(v[i].p1 - ps[j]), j));
    sort(list.begin(), list.end());
    for (int j = 0; j < list.size() - 1; j++) {
      int a = list[j].s, b = list[j + 1].s;
      g[a].push_back(b);
      g[b].push_back(a);
    }
  }
  return g;
}

bool merge_if_able(Segment &s, Segment t) {
  if (!isParallel(s, t))
    return false;
  if (ccw(s.p1, t.p1, s.p2) == 1 || ccw(s.p1, t.p1, s.p2) == -1)
    return false;
  if (ccw(s.p1, s.p2, t.p1) == -2 || ccw(t.p1, t.p2, s.p1) == -2)
    return false;
  s = Segment(min(s.p1, t.p1), max(s.p2, t.p2));
  return true;
}

void merge(vector<Segment> &v) {
  for (int i = 0; i < v.size(); i++) {
    if (v[i].p2 < v[i].p1)
      swap(v[i].p2, v[i].p1);
  }
  for (int i = 0; i < v.size(); i++)
    for (int j = i + 1; j < v.size(); j++)
      if (merge_if_able(v[i], v[j]))
        v[j--] = v.back(), v.pop_back();
}

Graph e;
vector<Segment> vs;
vector<Point> vp;
double ans = 0.0;
bool visited[MAX];
vector<Polygon> S, L;
Point s(inf, inf);
Polygon p;
int start = -1;
bool flag = false;

vector<int> f(vector<int> tmp, Point base) {
  vector<Point> tmp1;
  map<Point, int> mp;
  vector<int> res;
  for (int i = 0; i < tmp.size(); i++) {
    tmp1.push_back(vp[tmp[i]]);
    mp[vp[tmp[i]]] = tmp[i];
  }
  tmp1 = arg_sort(tmp1, base);
  for (int i = 0; i < tmp1.size(); i++)
    res.push_back(mp[tmp1[i]]);
  return res;
}

double getAngle(Vector a, Vector b) {
  double r = acos(dot(a, b) / (abs(a) * abs(b))) * 180 / pi;
  if (cross(a, b) < 0.0)
    r = 360 - r;
  return r;
}

void dfs(int now, int before) {
  if (now == start) {
    flag = true;
    return;
  }
  vector<int> v;
  vector<pair<double, int>> vd;
  for (int i = 0; i < e[now].size(); i++)
    if (e[now][i] != before)
      v.push_back(e[now][i]);
  for (int i = 0; i < v.size(); i++)
    vd.push_back(mp(getAngle(vp[before] - vp[now], vp[v[i]] - vp[now]), v[i]));
  sort(vd.begin(), vd.end());
  p.push_back(vp[now]);
  for (int i = 0; i < vd.size(); i++) {
    dfs(vd[i].s, now);
    if (flag)
      break;
    p.push_back(vp[now]);
  }
}

int main() {
  int n, a, b, c, d;

  cin >> n;
  cin >> a >> b;
  for (int i = 1; i < n; i++) {
    cin >> c >> d;
    vs.push_back(Segment(Point(a, b), Point(c, d)));
    a = c;
    b = d;
  }
  merge(vs);
  e = SegmentArrangement(vs, vp);
  for (int i = 0; i < vp.size(); i++)
    s = min(s, vp[i]);
  for (int i = 0; i < vp.size(); i++)
    if (vp[i] == s)
      start = i;
  vector<int> tmp;
  for (int i = 0; i < e[start].size(); i++)
    tmp.push_back(e[start][i]);
  tmp = f(tmp, vp[start]);
  p.push_back(vp[start]);
  dfs(tmp[0], start);
  double ans = getPolygonArea(p);
  if (ans < 0.0)
    cout << 0.0 << endl;
  else
    printf("%.10f\\n", ans);
  return 0;
}