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

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<long long, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<long long, long long> ll_ll;
typedef pair<double, double> d_d;

#define PI 3.141592653589793238462643383279
#define mod 1000000007LL
#define rep(i, n) for (i = 0; i < n; ++i)
#define rep1(i, n) for (i = 1; i < n; ++i)
#define rep2d(i, j, n)                                                         \\
  for (i = 0; i < n; ++i)                                                      \\
    for (j = i + 1; j < n; ++j)
#define per(i, n) for (i = n - 1; i > -1; --i)
#define int(x)                                                                 \\
  int x;                                                                       \\
  scanf("%d", &x)
#define int2(x, y)                                                             \\
  int x, y;                                                                    \\
  scanf("%d%d", &x, &y)
#define int3(x, y, z)                                                          \\
  int x, y, z;                                                                 \\
  scanf("%d%d%d", &x, &y, &z)
#define scn(n, a) rep(i, n) cin >> a[i]
#define sc2n(n, a, b) rep(i, n) cin >> a[i] >> b[i]
#define pri(x) cout << x << "\\n"
#define pri2(x, y) cout << x << " " << y << "\\n"
#define pri3(x, y, z) cout << x << " " << y << " " << z << "\\n"
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define endl "\\n"
#define kabe puts("---------------------------")
#define kara puts("")
#define debug(x) cout << " --- " << x << "\\n"
#define debug2(x, y) cout << " --- " << x << " " << y << "\\n"
#define debug3(x, y, z) cout << " --- " << x << " " << y << " " << z << "\\n"
#define X first
#define Y second
#define eps 0.00000000001
#define prid(x) printf("%.15lf\\n", x)

double xmax, xmin, ymax, ymin;
struct vec {
  double x, y;
  vec operator+(const vec &a) const { return (vec){x + a.x, y + a.y}; }
  vec operator-(const vec &a) const { return (vec){x - a.x, y - a.y}; }
  vec sca(double t) { return (vec){t * x, t * y}; }
  double dot(vec a) { return x * a.x + y * a.y; }
  double cross(vec a) { return x * a.y - y * a.x; }
  double norm() { return sqrt(x * x + y * y); }
  double norm2() { return x * x + y * y; }
  // double ppdist(vec p){ return sqrt( (p.x - x) * (p.x - x) + (p.y - y) * (p.y
  // - y) ); } double ppdist2(vec p){ return (p.x - x) * (p.x - x) + (p.y - y) *
  // (p.y - y); }
};
struct line {
  vec a, b;
  vec getvec() { return b - a; }
  vec proj(vec p) {
    return a + (b - a).sca((p - a).dot(b - a) / (b - a).dot(b - a));
  }
  vec vref(vec p) { return proj(p).sca(2.0) - p; }
  int ccw(vec p) {
    vec q = p - a, ba = b - a;
    if (ba.cross(q) > 0)
      return 1; // ccw
    if (ba.cross(q) < 0)
      return -1; // cw
    if (ba.dot(q) < 0)
      return -2; // back
    if (ba.dot(ba) < q.dot(q))
      return 2; // front
    return 0;   // on
  }
  bool paral(line l) { return abs(l.getvec().cross(getvec())) < eps; }
  bool orth(line l) { return abs(l.getvec().dot(getvec())) < eps; }
  bool intersec(line l) {
    bool res0 = (ccw(l.a) * ccw(l.b) == 4); // syukutai
    bool res1 = (getvec().cross(l.a - a) * getvec().cross(l.b - a)) <= eps;
    bool res2 = (l.getvec().cross(a - l.a) * l.getvec().cross(b - l.a)) <= eps;
    return !res0 && res1 && res2;
  }
  vec crosspoint(line l) {
    return a + getvec().sca((l.a - a).cross(l.getvec()) /
                            getvec().cross(l.getvec()));
  }
  double pldist(vec p) {
    double res = min((a - p).norm2(), (b - p).norm2());
    vec h = proj(p);
    if ((a - h).dot(b - h) < 0)
      res = min(res, (h - p).norm2());
    return sqrt(res);
  }
  double lldist(line l) {
    if (intersec(l))
      return 0.0;
    return min(min(pldist(l.a), pldist(l.b)), min(l.pldist(a), l.pldist(b)));
  }
};
struct polygon {
  vector<vec> p; // ccw
  double area() {
    double res = 0.0;
    for (int i = 0; i < p.size(); ++i)
      res += p[i].cross(p[(i + 1) % p.size()]);
    return res / 2.0;
  }
  bool isconv() {
    for (int i = 0; i < p.size(); ++i)
      if ((p[(i + 1) % p.size()] - p[i]).cross(p[(i + 2) % p.size()] - p[i]) <
          -eps)
        return false;
    return true;
  }
  int isin(vec a) {
    line l = (line){a, a + (vec){xmax - xmin, 0.0}};
    int cnt = 0, n = p.size();
    for (int i = 0; i < n; ++i) {
      line tmp = (line){p[i], p[(i + 1) % n]};
      if (tmp.ccw(a) == 0)
        return 1; // on line
      if (l.intersec(tmp)) {
        ++cnt;
        if (l.ccw(p[i]) == 0 &&
            l.ccw(p[(i + 1) % n]) * l.ccw(p[(i + n - 1) % n]) == -1)
          ++cnt;
        if (l.paral(tmp)) {
          ++cnt;
          if (l.ccw(p[(i + 2) % n]) * l.ccw(p[(i + n - 1) % n]) == -1)
            ++cnt;
        }
      }
    }
    return (cnt & 1) * 2; // 2:in 0:out
  }
};
struct circle {
  vec o;
  double r;
  int intersec(circle c) {
    double d = (o - c.o).norm(), rp = r + c.r, rm = abs(r - c.r);
    if (abs(rp - d) < eps)
      return 3;
    if (rp < d + eps)
      return 4;
    if (abs(rm - d) < eps)
      return 1;
    if (rm < d + eps)
      return 2;
    return 0;
  }
};

signed main(void) {
  int i, j, k;
  circle c, d;
  cin >> c.o.x >> c.o.y >> c.r >> d.o.x >> d.o.y >> d.r;
  pri(c.intersec(d));

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<long long, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<long long, long long> ll_ll;
typedef pair<double, double> d_d;

#define PI 3.141592653589793238462643383279
#define mod 1000000007LL
#define rep(i, n) for (i = 0; i < n; ++i)
#define rep1(i, n) for (i = 1; i < n; ++i)
#define rep2d(i, j, n)                                                         \\
  for (i = 0; i < n; ++i)                                                      \\
    for (j = i + 1; j < n; ++j)
#define per(i, n) for (i = n - 1; i > -1; --i)
#define int(x)                                                                 \\
  int x;                                                                       \\
  scanf("%d", &x)
#define int2(x, y)                                                             \\
  int x, y;                                                                    \\
  scanf("%d%d", &x, &y)
#define int3(x, y, z)                                                          \\
  int x, y, z;                                                                 \\
  scanf("%d%d%d", &x, &y, &z)
#define scn(n, a) rep(i, n) cin >> a[i]
#define sc2n(n, a, b) rep(i, n) cin >> a[i] >> b[i]
#define pri(x) cout << x << "\\n"
#define pri2(x, y) cout << x << " " << y << "\\n"
#define pri3(x, y, z) cout << x << " " << y << " " << z << "\\n"
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define endl "\\n"
#define kabe puts("---------------------------")
#define kara puts("")
#define debug(x) cout << " --- " << x << "\\n"
#define debug2(x, y) cout << " --- " << x << " " << y << "\\n"
#define debug3(x, y, z) cout << " --- " << x << " " << y << " " << z << "\\n"
#define X first
#define Y second
#define eps 0.00000000001
#define prid(x) printf("%.15lf\\n", x)

double xmax, xmin, ymax, ymin;
struct vec {
  double x, y;
  vec operator+(const vec &a) const { return (vec){x + a.x, y + a.y}; }
  vec operator-(const vec &a) const { return (vec){x - a.x, y - a.y}; }
  vec sca(double t) { return (vec){t * x, t * y}; }
  double dot(vec a) { return x * a.x + y * a.y; }
  double cross(vec a) { return x * a.y - y * a.x; }
  double norm() { return sqrt(x * x + y * y); }
  double norm2() { return x * x + y * y; }
  // double ppdist(vec p){ return sqrt( (p.x - x) * (p.x - x) + (p.y - y) * (p.y
  // - y) ); } double ppdist2(vec p){ return (p.x - x) * (p.x - x) + (p.y - y) *
  // (p.y - y); }
};
struct line {
  vec a, b;
  vec getvec() { return b - a; }
  vec proj(vec p) {
    return a + (b - a).sca((p - a).dot(b - a) / (b - a).dot(b - a));
  }
  vec vref(vec p) { return proj(p).sca(2.0) - p; }
  int ccw(vec p) {
    vec q = p - a, ba = b - a;
    if (ba.cross(q) > 0)
      return 1; // ccw
    if (ba.cross(q) < 0)
      return -1; // cw
    if (ba.dot(q) < 0)
      return -2; // back
    if (ba.dot(ba) < q.dot(q))
      return 2; // front
    return 0;   // on
  }
  bool paral(line l) { return abs(l.getvec().cross(getvec())) < eps; }
  bool orth(line l) { return abs(l.getvec().dot(getvec())) < eps; }
  bool intersec(line l) {
    bool res0 = (ccw(l.a) * ccw(l.b) == 4); // syukutai
    bool res1 = (getvec().cross(l.a - a) * getvec().cross(l.b - a)) <= eps;
    bool res2 = (l.getvec().cross(a - l.a) * l.getvec().cross(b - l.a)) <= eps;
    return !res0 && res1 && res2;
  }
  vec crosspoint(line l) {
    return a + getvec().sca((l.a - a).cross(l.getvec()) /
                            getvec().cross(l.getvec()));
  }
  double pldist(vec p) {
    double res = min((a - p).norm2(), (b - p).norm2());
    vec h = proj(p);
    if ((a - h).dot(b - h) < 0)
      res = min(res, (h - p).norm2());
    return sqrt(res);
  }
  double lldist(line l) {
    if (intersec(l))
      return 0.0;
    return min(min(pldist(l.a), pldist(l.b)), min(l.pldist(a), l.pldist(b)));
  }
};
struct polygon {
  vector<vec> p; // ccw
  double area() {
    double res = 0.0;
    for (int i = 0; i < p.size(); ++i)
      res += p[i].cross(p[(i + 1) % p.size()]);
    return res / 2.0;
  }
  bool isconv() {
    for (int i = 0; i < p.size(); ++i)
      if ((p[(i + 1) % p.size()] - p[i]).cross(p[(i + 2) % p.size()] - p[i]) <
          -eps)
        return false;
    return true;
  }
  int isin(vec a) {
    line l = (line){a, a + (vec){xmax - xmin, 0.0}};
    int cnt = 0, n = p.size();
    for (int i = 0; i < n; ++i) {
      line tmp = (line){p[i], p[(i + 1) % n]};
      if (tmp.ccw(a) == 0)
        return 1; // on line
      if (l.intersec(tmp)) {
        ++cnt;
        if (l.ccw(p[i]) == 0 &&
            l.ccw(p[(i + 1) % n]) * l.ccw(p[(i + n - 1) % n]) == -1)
          ++cnt;
        if (l.paral(tmp)) {
          ++cnt;
          if (l.ccw(p[(i + 2) % n]) * l.ccw(p[(i + n - 1) % n]) == -1)
            ++cnt;
        }
      }
    }
    return (cnt & 1) * 2; // 2:in 0:out
  }
};
struct circle {
  vec o;
  double r;
  int intersec(circle c) {
    double d = (o - c.o).norm(), rp = r + c.r, rm = abs(r - c.r);
    if (abs(rp - d) < eps)
      return 3;
    if (rp < d + eps)
      return 4;
    if (abs(rm - d) < eps)
      return 1;
    if (rm < d + eps)
      return 2;
    return 0;
  }
};

signed main(void) {
  int i, j, k;
  for (;;) {
    circle c, d;
    cin >> c.o.x >> c.o.y >> c.r >> d.o.x >> d.o.y >> d.r;
    pri(c.intersec(d));
  }

  return 0;
}