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

#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;

const double EPS = 1e-9;
const int LIM = 50;

bool eq(double a, double b) { return abs(a - b) < EPS; }

long long square(long long x) { return x * x; }

long long dist(int x1, int y1, int z1, int x2, int y2, int z2) {
  return square(x1 - x2) + square(y1 - y2) + square(z1 - z2);
}

int inner_product(int x1, int y1, int z1, int x2, int y2, int z2) {
  return x1 * x2 + y1 * y2 + z1 * z2;
}

int main() {
  int N, Q;
  long long x[LIM], y[LIM], z[LIM], r[LIM];
  long long l[LIM];

  cin >> N >> Q;

  for (int j = 0; j < N; ++j) {
    cin >> x[j] >> y[j] >> z[j] >> r[j] >> l[j];
  }

  for (int _ = 0; _ < Q; ++_) {
    long long sx, sy, sz, dx, dy, dz;
    cin >> sx >> sy >> sz >> dx >> dy >> dz;

    long long ans = 0;

    for (int k = 0; k < N; ++k) {
      if (dist(sx, sy, sz, x[k], y[k], z[k]) <= square(r[k]) ||
          dist(dx, dy, dz, x[k], y[k], z[k]) <= square(r[k])) {
        ans += l[k];
        continue;
      }
      long long plane1 = (sx - dx) * (x[k] - sx) + (sy - dy) * (y[k] - sy) +
                         (sz - dz) * (z[k] - sz);
      long long plane2 = (sx - dx) * (x[k] - dx) + (sy - dy) * (y[k] - dy) +
                         (sz - dz) * (z[k] - dz);

      if (plane1 * plane2 > 0) {
        continue;
      }

      long long ip = inner_product(x[k] - sx, y[k] - sy, z[k] - sz, dx - sx,
                                   dy - sy, dz - sz);

      double dist_f = dist(x[k], y[k], z[k], sx, sy, sz) -
                      ((double)square(ip)) / dist(sx, sy, sz, dx, dy, dz);

      if (dist_f <= square(r[k])) {
        ans += l[k];
      }
    }

    cout << ans << endl;
  }
  return 0;
}

Error: 0
 -1800000 0 25
 -2880000 0 25
 -3240000 0 25
 -2880000 0 25
 -1800000 0 25


Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;

const double EPS = 1e-9;
const int LIM = 50;

bool eq(double a, double b) { return abs(a - b) < EPS; }

long long square(long long x) { return x * x; }

long long dist(int x1, int y1, int z1, int x2, int y2, int z2) {
  return square(x1 - x2) + square(y1 - y2) + square(z1 - z2);
}

int inner_product(int x1, int y1, int z1, int x2, int y2, int z2) {
  return x1 * x2 + y1 * y2 + z1 * z2;
}

int main() {
  int N, Q;
  long long x[LIM], y[LIM], z[LIM], r[LIM];
  long long l[LIM];

  cin >> N >> Q;

  for (int j = 0; j < N; ++j) {
    cin >> x[j] >> y[j] >> z[j] >> r[j] >> l[j];
  }

  for (int _ = 0; _ < Q; ++_) {
    long long sx, sy, sz, dx, dy, dz;
    cin >> sx >> sy >> sz >> dx >> dy >> dz;

    long long ans = 0;

    for (int k = 0; k < N; ++k) {
      if (dist(sx, sy, sz, x[k], y[k], z[k]) <= square(r[k]) ||
          dist(dx, dy, dz, x[k], y[k], z[k]) <= square(r[k])) {
        cerr << " a" << endl;
        ans += l[k];
        continue;
      }
      long long plane1 = (sx - dx) * (x[k] - sx) + (sy - dy) * (y[k] - sy) +
                         (sz - dz) * (z[k] - sz);
      long long plane2 = (sx - dx) * (x[k] - dx) + (sy - dy) * (y[k] - dy) +
                         (sz - dz) * (z[k] - dz);

      cerr << " " << (plane1 * plane2);

      if (plane1 * plane2 > 0) {
        cerr << endl;
        continue;
      }

      long long ip = inner_product(x[k] - sx, y[k] - sy, z[k] - sz, dx - sx,
                                   dy - sy, dz - sz);

      double dist_f = dist(x[k], y[k], z[k], sx, sy, sz) -
                      ((double)square(ip)) / dist(sx, sy, sz, dx, dy, dz);

      cerr << " " << dist_f << " " << square(r[k]) << endl;

      if (dist_f <= square(r[k])) {
        ans += l[k];
      }
    }

    cout << ans << endl;
  }
  return 0;
}