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

#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
long double EPS = (long double)1e-7;
struct Point {
  long double px, py;
};
Point r1[100], r2[100];
int n, m;
vector<pair<Point, int>> vec, vec2;
inline bool solve(Point a1, Point a2) {
  if (a1.px > a2.px)
    swap(a1, a2);
  if (a1.px == a2.px) {
    int colour = 0;
    bool r[3] = {false, false, false};
    for (int i = 0; i < vec2.size(); i++) {
      if (r[1] == true && r[2] == true)
        return false;
      if (colour == 0) {
        if (vec2[i].first.px < a1.px) {
          colour = 3 - vec2[i].second;
        } else if (vec2[i].first.px == a1.px) {
          r[vec2[i].second] = true;
        } else if (vec2[i].first.px > a1.px) {
          colour = vec2[i].second;
        }
      } else {
        if (colour != 3 - vec2[i].second && vec2[i].first.px < a1.px) {
          return false;
        } else if (vec2[i].first.px == a1.px) {
          r[vec2[i].second] = true;
        } else if (colour != vec2[i].second && vec2[i].first.px > a1.px) {
          return false;
        }
      }
    }
    if (r[1] == true && r[2] == true)
      return false;
    return true;
  } else {
    long double KT = (a2.py - a1.py) / (a2.px - a1.px);
    long double YY = a1.py - a1.px * KT;
    int colour = 0;
    bool r[3] = {false, false, false};
    for (int i = 0; i < vec2.size(); i++) {
      if (r[1] == true && r[2] == true)
        return false;
      long double height = YY + KT * vec2[i].first.px;
      if (colour == 0) {
        if (vec2[i].first.py < height) {
          colour = 3 - vec2[i].second;
        } else if (vec2[i].first.py == height) {
          r[vec2[i].second] = true;
        } else if (vec2[i].first.py > height) {
          colour = vec2[i].second;
        }
      } else {
        if (colour != 3 - vec2[i].second && vec2[i].first.py < height) {
          return false;
        } else if (vec2[i].first.py == height) {
          r[vec2[i].second] = true;
        } else if (colour != vec2[i].second && vec2[i].first.py > height) {
          return false;
        }
      }
    }
    if (r[1] == true && r[2] == true)
      return false;
    return true;
  }
}
int main() {
  while (true) {
    vec.clear();
    vec2.clear();
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> r1[i].px >> r1[i].py;
      vec2.push_back(make_pair(r1[i], 1));
      for (long double j = -1.0l; j <= 1.0l; j += 1.0l) {
        for (long double k = -1.0l; k <= 1.0l; k += 1.0l) {
          if (fabs(j) + fabs(k) > 1.9l)
            continue;
          vec.push_back(
              make_pair(Point{r1[i].px + j * EPS, r1[i].py + k * EPS}, 1));
        }
      }
    }
    for (int i = 0; i < m; i++) {
      cin >> r2[i].px >> r2[i].py;
      vec2.push_back(make_pair(r2[i], 2));
      for (long double j = -1.0l; j <= 1.0l; j += 1.0l) {
        for (long double k = -1.0l; k <= 1.0l; k += 1.0l) {
          if (fabs(j) + fabs(k) > 1.9l)
            continue;
          vec.push_back(
              make_pair(Point{r2[i].px + j * EPS, r2[i].py + k * EPS}, 2));
        }
      }
    }
    bool ans = false;
    for (int i = 0; i < vec.size(); i++) {
      for (int j = i + 1; j < vec.size(); j++) {
        if (fabs(vec[i].first.px - vec[j].first.px) +
                fabs(vec[i].first.py - vec[j].first.py) <
            EPS * 6)
          continue;
        bool p1 = solve(vec[i].first, vec[j].first);
        if (p1 == true) {
          ans = true;
          break;
        }
      }
    }
    if (ans == true)
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
long double EPS = (long double)1e-7;
struct Point {
  long double px, py;
};
Point r1[100], r2[100];
int n, m;
vector<pair<Point, int>> vec, vec2;
inline bool solve(Point a1, Point a2) {
  if (a1.px > a2.px)
    swap(a1, a2);
  if (a1.px == a2.px) {
    int colour = 0;
    bool r[3] = {false, false, false};
    for (int i = 0; i < vec2.size(); i++) {
      if (r[1] == true && r[2] == true)
        return false;
      if (colour == 0) {
        if (vec2[i].first.px < a1.px) {
          colour = 3 - vec2[i].second;
        } else if (vec2[i].first.px == a1.px) {
          r[vec2[i].second] = true;
        } else if (vec2[i].first.px > a1.px) {
          colour = vec2[i].second;
        }
      } else {
        if (colour != 3 - vec2[i].second && vec2[i].first.px < a1.px) {
          return false;
        } else if (vec2[i].first.px == a1.px) {
          r[vec2[i].second] = true;
        } else if (colour != vec2[i].second && vec2[i].first.px > a1.px) {
          return false;
        }
      }
    }
    if (r[1] == true && r[2] == true)
      return false;
    return true;
  } else {
    long double KT = (a2.py - a1.py) / (a2.px - a1.px);
    long double YY = a1.py - a1.px * KT;
    int colour = 0;
    bool r[3] = {false, false, false};
    for (int i = 0; i < vec2.size(); i++) {
      if (r[1] == true && r[2] == true)
        return false;
      long double height = YY + KT * vec2[i].first.px;
      if (colour == 0) {
        if (vec2[i].first.py < height) {
          colour = 3 - vec2[i].second;
        } else if (vec2[i].first.py == height) {
          r[vec2[i].second] = true;
        } else if (vec2[i].first.py > height) {
          colour = vec2[i].second;
        }
      } else {
        if (colour != 3 - vec2[i].second && vec2[i].first.py < height) {
          return false;
        } else if (vec2[i].first.py == height) {
          r[vec2[i].second] = true;
        } else if (colour != vec2[i].second && vec2[i].first.py > height) {
          return false;
        }
      }
    }
    if (r[1] == true && r[2] == true)
      return false;
    return true;
  }
}
int main() {
  while (true) {
    vec.clear();
    vec2.clear();
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> r1[i].px >> r1[i].py;
      vec2.push_back(make_pair(r1[i], 1));
      for (long double j = -1.0l; j <= 1.0l; j += 1.0l) {
        for (long double k = -1.0l; k <= 1.0l; k += 1.0l) {
          vec.push_back(
              make_pair(Point{r1[i].px + j * EPS, r1[i].py + k * EPS}, 1));
        }
      }
    }
    for (int i = 0; i < m; i++) {
      cin >> r2[i].px >> r2[i].py;
      vec2.push_back(make_pair(r2[i], 2));
      for (long double j = -1.0l; j <= 1.0l; j += 1.0l) {
        for (long double k = -1.0l; k <= 1.0l; k += 1.0l) {
          vec.push_back(
              make_pair(Point{r2[i].px + j * EPS, r2[i].py + k * EPS}, 2));
        }
      }
    }
    bool ans = false;
    for (int i = 0; i < vec.size(); i++) {
      for (int j = i + 1; j < vec.size(); j++) {
        if (fabs(vec[i].first.px - vec[j].first.px) +
                fabs(vec[i].first.py - vec[j].first.py) <
            EPS * 6)
          continue;
        bool p1 = solve(vec[i].first, vec[j].first);
        if (p1 == true) {
          ans = true;
          break;
        }
      }
    }
    if (ans == true)
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  }
  return 0;
}