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

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
using Point = complex<ld>;
const ld eps = 1e-9;

vector<ld> quad_equa(ld a, ld b, ld c) {
  ld root = b * b - 4 * a * c;
  if (abs(root) < eps) {
    ld ans = -b / (2 * a);
    return vector<ld>(2, ans);
  } else if (root > 0) {
    ld ans = -b / (2 * a);
    vector<ld> anss(2);
    anss[0] = ans + sqrt(root) / (2 * a);
    anss[1] = ans - sqrt(root) / (2 * a);
    if (anss[0] > anss[1])
      swap(anss[0], anss[1]);
    return anss;
  } else {
    return vector<ld>();
  }
}

pair<ld, Point> getway(Point dp, const ld dv, Point fp, Point fv) {
  fp -= dp;
  auto ts = quad_equa(norm(fv) - dv * dv,
                      2 * fv.real() * fp.real() + 2 * fv.imag() * fp.imag(),
                      norm(fp));
  if (ts.empty()) {
    return make_pair(1e18, Point(0.l, 0.l));
  } else {
    ld t;
    if (ts[0] < -eps) {
      if (ts.size() > 1) {

        t = ts[1];
      } else {
        t = ts[0];
      }
    } else
      t = ts[0];
    if (t < -eps)
      return make_pair(1e18, Point(0.l, 0.l));
    Point catp = fp + fv * t;
    return make_pair(t, catp / ld(t));
  }
}

int main() {
  while (1) {
    int N;
    cin >> N;
    int M;
    cin >> M;
    if (!N)
      break;
    vector<Point> dps;
    vector<ld> dvs;
    vector<int> nums(N);
    for (int i = 0; i < N; ++i) {
      ld dx, dy, v;
      cin >> dx >> dy >> v;
      dps.push_back(Point(dx, dy));
      dvs.push_back(v);
    }
    for (int i = 0; i < M; ++i) {
      vector<pair<ld, Point>> ways(N);
      ld fpx, fpy, fvx, fvy;
      cin >> fpx >> fpy >> fvx >> fvy;
      Point fp(fpx, fpy);
      Point fv(fvx, fvy);
      ld mint = 1e19;
      int minid = -1;
      for (int j = 0; j < N; ++j) {
        ways[j] = getway(dps[j], dvs[j], fp, fv);
        if (mint > ways[j].first) {
          mint = ways[j].first;
          minid = j;
        }
      }
      nums[minid]++;
      for (int j = 0; j < N; ++j) {
        dps[j] += ways[j].second * mint;
      }
    }
    for (int i = 0; i < N; ++i) {
      cout << nums[i];
      if (i == N - 1)
        cout << endl;
      else
        cout << " ";
    }
  }

  return 0;
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
using Point = complex<ld>;
const ld eps = 1e-9;

vector<ld> quad_equa(ld a, ld b, ld c) {
  ld root = b * b - 4 * a * c;
  if (abs(root) < eps) {
    ld ans = -b / (2 * a);
    return vector<ld>(2, ans);
  } else if (root > 0) {
    ld ans = -b / (2 * a);
    vector<ld> anss(2);
    anss[0] = ans + sqrt(root) / (2 * a);
    anss[1] = ans - sqrt(root) / (2 * a);
    return anss;
  } else {
    return vector<ld>();
  }
}

pair<ld, Point> getway(Point dp, const ld dv, Point fp, Point fv) {
  fp -= dp;
  auto ts = quad_equa(norm(fv) - dv * dv,
                      2 * fv.real() * fp.real() + 2 * fv.imag() * fp.imag(),
                      norm(fp));
  if (ts.empty()) {
    return make_pair(1e18, Point(0.l, 0.l));
  } else {
    ld t;
    if (ts[0] < eps) {
      if (ts.size() > 1) {

        t = ts[1];
      } else {
        t = ts[0];
      }
    } else
      t = ts[0];
    Point catp = fp + fv * t;
    return make_pair(t, catp / ld(t));
  }
}

int main() {
  while (1) {
    int N;
    cin >> N;
    int M;
    cin >> M;
    if (!N)
      break;
    vector<Point> dps;
    vector<ld> dvs;
    vector<int> nums(N);
    for (int i = 0; i < N; ++i) {
      ld dx, dy, v;
      cin >> dx >> dy >> v;
      dps.push_back(Point(dx, dy));
      dvs.push_back(v);
    }
    for (int i = 0; i < M; ++i) {
      vector<pair<ld, Point>> ways(N);
      ld fpx, fpy, fvx, fvy;
      cin >> fpx >> fpy >> fvx >> fvy;
      Point fp(fpx, fpy);
      Point fv(fvx, fvy);
      ld mint = 1e19;
      int minid = -1;
      for (int j = 0; j < N; ++j) {
        ways[j] = getway(dps[j], dvs[j], fp, fv);
        if (mint > ways[j].first) {
          mint = ways[j].first;
          minid = j;
        }
      }
      nums[minid]++;
      for (int j = 0; j < N; ++j) {
        dps[j] += ways[j].second * mint;
      }
    }
    for (int i = 0; i < N; ++i) {
      cout << nums[i];
      if (i == N - 1)
        cout << endl;
      else
        cout << " ";
    }
  }

  return 0;
}