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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#define FI first
#define SE second
using namespace std;
const double EPS = 1e-8;
const int MAXN = 10005;
const int INF = 1111111111;
const double PI = acos(-1.0);
int dcmp(double x) {
  if (fabs(x) < EPS)
    return 0;
  return x < 0 ? -1 : 1;
}
struct Point {
  double x, y;
  Point() {}
  Point(double a, double b) : x(a), y(b) {}
  Point operator+(const Point &a) const { return Point(x + a.x, y + a.y); }
  Point operator-(const Point &a) const { return Point(x - a.x, y - a.y); }
  Point operator*(double k) const { return Point(x * k, y * k); }
  Point operator/(double k) const { return Point(x / k, y / k); }
  void input() { scanf("%lf%lf", &x, &y); }
};
typedef Point Vector;
double length(Vector a) { return sqrt(a.x * a.x + a.y * a.y); }
Vector normal(Vector a) {
  double l = length(a);
  return Vector(-a.y / l, a.x / l);
}

Vector rotate(Vector a, double rad) {
  Vector c;
  c.x = a.x * cos(rad) - a.y * sin(rad);
  c.y = a.x * sin(rad) + a.y * cos(rad);
  return c;
}

int main() {
  // freopen("/home/qitaishui/code/in.txt","r",stdin);
  Point o, left, right, vec;
  double d, v1, v2, r1, r2, t;
  int n;
  while (scanf("%d%lf", &n, &d) && n) {
    left = Point(-d, 0);
    right = Point(d, 0);
    d *= 2;
    for (int i = 0; i < n; i++) {
      scanf("%lf%lf%lf", &v1, &v2, &t);
      v1 = v1 / 180 * PI;
      v2 = v2 / 180 * PI;
      if (dcmp(v1 - v2) == 0) {
        vec = normal(right - left);
        left = left + vec * v1 * t;
        right = right + vec * v1 * t;
      } else if (dcmp(v1 * v2) > 0) {
        vec = (right - left) / length(right - left);

        if (fabs(v1) > fabs(v2)) {
          r2 = d / (v1 / v2 - 1);
          o = right + vec * r2;
          left = o + rotate(left - o, -v1 * t / length(left - o));
          right = o + rotate(right - o, -v2 * t / length(right - o));
        } else {
          r1 = d / (v2 / v1 - 1);
          o = left - vec * r1;
          left = o + rotate(left - o, v1 * t / length(left - o));
          right = o + rotate(right - o, v2 * t / length(right - o));
        }
      } else {
        o = left + (right - left) * fabs(v1) / (fabs(v1) + fabs(v2));
        if (dcmp(length(left - o)) > 0)
          left = o + rotate(left - o, -v1 * t / length(left - o));
        if (dcmp(length(right - o)) > 0)
          right = o + rotate(right - o, v2 * t / length(right - o));
      }
    }
    o = (left + right) / 2;
    printf("%.5f\\n%.5f\\n", o.x + EPS, o.y + EPS);
  }
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>

using namespace std;

typedef long double DB;
const DB EPS = 1e-5;
const DB PI = acos(-1.0);
inline int sgn(DB x) { return (x > EPS) - (x < -EPS); }
typedef pair<DB, DB> Point;
#define x first
#define y second
Point operator+(const Point &a, const Point &b) {
  return Point(a.x + b.x, a.y + b.y);
}
Point operator-(const Point &a, const Point &b) {
  return Point(a.x - b.x, a.y - b.y);
}
Point operator*(const Point &a, const DB &p) { return Point(a.x * p, a.y * p); }
Point operator/(const Point &a, const DB &p) { return Point(a.x / p, a.y / p); }

inline DB dot(const Point &a, const Point &b) { return a.x * b.x + a.y * b.y; }
inline DB veclen(const Point &a) { return sqrt(dot(a, a)); }
inline DB angle(const Point &a) { return atan2(a.y, a.x); }
inline Point normal(const Point &a) { return Point(-a.y, a.x) / veclen(a); }

void get_nx(Point &l, Point &r, DB v1, DB v2, DB t) {
  Point O;
  if (sgn(v1 - v2) == 0) {
    Point dir = normal(r - l);
    l = l + dir * v1 * t;
    r = r + dir * v2 * t;
    return;
  }
  if (sgn(v1) * sgn(v2) < 0) {
    DB d = fabs(v1) / (fabs(v1) + fabs(v2));
    // cout << "opp" << ' ' << d << endl;
    O = l + (r - l) * d;
    DB c = angle(l - O);
    c -= v1 * t / veclen(l - O);
    l = O + Point(cos(c), sin(c)) * veclen(l - O);
    c += PI;
    r = O + Point(cos(c), sin(c)) * veclen(r - O);
  } else {
    O = r + (r - l) * v2 / (v1 - v2);
    // cout << O.x << ' ' << O.y << endl;
    DB c = angle(r - O);
    assert(sgn(c - angle(l - O)) == 0);
    if (fabs(v1) > fabs(v2))
      c -= v2 * t / veclen(r - O);
    else
      c += v1 * t / veclen(l - O);
    l = O + Point(cos(c), sin(c)) * veclen(l - O);
    r = O + Point(cos(c), sin(c)) * veclen(r - O);
  }
}

DB fix(DB x) {
  if (sgn(x) == 0)
    return 0.0;
  return x;
}

int main() {
  // freopen("in", "r", stdin);
  cout << setiosflags(ios::fixed) << setprecision(6);
  int n, m;
  while (cin >> n >> m && (n || m)) {
    Point L(-m, 0), R(m, 0);
    DB v1, v2, t;
    for (int i = 0; i < n; i++) {
      cin >> v1 >> v2 >> t;
      v1 = v1 / 180 * PI;
      v2 = v2 / 180 * PI;
      get_nx(L, R, v1, v2, t);
      // cout << L.x << ' ' << L.y << ' ' << R.x << ' ' << R.y << endl;
    }
    Point M = (L + R) / 2;
    cout << (double)fix(M.x) << endl << (double)fix(M.y) << endl;
  }
  return 0;
}