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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define fs first
#define sc second
using namespace std;
typedef pair<int, int> P;

// for vector
inline int norm(const P &a) { return a.fs * a.fs + a.sc * a.sc; }
inline int dot(const P &a, const P &b) { return a.fs * b.fs + a.sc * b.sc; }
inline int cross(const P &a, const P &b) { return a.fs * b.sc - a.sc * b.fs; }

inline int ccw(const P &a, P b, P c) {
  b.fs -= a.fs;
  b.sc -= a.sc;
  c.fs -= a.fs;
  c.sc -= a.sc;
  if (cross(b, c) > 0)
    return 1; // counter clockwise
  if (cross(b, c) < 0)
    return -1; // clockwise
  if (dot(b, c) < -0)
    return 2; // c--a--b on line
  if (norm(b) < norm(c))
    return -2; // a--b--c on line
  return 0;    // on segment
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);

  int n, m;
  P b[110], w[110];
  while (cin >> n >> m) {
    if (n == 0 && m == 0)
      break;
    rep(i, n) cin >> b[i].fs >> b[i].sc;
    rep(i, m) cin >> w[i].fs >> w[i].sc;

    bool ans = false;
    rep(i, n) rep(j, i) {
      bool f = true;
      set<int> dirb;
      rep(k, n) {
        if (k == i || k == j)
          continue;
        int tmp = ccw(b[i], b[j], b[k]);
        if (tmp == 0) {
          f = false;
          break;
        }
        if (abs(tmp) == 1) {
          dirb.insert(tmp);
          if (dirb.size() == 2) {
            f = false;
            break;
          }
        }
      }

      if (f) {
        set<int> dirw;
        rep(k, m) {
          int tmp = ccw(b[i], b[j], w[k]);
          if (abs(tmp) != 1) {
            f = false;
            break;
          }
          dirw.insert(tmp);
          if (dirw.size() == 2) {
            f = false;
            break;
          }
        }
        if (f && (dirb.size() == 0 || dirw.size() == 0 ||
                  *dirb.begin() != *dirw.begin()))
          ans = true;
      }
      if (ans)
        break;
    }

    rep(i, m) rep(j, i) {
      bool f = true;
      set<int> dirw;
      rep(k, m) {
        if (k == i || k == j)
          continue;
        int tmp = ccw(w[i], w[j], w[k]);
        if (tmp == 0) {
          f = false;
          break;
        }
        if (abs(tmp) == 1) {
          dirw.insert(tmp);
          if (dirw.size() == 2) {
            f = false;
            break;
          }
        }
      }

      if (f) {
        set<int> dirb;
        rep(k, n) {
          int tmp = ccw(w[i], w[j], b[k]);
          if (abs(tmp) != 1) {
            f = false;
            break;
          }
          dirb.insert(tmp);
          if (dirb.size() == 2) {
            f = false;
            break;
          }
        }
        if (f && (dirb.size() == 0 || dirw.size() == 0 ||
                  *dirb.begin() != *dirw.begin()))
          ans = true;
      }
      if (ans)
        break;
    }

    rep(i, n) rep(j, m) {
      bool f = true;
      set<int> dirb;
      rep(k, n) {
        if (k == i)
          continue;
        int tmp = ccw(b[i], w[j], b[k]);
        if (tmp == 0 || tmp == -2) {
          f = false;
          break;
        }
        if (abs(tmp) == 1) {
          dirb.insert(tmp);
          if (dirb.size() == 2) {
            f = false;
            break;
          }
        }
      }

      if (f) {
        set<int> dirw;
        rep(k, m) {
          if (k == j)
            continue;
          int tmp = ccw(b[i], w[j], w[k]);
          if (tmp == 0 || tmp == 2) {
            f = false;
            break;
          }
          if (abs(tmp) == 1) {
            dirw.insert(tmp);
            if (dirw.size() == 2) {
              f = false;
              break;
            }
          }
        }
        if (f && (dirb.size() == 0 || dirw.size() == 0 ||
                  *dirb.begin() != *dirw.begin()))
          ans = true;
      }
      if (ans)
        break;
    }

    cout << (ans ? "YES" : "NO") << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define fs first
#define sc second
using namespace std;
typedef pair<int, int> P;

// for vector
inline int norm(const P &a) { return a.fs * a.fs + a.sc * a.sc; }
inline int dot(const P &a, const P &b) { return a.fs * b.fs + a.sc * b.sc; }
inline int cross(const P &a, const P &b) { return a.fs * b.sc - a.sc * b.fs; }

inline int ccw(const P &a, P b, P c) {
  b.fs -= a.fs;
  b.sc -= a.sc;
  c.fs -= a.fs;
  c.sc -= a.sc;
  if (cross(b, c) > 0)
    return 1; // counter clockwise
  if (cross(b, c) < 0)
    return -1; // clockwise
  if (dot(b, c) < -0)
    return 2; // c--a--b on line
  if (norm(b) < norm(c))
    return -2; // a--b--c on line
  return 0;    // on segment
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);

  int n, m;
  P b[110], w[110];
  while (cin >> n >> m, n || m) {
    rep(i, n) cin >> b[i].fs >> b[i].sc;
    rep(i, m) cin >> w[i].fs >> w[i].sc;

    bool ans = false;
    rep(i, n) rep(j, i) {
      bool f = true;
      set<int> dirb;
      rep(k, n) {
        if (k == i || k == j)
          continue;
        int tmp = ccw(b[i], b[j], b[k]);
        if (tmp == 0) {
          f = false;
          break;
        }
        if (abs(tmp) == 1)
          dirb.insert(tmp);
      }
      if (dirb.size() == 2)
        f = false;

      set<int> dirw;
      rep(k, m) {
        int tmp = ccw(b[i], b[j], w[k]);
        if (abs(tmp) != 1) {
          f = false;
          break;
        }
        dirw.insert(tmp);
      }
      if (dirw.size() == 2)
        f = false;
      if (f && (dirb.size() == 0 || dirw.size() == 0 ||
                *dirb.begin() != *dirw.begin()))
        ans = true;
    }

    rep(i, m) rep(j, i) {
      bool f = true;
      set<int> dirw;
      rep(k, m) {
        if (k == i || k == j)
          continue;
        int tmp = ccw(w[i], w[j], w[k]);
        if (tmp == 0) {
          f = false;
          break;
        }
        if (abs(tmp) == 1)
          dirw.insert(tmp);
      }
      if (dirw.size() == 2)
        f = false;

      set<int> dirb;
      rep(k, n) {
        int tmp = ccw(w[i], w[j], b[k]);
        if (abs(tmp) != 1) {
          f = false;
          break;
        }
        dirb.insert(tmp);
      }
      if (dirb.size() == 2)
        f = false;
      if (f && (dirb.size() == 0 || dirw.size() == 0 ||
                *dirb.begin() != *dirw.begin()))
        ans = true;
    }

    rep(i, n) rep(j, m) {
      bool f = true;
      set<int> dirb;
      rep(k, n) {
        if (k == i)
          continue;
        int tmp = ccw(b[i], w[j], b[k]);
        if (tmp == 0 || tmp == -2) {
          f = false;
          break;
        }
        if (abs(tmp) == 1)
          dirb.insert(tmp);
        dirb.insert(tmp);
      }
      if (dirb.size() == 2)
        f = false;

      set<int> dirw;
      rep(k, m) {
        if (k == j)
          continue;
        int tmp = ccw(b[i], w[j], w[k]);
        if (tmp == 0 || tmp == 2) {
          f = false;
          break;
        }
        if (abs(tmp) == 1)
          dirw.insert(tmp);
      }
      if (dirw.size() == 2)
        f = false;

      if (f && (dirb.size() == 0 || dirw.size() == 0 ||
                *dirb.begin() != *dirw.begin()))
        ans = true;
    }

    cout << (ans ? "YES" : "NO") << endl;
  }
}