Problem p00633 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <complex>
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;
const double EPS = 1e-10;
const double INF = 1e12;
const double PI = acos(-1);
#define EQ(n, m) (abs((n) - (m)) < EPS)
#define X real()
#define Y imag()

typedef complex<double> P;
typedef vector<P> VP;
struct C {
  P p;
  double r;
  C(const P &p, const double &r) : p(p), r(r) {}
  C() {}
};

P unit(const P &p) { return p / abs(p); }
P rotate(const P &p, double rad) { return p * P(cos(rad), sin(rad)); }
VP crosspointCC(C a, C b) {
  VP ret;
  if (a.r < b.r)
    swap(a, b);
  double dist = abs(b.p - a.p);
  P dir = a.r * unit(b.p - a.p);
  if (EQ(dist, a.r + b.r) || EQ(dist, a.r - b.r)) {
    ret.push_back(a.p + dir);
  } else if (a.r - b.r < dist && dist < a.r + b.r) {
    double cos = (a.r * a.r + dist * dist - b.r * b.r) / (2 * a.r * dist);
    double sin = sqrt(1 - cos * cos);
    ret.push_back(a.p + dir * P(cos, sin));
    ret.push_back(a.p + dir * P(cos, -sin));
  }
  return ret;
}

int main() {
  while (1) {
    int n;
    cin >> n;
    if (n == 0)
      break;

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

    double ans = 0;
    for (int i = 0; i < n; i++) {
      vector<double> ar{-PI, PI};
      for (int j = 0; j < n; j++) {
        if (i == j)
          continue;
        VP ret = crosspointCC(c[i], c[j]);
        for (int k = 0; k < (int)ret.size(); k++) {
          ar.push_back(arg(ret[k] - c[i].p));
        }
      }
      sort(ar.begin(), ar.end());
      ar.erase(unique(ar.begin(), ar.end()), ar.end());
      for (int j = 0; j < (int)ar.size() - 1; j++) {
        double mid = (ar[j] + ar[j + 1]) / 2;
        P p = c[i].p + rotate(P(c[i].r, 0), mid);
        bool out = true;
        for (int k = 0; k < n; k++) {
          if (abs(p - c[k].p) + EPS < c[k].r) {
            out = false;
          }
        }
        if (out) {
          ans += (ar[j + 1] - ar[j]) * c[i].r;
        }
      }
    }
    cout << fixed << setprecision(10);
    cout << ans << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <complex>
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;
const double EPS = 1e-10;
const double INF = 1e12;
const double PI = acos(-1);
#define EQ(n, m) (abs((n) - (m)) < EPS)
#define X real()
#define Y imag()

typedef complex<double> P;
typedef vector<P> VP;
struct C {
  P p;
  double r;
  C(const P &p, const double &r) : p(p), r(r) {}
  C() {}
};

P unit(const P &p) { return p / abs(p); }
P rotate(const P &p, double rad) { return p * P(cos(rad), sin(rad)); }
VP crosspointCC(C a, C b) {
  VP ret;
  if (a.r < b.r)
    swap(a, b);
  double dist = abs(b.p - a.p);
  P dir = a.r * unit(b.p - a.p);
  if (EQ(dist, a.r + b.r) || EQ(dist, a.r - b.r)) {
    ret.push_back(a.p + dir);
  } else if (a.r - b.r < dist && dist < a.r + b.r) {
    double cos = (a.r * a.r + dist * dist - b.r * b.r) / (2 * a.r * dist);
    double sin = sqrt(1 - cos * cos);
    ret.push_back(a.p + dir * P(cos, sin));
    ret.push_back(a.p + dir * P(cos, -sin));
  }
  return ret;
}

int main() {
  while (1) {
    int n;
    cin >> n;
    if (n == 0)
      break;

    vector<C> c(n);
    for (int i = 0; i < n; i++) {
      double x, y, r;
      cin >> x >> y >> r;
      c[i] = C(P(x, y), r);
    }

    double ans = 0;
    for (int i = 0; i < n; i++) {
      vector<double> ar{-PI, PI};
      for (int j = 0; j < n; j++) {
        if (i == j)
          continue;
        VP ret = crosspointCC(c[i], c[j]);
        for (int k = 0; k < (int)ret.size(); k++) {
          ar.push_back(arg(ret[k] - c[i].p));
        }
      }
      sort(ar.begin(), ar.end());
      ar.erase(unique(ar.begin(), ar.end()), ar.end());
      for (int j = 0; j < (int)ar.size() - 1; j++) {
        double mid = (ar[j] + ar[j + 1]) / 2;
        P p = c[i].p + rotate(P(c[i].r, 0), mid);
        bool out = true;
        for (int k = 0; k < n; k++) {
          if (abs(p - c[k].p) + EPS < c[k].r) {
            out = false;
          }
        }
        if (out) {
          ans += (ar[j + 1] - ar[j]) * c[i].r;
        }
      }
    }
    cout << fixed << setprecision(10);
    cout << ans << endl;
  }
  return 0;
}