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

#include <algorithm>
#include <iostream>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
#define maxn 1000010
#define oo 1000000000
#define eps (1e-8)
#define clearAll(a) memset(a, 0, sizeof(a))
#define sq(a) ((a) * (a))

using namespace std;

typedef long long ll;

const double pi = acos(-1);

int sgn(double x) { return x < -eps ? -1 : (x > eps); }

struct Vect {
  double x, y;
  Vect() { x = y = 0.0; }
  Vect(double xx, double yy) {
    x = xx;
    y = yy;
  }

  Vect operator+(const Vect &v) const {
    Vect res(x + v.x, y + v.y);
    return res;
  }

  Vect operator-(const Vect &v) const {
    Vect res(x - v.x, y - v.y);
    return res;
  }

  Vect operator*(double l) {
    Vect res(x * l, y * l);
    return res;
  }

  Vect operator/(double l) {
    Vect res(x / l, y / l);
    return res;
  }

  double operator*(const Vect &v) const { return v.x * x + v.y * y; }

  double operator&(const Vect &v) const { return x * v.y - y * v.x; }

  double length() { return sqrt(x * x + y * y); }

  Vect Rotate(double B) {
    double tx, ty;
    tx = x * cos(B) - y * sin(B);
    ty = x * sin(B) + y * cos(B);
    return Vect(tx, ty);
  }

  Vect verti() { return Vect(-y, x); }

  Vect adjust(double len) {
    double l = len / length();
    return Vect(x * l, y * l);
  }
};

struct Point {
  double x, y;
  Point() { x = y = 0.0; }
  Point(double xx, double yy) {
    x = xx;
    y = yy;
  }

  void setPoint(double xx, double yy) {
    x = xx;
    y = yy;
  }

  Vect operator-(const Point &a) const {
    Vect res(x - a.x, y - a.y);
    return res;
  }

  Point operator+(const Vect &v) const {
    Point res(x + v.x, y + v.y);
    return res;
  }

  Point operator-(const Vect &v) const {
    Point res(x - v.x, y - v.y);
    return res;
  }

  bool operator==(const Point a) const {
    return fabs(x - a.x) < eps && fabs(y - a.y) < eps;
  }

  Point Rotate(double B, Point O) {
    Vect v = O - Point(0, 0);
    Vect tmp(x - v.x, y - v.y);
    tmp = tmp.Rotate(B) + v;
    return Point(tmp.x, tmp.y);
  }
};

Point pl, pr;
double t;
int n, D;

inline double getRad(double s) { return pi * (s / 180); }

void condEqual(double sl, double sr) {
  Vect v = (pr - pl).verti();

  double len = pi * (sl / 180) * t;
  pl = pl + v.adjust(len);
  pr = pr + v.adjust(len);
}

void condDiff(double sl, double sr) {
  double dl = 2 * D * fabs(sl) / (fabs(sl) + fabs(sr));
  double dr = 2 * D * fabs(sr) / (fabs(sl) + fabs(sr));

  Point O = pl + (pr - pl).adjust(dl);

  double Bl = getRad(sl) / dl * t;
  double Br = getRad(sr) / dr * t;

  if (sgn(sl) != 0)
    pl = pl.Rotate(-Bl, O);
  if (sgn(sr) != 0)
    pr = pr.Rotate(Br, O);
}

void condLR(double sl, double sr) {
  double dl = 2 * D * fabs(sl) / (fabs(sl) - fabs(sr));
  double dr = 2 * D * fabs(sr) / (fabs(sl) - fabs(sr));

  Point O = pl + (pr - pl).adjust(dl);

  double Bl = getRad(sl) / dl * t;
  double Br = getRad(sr) / dr * t;

  if (sgn(sl) != 0)
    pl = pl.Rotate(-Bl, O);
  if (sgn(sr) != 0)
    pr = pr.Rotate(-Br, O);
}

void condRL(double sl, double sr) {
  double dl = 2 * D * fabs(sl) / (-fabs(sl) + fabs(sr));
  double dr = 2 * D * fabs(sr) / (-fabs(sl) + fabs(sr));

  Point O = pl - (pr - pl).adjust(dl);

  double Bl = getRad(sl) / dl * t;
  double Br = getRad(sr) / dr * t;

  if (sgn(sl) != 0)
    pl = pl.Rotate(Bl, O);
  if (sgn(sr) != 0)
    pr = pr.Rotate(Br, O);
}

int main() {
  // freopen("C:\\\\Users\\\\py\\\\Desktop\\\\input.txt","r",stdin);
  // freopen("C:\\\\Users\\\\py\\\\Desktop\\\\output.txt","w",stdout);

  while (scanf("%d%d", &n, &D) && (n || D)) {
    pl.setPoint(-D, 0);
    pr.setPoint(D, 0);

    double spl, spr;

    for (int i = 1; i <= n; i++) {
      scanf("%lf%lf%lf", &spl, &spr, &t);

      if (sgn(spl - spr) == 0)
        condEqual(spl, spr);
      else {

        if (sgn(spl) != sgn(spr))
          condDiff(spl, spr);
        else if (fabs(spl) > fabs(spr))
          condLR(spl, spr);
        else
          condRL(spl, spr);
      }
    }

    printf("%.5f\\n%.5f\\n", (pl.x + pr.x) / 2, (pl.y + pr.y) / 2);
  }

  return 0;
}

Error: TLE

Fixed submission:

/*
 * main.cpp
 *
 *  Created on: 2013-7-28
 *      Author: whd
 */
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long big;
double l, r, ang, d;
int n;
const double pi = acos(-1.0);
double x, y, t;
const double delta = 0.00005;
int sgn(int x) {
  if (fabs(x) < 1e-9)
    return 0;
  return x > 0 ? 1 : -1;
}
struct P {
  double x, y;
  void output() { printf("%f %f#\\n", x, y); }
  P() {}
  P(double _x, double _y) : x(_x), y(_y) {}
};
P L, R;
int main() {
  while (scanf("%d%lf", &n, &d), n || (d > 1e-8)) {
    x = 0;
    y = 0;
    ang = pi / 2.0;
    L = P(-d, 0);
    R = P(d, 0);
    while (n--) {
      scanf("%lf%lf%lf", &l, &r, &t);
      l = (l / 360.0) * 2 * pi;
      r = (r / 360.0) * 2 * pi;
      while (t > 0) {
        ang = atan2(R.y - L.y, R.x - L.x);
        ang += pi / 2.0;
        L.x += cos(ang) * l * delta;
        L.y += sin(ang) * l * delta;
        R.x += cos(ang) * r * delta;
        R.y += sin(ang) * r * delta;
        t -= delta;
      }
    }
    printf("%.5f\\n%.5f\\n", (L.x + R.x) / 2.0, (L.y + R.y) / 2.0);
  }
}