Problem p01364 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <complex>
#include <fstream>
#include <iomanip>
#include <iostream>

using namespace std;

typedef long double elem;
typedef complex<elem> point, vec;

const elem pi = 2.0 * acos(0.0);
const elem eps = 1e-9;
bool eq(elem a, elem b) { return abs(b - a) < eps; }
vec uvec(vec a) { return a / abs(a); }
vec nmr(vec a) { return a * vec(0, -1); }
vec nml(vec a) { return a * vec(0, 1); }
vec unmr(vec a) { return uvec(nmr(a)); }
vec unml(vec a) { return uvec(nml(a)); }

bool near(const point &a, const point &b) { return abs(b - a) < eps; }

struct buggy {
  point center;
  point left;
  point right;
  buggy() {}
  buggy(point c, point l, point r) : center(c), left(l), right(r) {}
};

point Rot(point p, elem theta) { return p * polar((elem)1.0, theta); }
buggy rot_buggy(const buggy &b, point &center, double rot) {
  buggy ret;
  ret.left = Rot(b.left - center, rot) + center;
  ret.right = Rot(b.right - center, rot) + center;
  ret.center = Rot(b.center - center, rot) + center;
  return ret;
}

buggy move(const buggy &b, vec direction, elem d) {
  buggy ret;
  ret.left = b.left + direction * d;
  ret.right = b.right + direction * d;
  ret.center = b.center + direction * d;
  return ret;
}

int main() {
  while (true) {
    int N;
    elem D;
    cin >> N >> D;
    if (N == 0 && eq(D, 0))
      break;
    buggy b(point(0, 0), point(-D, 0), point(D, 0));
    for (int i = 0; i < N; ++i) {
      elem Lsp, Rsp, time;
      cin >> Lsp >> Rsp >> time;
      if (eq(Lsp, Rsp)) {
        vec v = unml(b.right - b.left);
        elem progress = time * pi * (Lsp / 180.0);
        b = move(b, v, progress);
        continue;
      } else {
        if (eq(Rsp, 0)) {
          b = rot_buggy(b, b.right, -pi * ((Lsp * time) / 180.0) / (2 * D));
          continue;
        } else {
          elem x = 2 * D * Rsp / (Lsp - Rsp);
          vec v = b.right - b.left;
          point rot_center = x * (v / abs(v)) + b.right;
          double rot = -pi * (((Rsp * time) / 180.0) / x);
          b = rot_buggy(b, rot_center, rot);
        }
      }
    }
    cout.precision(8);
    cout << fixed << b.center.real() << endl;
    cout << fixed << b.center.imag() << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <complex>
#include <fstream>
#include <iomanip>
#include <iostream>

using namespace std;

typedef long double elem;
typedef complex<elem> point, vec;

const elem pi = 2.0 * acos(0.0);
const elem eps = 1e-9;
bool eq(elem a, elem b) { return abs(b - a) < eps; }
vec uvec(vec a) { return a / abs(a); }
vec nmr(vec a) { return a * vec(0, -1); }
vec nml(vec a) { return a * vec(0, 1); }
vec unmr(vec a) { return uvec(nmr(a)); }
vec unml(vec a) { return uvec(nml(a)); }

bool near(const point &a, const point &b) { return abs(b - a) < eps; }

struct buggy {
  point center;
  point left;
  point right;
  buggy() {}
  buggy(point c, point l, point r) : center(c), left(l), right(r) {}
};

point Rot(point p, elem theta) { return p * polar((elem)1.0, theta); }
buggy rot_buggy(const buggy &b, point &center, double rot) {
  buggy ret;
  ret.left = Rot(b.left - center, rot) + center;
  ret.right = Rot(b.right - center, rot) + center;
  ret.center = Rot(b.center - center, rot) + center;
  return ret;
}

buggy move(const buggy &b, vec direction, elem d) {
  buggy ret;
  ret.left = b.left + direction * d;
  ret.right = b.right + direction * d;
  ret.center = b.center + direction * d;
  return ret;
}

int main() {
  ifstream ifs("F.txt");
  while (true) {
    int N;
    elem D;
    ifs >> N >> D;
    if (N == 0 && eq(D, 0))
      break;
    buggy b(point(0, 0), point(-D, 0), point(D, 0));
    for (int i = 0; i < N; ++i) {
      elem Lsp, Rsp, time;
      ifs >> Lsp >> Rsp >> time;
      if (eq(Lsp, Rsp)) {
        vec v = unml(b.right - b.left);
        elem progress = time * pi * (Lsp / 180.0);
        b = move(b, v, progress);
        continue;
      } else {
        if (eq(Rsp, 0)) {
          b = rot_buggy(b, b.right, -pi * ((Lsp * time) / 180.0) / (2 * D));
          continue;
        } else {
          elem x = 2 * D * Rsp / (Lsp - Rsp);
          vec v = b.right - b.left;
          point rot_center = x * (v / abs(v)) + b.right;
          double rot = -pi * (((Rsp * time) / 180.0) / x);
          b = rot_buggy(b, rot_center, rot);
        }
      }
    }
    cout.precision(8);
    cout << fixed << b.center.real() << endl;
    cout << fixed << b.center.imag() << endl;
  }
  return 0;
}