Problem p01318 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef complex<double> P;
typedef pair<double, double> P1;
const double EPS = 1e-10;
typedef pair<P, P> L;
int n;

// kaitenn
P rotate(P p, double rad) {
  double x = real(p) * cos(rad) - imag(p) * sin(rad);
  double y = real(p) * sin(rad) + imag(p) * cos(rad);
  return P(x, y);
}

//???
struct Circle {
  P c;      //?????????
  double r; //??????
  Circle(P c = P(0, 0), double r = 0) : c(c), r(r) {}
};
Circle c[2][51];

L getIntangent(Circle a, Circle b, double f = 1.0) {
  P base = b.c - a.c;
  double w = a.r + b.r;
  double h = sqrt(max(0.0, norm(base) - w * w));
  P k = base * P(w, h * f) / norm(base);
  return L(a.c + k * a.r, b.c - k * b.r);
}

L getOuttangent(Circle a, Circle b, double f = 1.0) {
  P base = b.c - a.c;
  double h = b.r - a.r;
  double w = sqrt(max(0.0, norm(base) - h * h));
  P k = base * P(w, h * f) / norm(base) * P(0, f);
  return L(a.c + k * a.r, b.c + k * b.r);
}

vector<L> tangentCC(Circle a, Circle b) {
  double dis = abs(a.c - b.c), R = a.r + b.r;
  vector<L> res;
  if (dis > R + EPS)
    res.push_back(getIntangent(a, b));
  if (dis > R - EPS)
    res.push_back(getIntangent(a, b, -1));
  if (dis > abs(a.r - b.r) + EPS)
    res.push_back(getOuttangent(a, b));
  if (dis > abs(a.r - b.r) - EPS)
    res.push_back(getOuttangent(a, b, -1));
  return res;
}

int cal(P a, P b) {
  int res = 0;
  b -= a;
  for (int i = 0; i < n; i++) {
    P t = (c[0][i].c - a) * conj(b) / abs(b);
    if (c[0][i].r <= abs(t.imag()) + EPS && abs(t.imag()) <= c[1][i].r + EPS)
      res++;
  }
  return res;
}

int main() {
  while (cin >> n, n) {
    for (int i = 0, a, b, r, m; i < n; i++) {
      cin >> a >> b >> r >> m;
      c[0][i] = Circle(P(a, b), r);
      c[1][i] = Circle(P(a, b), r + m);
    }

    int ans = 1;
    for (int i = 0; i < n; i++)
      for (int j = i + 1; j < n; j++) {
        for (int k = 0; k < 4; k++) {
          vector<L> res = tangentCC(c[k & 1][i], c[(k >> 1) & 1][j]);
          for (int l = 0; l < res.size(); l++)
            ans = max(ans, cal(res[l].first, res[l].second));
        }
      }
    cout << ans << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef complex<double> P;
typedef pair<double, double> P1;
const double EPS = 1e-10;
typedef pair<P, P> L;
int n, m[51];

// kaitenn
P rotate(P p, double rad) {
  double x = real(p) * cos(rad) - imag(p) * sin(rad);
  double y = real(p) * sin(rad) + imag(p) * cos(rad);
  return P(x, y);
}

//???
struct Circle {
  P c;      //?????????
  double r; //??????
  Circle(P c = P(0, 0), double r = 0) : c(c), r(r) {}
};
Circle c[2][51];

L getIntangent(Circle a, Circle b, double f = 1.0) {
  P base = b.c - a.c;
  double w = a.r + b.r;
  double h = sqrt(max(0.0, norm(base) - w * w));
  P k = base * P(w, h * f) / norm(base);
  return L(a.c + k * a.r, b.c - k * b.r);
}

L getOuttangent(Circle a, Circle b, double f = 1.0) {
  P base = b.c - a.c;
  double h = b.r - a.r;
  double w = sqrt(max(0.0, norm(base) - h * h));
  P k = base * P(w, h * f) / norm(base) * P(0, f);
  return L(a.c + k * a.r, b.c + k * b.r);
}

vector<L> tangentCC(Circle a, Circle b) {
  double dis = abs(a.c - b.c), R = a.r + b.r;
  vector<L> res;
  if (dis > R + EPS)
    res.push_back(getIntangent(a, b));
  if (dis > R - EPS)
    res.push_back(getIntangent(a, b, -1));
  if (dis > abs(a.r - b.r) + EPS)
    res.push_back(getOuttangent(a, b));
  if (dis > abs(a.r - b.r) - EPS)
    res.push_back(getOuttangent(a, b, -1));
  return res;
}

int cal(P a, P b) {
  int res = 0;
  b -= a;
  for (int i = 0; i < n; i++) {
    P t = (c[0][i].c - a) * conj(b) / abs(b);
    if (c[0][i].r <= abs(t.imag()) + EPS && abs(t.imag()) <= c[1][i].r + EPS)
      res++;
  }
  return res;
}

int main() {
  while (cin >> n, n) {

    for (int i = 0, a, b, r, m; i < n; i++) {
      cin >> a >> b >> r >> m;
      c[0][i] = Circle(P(a, b), r);
      c[1][i] = Circle(P(a, b), r + m);
    }

    int ans = 1;
    for (int i = 0; i < n; i++)
      for (int j = i + 1; j < n; j++) {
        for (int k = 0; k < 4; k++) {
          vector<L> res = tangentCC(c[k & 1][i], c[k & 2][j]);
          for (int l = 0; l < res.size(); l++)
            ans = max(ans, cal(res[l].first, res[l].second));
        }
      }
    cout << ans << endl;
  }

  return 0;
}