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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define eps 1e-9
#define ALL(x) x.begin(), x.end()
#define INS(x) inserter(x, x.begin())
#define FOR(i, j, k) for (int i = j; i <= k; i++)
#define MAXN 105
#define MAXM 40005
#define INF 0x3fffffff
using namespace std;
typedef long long LL;
struct Point {
  double x, y;
  Point(double x = 0, double y = 0) : x(x), y(y) {}
};
typedef Point Vector;

Point read_point(void) {
  double x, y;
  scanf("%lf%lf", &x, &y);
  return Point(x, y);
}

const double EPS = 1e-10;

Vector operator+(Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); }

Vector operator-(Vector A, Vector B) { return Vector(A.x - B.x, A.y - B.y); }

Vector operator*(Vector A, double p) { return Vector(A.x * p, A.y * p); }

Vector operator/(Vector A, double p) { return Vector(A.x / p, A.y / p); }

bool operator<(const Point &a, const Point &b) {
  return a.x < b.x || (a.x == b.x && a.y < b.y);
}

int dcmp(double x) {
  if (fabs(x) < EPS)
    return 0;
  else
    return x < 0 ? -1 : 1;
}

bool operator==(const Point &a, const Point &b) {
  return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;
}

double Dot(Vector A, Vector B) { return A.x * B.x + A.y * B.y; }

double Length(Vector A) { return sqrt(Dot(A, A)); }

double Angle(Vector A, Vector B) {
  return acos(Dot(A, B) / Length(A) / Length(B));
}

double Cross(Vector A, Vector B) { return A.x * B.y - A.y * B.x; }

double Area2(Point A, Point B, Point C) { return Cross(B - A, C - A); }

Vector VRotate(Vector A, double rad) {
  return Vector(A.x * cos(rad) - A.y * sin(rad),
                A.x * sin(rad) + A.y * cos(rad));
}

Point PRotate(Point A, Point B, double rad) { return A + VRotate(B - A, rad); }

Vector Normal(Vector A) {
  double l = Length(A);
  return Vector(-A.y / l, A.x / l);
}

Point GetLineIntersection(Point P, Vector v, Point Q, Vector w) {
  Vector u = P - Q;
  double t = Cross(w, u) / Cross(v, w);
  return P + v * t;
}

double DistanceToLine(Point P, Point A, Point B) {
  Vector v1 = B - A, v2 = P - A;
  return fabs(Cross(v1, v2)) / Length(v1);
}

double DistanceToSegment(Point P, Point A, Point B) {
  if (A == B)
    return Length(P - A);
  Vector v1 = B - A, v2 = P - A, v3 = P - B;
  if (dcmp(Dot(v1, v2)) < 0)
    return Length(v2);
  else if (dcmp(Dot(v1, v3)) > 0)
    return Length(v3);
  else
    return fabs(Cross(v1, v2)) / Length(v1);
}

Point GetLineProjection(Point P, Point A, Point B) {
  Vector v = B - A;
  return A + v * (Dot(v, P - A) / Dot(v, v));
}

bool SegmentProperIntersection(Point a1, Point a2, Point b1, Point b2) {
  double c1 = Cross(a2 - a1, b1 - a1), c2 = Cross(a2 - a1, b2 - a1);
  double c3 = Cross(b2 - b1, a1 - b1), c4 = Cross(b2 - b1, a2 - b1);
  return dcmp(c1) * dcmp(c2) < 0 && dcmp(c3) * dcmp(c4) < 0;
}

bool OnSegment(Point P, Point a1, Point a2) {
  Vector v1 = a1 - P, v2 = a2 - P;
  return dcmp(Cross(v1, v2)) == 0 && dcmp(Dot(v1, v2)) < 0;
}

double PolygonArea(Point *P, int n) {
  double ans = 0.0;
  for (int i = 1; i < n - 1; ++i)
    ans += Cross(P[i] - P[0], P[i + 1] - P[0]);
  return ans / 2;
}

const double PI = acos(-1.0);
struct Line {
  Point p;
  Vector v;
  double ang;
  Line() {}
  Line(Point p, Vector v) : p(p), v(v) { ang = atan2(v.y, v.x); }
  Point point(double t) { return p + v * t; }
  bool operator<(const Line &L) const { return ang < L.ang; }
};

int isPointInPolygon(Point P, Point *Poly, int n) {
  int wn;
  for (int i = 0; i < n; ++i) {
    if (OnSegment(P, Poly[i], Poly[(i + 1) % n]))
      return -1;
    int k = dcmp(Cross(Poly[(i + 1) % n] - Poly[i], P - Poly[i]));
    int d1 = dcmp(Poly[i].y - P.y);
    int d2 = dcmp(Poly[(i + 1) % n].y - P.y);
    if (k > 0 && d1 <= 0 && d2 > 0)
      wn++;
    if (k < 0 && d2 <= 0 && d1 > 0)
      wn--;
  }
  if (wn != 0)
    return 1;
  return 0;
}

int ConvexHull(Point *p, int n, Point *ch) {
  sort(p, p + n);
  int m = 0;
  for (int i = 0; i < n; ++i) {
    while (m > 1 && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0)
      m--;
    ch[m++] = p[i];
  }
  int k = m;
  for (int i = n - 2; i >= 0; --i) {
    while (m > k && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0)
      m--;
    ch[m++] = p[i];
  }
  if (n > 1)
    m--;
  return m;
}

bool ConvexHullIntersection(Point *ch1, int t1, Point *ch2, int t2) {
  double angle[MAXN], x;
  int i, j, k, m;
  if (t1 == 1)
    return true;
  if (t1 == 2) {
    for (i = 0; i < t2; i++) {
      k = dcmp(Cross(ch1[1] - ch1[0], ch2[i] - ch1[0]));
      if (k == 0 && Dot(ch1[1] - ch1[0], ch2[i] - ch1[0]) > 0) {
        if (Length(ch2[i] - ch1[0]) < Length(ch1[1] - ch1[0]))
          break;
      }
    }
    if (i < t2)
      return false;
    if (t2 == 2 && SegmentProperIntersection(ch1[0], ch1[1], ch2[0], ch2[1]))
      return false;
    return true;
  }

  angle[0] = 0;
  for (i = 2; i < t1; i++)
    angle[i - 1] = Angle(ch1[1] - ch1[0], ch1[i] - ch1[0]);
  for (i = 0; i < t2; i++) {
    j = dcmp(Cross(ch1[1] - ch1[0], ch2[i] - ch1[0]));
    if (j < 0 || (j == 0 && Dot(ch1[1] - ch1[0], ch2[i] - ch1[0]) < 0))
      continue;
    j = dcmp(Cross(ch1[t1 - 1] - ch1[0], ch2[i] - ch1[0]));
    if (j > 0 || (j == 0 && Dot(ch1[t1 - 1] - ch1[0], ch2[i] - ch1[0]) < 0))
      continue;
    x = Angle(ch1[1] - ch1[0], ch2[i] - ch1[0]);
    m = lower_bound(angle, angle + t1 - 1, x) - angle;
    if (m == 0)
      j = 0;
    else
      j = m - 1;
    k = dcmp(Cross(ch1[j + 1] - ch2[i], ch1[j + 2] - ch2[i]));
    if (k >= 0)
      break;
  }
  if (i < t2)
    return false;
  return true;
}

int i, j, k, n, m, x, y, T, ans, big, cas, num, len;
bool flag;
Point a[105], b[105], cha[105], chb[105];

int main() {
  while (scanf("%d%d", &n, &m), n + m) {

    for (i = 0; i < n; i++) {
      scanf("%lf%lf", &a[i].x, &a[i].y);
    }

    int as = ConvexHull(a, n, cha);

    for (i = 0; i < m; i++) {
      scanf("%lf%lf", &b[i].x, &b[i].y);
    }

    int bs = ConvexHull(b, m, chb);

    if (ConvexHullIntersection(cha, as, chb, bs) &&
        ConvexHullIntersection(chb, bs, cha, as))
      printf("YES\\n");
    else
      printf("NO\\n");
  }

  return 0;
}

Error: WA

Fixed submission:

#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
typedef struct node {
  int x, y;
};
int n, m;
node a[110];
double s(int i, int j, int k) {
  return fabs(a[i].x * a[j].y + a[k].x * a[i].y + a[j].x * a[k].y -
              a[k].x * a[j].y - a[i].x * a[k].y - a[j].x * a[i].y);
}
bool tepan(int i, int j, int k, int l) {
  if (max(a[i].x, a[j].x) < min(a[k].x, a[l].x) ||
      min(a[i].x, a[j].x) > max(a[k].x, a[l].x))
    return false;
  else
    return true;
}
bool ok(int i, int j, int k, int l) {
  int a = s(i, j, k), b = s(l, i, j), c = s(l, k, i), d = s(l, j, k);
  if (a == 0 && b == 0 && c == 0 && d == 0)
    return tepan(i, j, k, l);
  if (a == b + c + d)
    return true;
  else
    return false;
}
bool xing(int i, int j, int k) {
  if ((a[k].y - a[i].y) * (a[k].x - a[j].x) !=
      (a[k].x - a[i].x) * (a[k].y - a[j].y))
    return false;
  if (a[k].y > max(a[i].y, a[j].y) || a[k].y < min(a[i].y, a[j].y))
    return false;
  return true;
}
int main() {
  while (~scanf("%d%d", &n, &m), n + m) {
    for (int i = 0; i < m + n; i++)
      scanf("%d%d", &a[i].x, &a[i].y);
    int flag = 1;
    for (int i = 0; i < n; i++)
      for (int j = i + 1; j < n; j++)
        for (int k = j + 1; k < n; k++)
          for (int l = n; l < n + m; l++)
            if (ok(i, j, k, l)) {
              flag = 0;
              cout << i << ' ' << j << ' ' << k << ' ' << l << endl;
              break;
            }
    if (m < 3 && n < 3) {
      if (m == 1 && n == 2) {
        if (xing(0, 1, 2)) {
          flag = 0;
        }
      } else if (m == 2 && n == 1) {
        if (xing(1, 2, 0)) {
          flag = 0;
        }
      } else if (m == 2 && n == 2) {
        a[4].x = 2 * a[0].x - a[1].x;
        a[4].y = 2 * a[0].y - a[1].y;
        a[5].x = 2 * a[1].x - a[0].x;
        a[5].y = 2 * a[1].y - a[0].y;
        if (ok(2, 3, 4, 0) && ok(2, 3, 5, 1)) {
          flag = 0;
        }
      }
    }
    if (flag)
      printf("YES\\n");
    else
      printf("NO\\n");
  }
  return 0;
}

/*
3 3
100 700
200 200
600 600
500 100
500 300
800 500
3 3
100 300
400 600
400 100
600 400
500 900
300 300
3 4
300 300
500 300
400 600
100 100
200 900
500 900
800 100
1 2
300 300
100 100
500 500
1 1
100 100
200 100
2 2
0 0
500 700
1000 1400
1500 2100
2 2
0 0
1000 1000
1000 0
0 1000
3 3
0 100
4999 102
10000 103
5001 102
10000 102
0 101
3 3
100 100
200 100
100 200
0 0
400 0
0 400
3 3
2813 1640
2583 2892
2967 1916
541 3562
9298 3686
7443 7921
0 0
*/