Problem p00877 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef complex<double> P;
typedef pair<P, P> L;

const double EPS = 1e-8;

namespace std {
bool operator<(const P &a, const P &b) {
  return fabs(real(a) - real(b)) < EPS ? imag(a) < imag(b) : real(a) < real(b);
}
} // namespace std

double dot(P a, P b) { return real(conj(a) * b); }

double cross(P a, P b) { return imag(conj(a) * b); }

int ccw(P a, P b, P c) {
  b -= a;
  c -= a;
  if (cross(b, c) > EPS)
    return 1; // ????????????(b-a)????????´???????????????(c-a)????????¨?????????
  if (cross(b, c) < -EPS)
    return -1; // ????????????(b-a)????????´???????????????(c-a)????????¨?????????
  if (dot(b, c) < -EPS)
    return 2;
  if (norm(b) < norm(c))
    return -2;
  return 0;
}

vector<P> convex_hull(vector<P> v) {
  vector<P> c; // convex
  sort(v.begin(), v.end());
  c.assign(v.begin(), v.begin() + 2);

  for (int i = 2; i < v.size(); i++) {
    P a = c[c.size() - 1] - c[c.size() - 2];
    P b = v[i] - c[c.size() - 2];

    while (cross(a, b) > EPS) {
      c.pop_back();
      if (c.size() < 2)
        break;
      a = c[c.size() - 1] - c[c.size() - 2];
      b = v[i] - c[c.size() - 2];
    }

    c.push_back(v[i]);
  }

  int n = c.size();
  c.push_back(v[v.size() - 2]);

  for (int i = v.size() - 3; i >= 0; i--) {
    P a = c[c.size() - 1] - c[c.size() - 2];
    P b = v[i] - c[c.size() - 2];

    while (cross(a, b) > EPS) {
      c.pop_back();
      if (c.size() < n + 1)
        break;
      a = c[c.size() - 1] - c[c.size() - 2];
      b = v[i] - c[c.size() - 2];
    }

    c.push_back(v[i]);
  }
  c.pop_back();
  return c;
}

L get2points(vector<P> p) {

  double dis = 0;
  int idx1, idx2;

  for (int i = 0; i < p.size(); i++) {
    for (int j = i + 1; j < p.size(); j++) {
      if (abs(p[j] - p[i]) > dis) {
        dis = abs(p[j] - p[i]);
        idx1 = i;
        idx2 = j;
      }
    }
  }

  return L(p[idx1], p[idx2]);
}

bool isIntersectSP(L s, P p) {
  return abs(s.first - p) + abs(s.second - p) - abs(s.second - s.first) < EPS;
}

bool check(vector<P> p) {

  L l = get2points(p);

  for (int i = 0; i < p.size(); i++)
    if (!isIntersectSP(l, p[i]))
      return false;

  return true;
}

bool isIntersect(L s1, L s2) {

  // ccw ??????????????????????????¨,
  // ????????????????????´?????????????????¨??????????????????????????????
  if (max(real(s1.first), real(s1.second)) + EPS <
          min(real(s2.first), real(s2.second)) ||
      max(imag(s1.first), imag(s1.second)) + EPS <
          min(imag(s2.first), imag(s2.second)) ||
      max(real(s2.first), real(s2.second)) + EPS <
          min(real(s1.first), real(s1.second)) ||
      max(imag(s2.first), imag(s2.second)) + EPS <
          min(imag(s1.first), imag(s1.second)))
    return false;

  return (ccw(s1.first, s1.second, s2.first) *
                  ccw(s1.first, s1.second, s2.second) <=
              0 &&
          ccw(s2.first, s2.second, s1.first) *
                  ccw(s2.first, s2.second, s1.second) <=
              0);
}

int main() {

  while (1) {

    int n, m;
    cin >> n >> m;

    if (!n && !m)
      break;

    vector<P> p1, p2;

    for (int i = 0; i < n; i++) {
      int a, b;
      cin >> a >> b;
      p1.push_back(P(a, b));
    }

    for (int i = 0; i < m; i++) {
      int a, b;
      cin >> a >> b;
      p2.push_back(P(a, b));
    }

    bool ans = true;

    vector<P> v1, v2;

    if (p1.size() != 1) {

      if (check(p1)) {
        L l = get2points(p1);
        for (int i = 0; i < p2.size(); i++)
          if (isIntersectSP(l, p2[i]))
            ans = false;
        v1.push_back(l.first);
        v1.push_back(l.second);
      } else {
        vector<P> p = convex_hull(p1);

        for (int i = 0; i < p2.size(); i++) {
          int com = ccw(p[0], p[1], p2[i]);
          int flag = 0;
          for (int j = 0; j < p.size(); j++)
            if (com != ccw(p[j], p[(j + 1) % p.size()], p2[i]))
              flag = 1;
          if (flag == 0)
            ans = false;
        }

        for (int i = 0; i < p.size(); i++)
          for (int j = 0; j < p2.size(); j++)
            if (isIntersectSP(L(p[i], p[(i + 1) % p.size()]), p2[j]))
              ans = false;
        v1 = p;
      }
    }

    if (p2.size() != 1) {

      if (check(p2)) {
        L l = get2points(p2);
        for (int i = 0; i < p1.size(); i++)
          if (isIntersectSP(l, p1[i]))
            ans = false;
        v2.push_back(l.first);
        v2.push_back(l.second);
      } else {
        vector<P> p = convex_hull(p2);

        for (int i = 0; i < p1.size(); i++) {
          int com = ccw(p[0], p[1], p1[i]);
          int flag = 0;
          for (int j = 0; j < p.size(); j++)
            if (com != ccw(p[j], p[(j + 1) % p.size()], p1[i]))
              flag = 1;
          if (flag == 0)
            ans = false;
        }

        for (int i = 0; i < p.size(); i++)
          for (int j = 0; j < p1.size(); j++)
            if (isIntersectSP(L(p[i], p[(i + 1) % p.size()]), p1[j]))
              ans = false;
        v2 = p;
      }
    }

    if (p1.size() != 1 && p2.size() != 1) {

      for (int i = 0; i < v1.size(); i++)
        for (int j = 0; j < v2.size(); j++) {
          L l1 = L(v1[i], v1[(i + 1) % v1.size()]);
          L l2 = L(v2[j], v2[(j + 1) % v2.size()]);
          if (isIntersect(l1, l2))
            ans = false;
        }
    }

    if (ans)
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef complex<double> P; // Point
typedef pair<P, P> L;      // Line, Segment

const double EPS = 1e-8;

namespace std {
bool operator<(const P &a, const P &b) {
  return fabs(real(a) - real(b)) < EPS ? imag(a) < imag(b) : real(a) < real(b);
}
} // namespace std

double dot(P a, P b) { return real(conj(a) * b); }

double cross(P a, P b) { return imag(conj(a) * b); }

// ???????????? a,b ???????????¢???
int ccw(P a, P b, P c) {
  b -= a;
  c -= a;
  if (cross(b, c) > EPS)
    return 1; // ????????????(b-a)????????´???????????????(c-a)????????¨?????????
  if (cross(b, c) < -EPS)
    return -1; // ????????????(b-a)????????´???????????????(c-a)????????¨?????????
  if (dot(b, c) < -EPS)
    return 2;
  if (norm(b) < norm(c))
    return -2;
  return 0;
}

// ????????¨?????????????????????????????????????????????true???
bool isIntersect(L s1, L s2) {

  // ccw ??????????????????????????¨,
  // ????????????????????´?????????????????¨??????????????????????????????
  if (max(real(s1.first), real(s1.second)) + EPS <
          min(real(s2.first), real(s2.second)) ||
      max(imag(s1.first), imag(s1.second)) + EPS <
          min(imag(s2.first), imag(s2.second)) ||
      max(real(s2.first), real(s2.second)) + EPS <
          min(real(s1.first), real(s1.second)) ||
      max(imag(s2.first), imag(s2.second)) + EPS <
          min(imag(s1.first), imag(s1.second)))
    return false;

  return (ccw(s1.first, s1.second, s2.first) *
                  ccw(s1.first, s1.second, s2.second) <=
              0 &&
          ccw(s2.first, s2.second, s1.first) *
                  ccw(s2.first, s2.second, s1.second) <=
              0);
}

// ????????¨??????????????????
bool isIntersectSP(L s, P p) {
  return abs(s.first - p) + abs(s.second - p) - abs(s.second - s.first) < EPS;
}

vector<P> convex_hull2(vector<P> ps) {
  int n = ps.size(), k = 0;
  sort(ps.begin(), ps.end());
  vector<P> ch(2 * n);
  for (int i = 0; i < n; ch[k++] = ps[i++]) // lower-hull
    while (k >= 2 && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0)
      --k;
  for (int i = n - 2, t = k + 1; i >= 0; ch[k++] = ps[i--]) // upper-hull
    while (k >= t && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0)
      --k;
  ch.resize(k - 1);
  return ch;
}

int main() {

  while (1) {

    int n, m;
    cin >> n >> m;
    if (!n && !m)
      break;

    vector<P> B(n), W(m);

    int x, y;

    for (int i = 0; i < n; i++) {
      cin >> x >> y;
      B[i] = P(x, y);
    }

    for (int i = 0; i < m; i++) {
      cin >> x >> y;
      W[i] = P(x, y);
    }

    bool ans = true;

    if (n <= 3 && m <= 3) {

      for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++) {
          if (i == j)
            continue;

          for (int k = 0; k < m; k++)
            for (int l = 0; l < m; l++) {
              if (k == l)
                continue;

              if (isIntersect(L(B[i], B[j]), L(W[k], W[l])))
                ans = false;
            }
        }
    }

    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++) {
        if (i == j)
          continue;

        for (int k = 0; k < m; k++)
          if (isIntersectSP(L(B[i], B[j]), W[k]))
            ans = false;
      }

    for (int i = 0; i < m; i++)
      for (int j = 0; j < m; j++) {
        if (i == j)
          continue;

        for (int k = 0; k < n; k++)
          if (isIntersectSP(L(W[i], W[j]), B[k]))
            ans = false;
      }

    vector<P> B2 = convex_hull2(B);

    int f = 0, flag = 1;

    for (int i = 0; i < B2.size(); i++) {
      for (int j = 0; j < W.size(); j++) {
        int ip = (i + 1) % B2.size();
        if (ccw(B2[i], B2[ip], W[j]) == -1)
          flag = 0;
        //	cout<<ccw(B2[i],B2[ip],W[j])<<endl;
      }
    }

    if (B2.size() >= 3 && flag)
      ans = false;

    vector<P> W2 = convex_hull2(W);

    f = 0;
    flag = 1;

    for (int i = 0; i < W2.size(); i++) {
      for (int j = 0; j < B.size(); j++) {
        int ip = (i + 1) % W2.size();
        if (ccw(W2[i], W2[ip], B[j]) == -1)
          flag = 0;
        //	cout<<ccw(W2[i],W2[ip],B[j])<<endl;
      }
    }

    if (W2.size() >= 3 && flag)
      ans = false;

    if (ans)
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  }

  return 0;
}