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

#include <bits/stdc++.h>
using namespace std;

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define show(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define show(x)
#endif

typedef long long int ll;
typedef pair<int, int> pii;
template <typename T> using vec = std::vector<T>;

const int inf = 1 << 30;
const long long int infll = 1LL << 62;
const double eps = 1e-7;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

vector<long double> operator-(const vector<long double> &a,
                              const vector<long double> &b) {
  vector<long double> res(a.size());
  rep(i, 0, a.size()) res[i] = a[i] - b[i];
  return res;
}

void solve(int n, int m, int r) {
  vector<long double> sx(n), sy(n), sz(n), sr(n);
  vector<long double> tx(m), ty(m), tz(m), tb(m);
  long double ex, ey, ez;
  rep(i, 0, n) cin >> sx[i] >> sy[i] >> sz[i] >> sr[i];
  rep(i, 0, m) cin >> tx[i] >> ty[i] >> tz[i] >> tb[i];
  cin >> ex >> ey >> ez;

  auto pow_distance = [](const vector<long double> &a,
                         const vector<long double> &b) {
    long double res = 0;
    rep(i, 0, 3) res += (a[i] - b[i]) * (a[i] - b[i]);
    return res;
  };

  auto dot = [](const vector<long double> &a, const vector<long double> &b) {
    long double res = 0;
    rep(i, 0, 3) res += a[i] * b[i];
    return res;
  };

  vector<vector<int>> remove_list(m);
  vector<long double> o = {ex, ey, ez};
  rep(i, 0, m) {
    vector<long double> a = {tx[i], ty[i], tz[i]};
    rep(j, 0, n) {
      vector<long double> b = {sx[j], sy[j], sz[j]};
      long double pow_d1 = pow_distance(a, b);
      long double pow_d2 = pow_distance(o, b);
      bool f1 = pow_d1 <= sr[j] * sr[j], f2 = pow_d2 <= sr[j] * sr[j];
      if ((f1 and !f2) or (!f1 and f2)) {
        remove_list[i].push_back(j);
        continue;
      }
      if (f1 and f2)
        continue;

      // a->a,b->o,c->b
      if (dot(o - a, b - a) < eps) {
        if (pow_d1 <= sr[j] * sr[j])
          remove_list[i].push_back(j);
        continue;
      }
      if (dot(a - o, b - o) < eps) {
        if (pow_d2 <= sr[j] * sr[j])
          remove_list[i].push_back(i);
        continue;
      }
      long double pow_a_plus_b = pow_distance(a, o);
      long double pow_a_minus_b =
          (pow_d1 - pow_d2) * (pow_d1 - pow_d2) / pow_a_plus_b;
      long double pow_d =
          (2 * (pow_d1 + pow_d2) - pow_a_plus_b - pow_a_minus_b) / 4;
      if (pow_d <= sr[j] * sr[j])
        remove_list[i].push_back(j);
    }
  }

  long double ans = 0;
  rep(i, 0, 1 << m) {
    long double sum = 0;
    rep(j, 0, m) if ((i >> j) & 1) {
      vector<long double> p = {tx[j], ty[j], tz[j]};
      sum += tb[j] / pow_distance(p, o);
    }
    if (sum <= ans)
      continue;
    bool ok = true;
    set<int> removed;
    rep(j, 0, m) {
      if (!((i >> j) & 1))
        continue;
      for (int id : remove_list[j])
        if (!removed.count(id))
          removed.insert(id);
      if (removed.size() > r) {
        ok = false;
        break;
      }
    }
    if (!ok)
      continue;
    ans = sum;
  }
  cout << ans << endl;
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(10);
  while (true) {
    int n, m, r;
    cin >> n >> m >> r;
    if (!n and !m and !r)
      break;
    solve(n, m, r);
  }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define show(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define show(x)
#endif

typedef long long int ll;
typedef pair<int, int> pii;
template <typename T> using vec = std::vector<T>;

const int inf = 1 << 30;
const long long int infll = 1LL << 62;
const double eps = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

void solve(int n, int m, int r) {
  vector<long double> sx(n), sy(n), sz(n), sr(n);
  vector<long double> tx(m), ty(m), tz(m), tb(m);
  long double ex, ey, ez;
  rep(i, 0, n) cin >> sx[i] >> sy[i] >> sz[i] >> sr[i];
  rep(i, 0, m) cin >> tx[i] >> ty[i] >> tz[i] >> tb[i];
  cin >> ex >> ey >> ez;

  auto pow_distance = [](long double x1, long double y1, long double z1,
                         long double x2, long double y2, long double z2) {
    return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) +
           (z1 - z2) * (z1 - z2);
  };

  vector<vector<int>> remove_list(m);
  rep(i, 0, m) rep(j, 0, n) {
    long double pow_d1 = pow_distance(tx[i], ty[i], tz[i], sx[j], sy[j], sz[j]);
    long double pow_d2 = pow_distance(ex, ey, ez, sx[j], sy[j], sz[j]);
    bool f1 = pow_d1 <= sr[j] * sr[j], f2 = pow_d2 <= sr[j] * sr[j];
    if ((f1 and !f2) or (!f1 and f2)) {
      remove_list[i].push_back(j);
      continue;
    }
    if (f1 and f2)
      continue;
    show(i);
    show(j);
    long double pow_a_plus_b = pow_distance(tx[i], ty[i], tz[i], ex, ey, ez);
    long double pow_a_minus_b =
        (pow_d1 - pow_d2) * (pow_d1 - pow_d2) / pow_a_plus_b;
    long double pow_d =
        (2 * (pow_d1 + pow_d2) - pow_a_plus_b - pow_a_minus_b) / 4;
    show(pow_d);
    cerr << pow_d1 << " " << pow_d2 << endl;
    cerr << pow_a_plus_b << " " << pow_distance(tx[i], ty[i], tz[i], ex, ey, ez)
         << endl;
    // if(pow_a_plus_b>pow_distance(tx[i],ty[i],tz[i],ex,ey,ez))
    // pow_d=min(pow_d1,pow_d2);
    if (pow_d == 0)
      pow_d = min(pow_d1, pow_d2);
    show(pow_d);
    if (pow_d <= sr[j] * sr[j])
      remove_list[i].push_back(j);
  }

  long double ans = 0;
  rep(i, 0, 1 << m) {
    long double sum = 0;
    rep(j, 0, m) if ((i >> j) & 1) sum +=
        tb[j] / pow_distance(tx[j], ty[j], tz[j], ex, ey, ez);
    if (sum <= ans)
      continue;
    // show(sum);
    // show(i);
    bool ok = true;
    set<int> removed;
    rep(j, 0, m) {
      if (!((i >> j) & 1))
        continue;
      // if(i==22) show(j);
      for (int id : remove_list[j])
        if (!removed.count(id)) {
          // show(id);
          removed.insert(id);
        }
      if (removed.size() > r) {
        // show(removed.size());
        // if(i==22) for(int id:removed) show(id);
        ok = false;
        break;
      }
    }
    if (!ok)
      continue;
    ans = sum;
  }
  cout << ans << endl;
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(10);
  while (true) {
    int n, m, r;
    cin >> n >> m >> r;
    if (!n and !m and !r)
      break;
    solve(n, m, r);
  }
  return 0;
}