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

#include <bits/stdc++.h>
#define f first
#define s second
#define mp make_pair
#define pi M_PI
#define inf 1 << 30
#define eps (1e-9)
#define equals(a, b) (fabs((a) - (b)) < eps)
using namespace std;

class Point {
public:
  double x, y;
  Point(double x = 0, double y = 0) : x(x), y(y) {}

  Point operator+(Point p) { return Point(x + p.x, y + p.y); }
  Point operator-(Point p) { return Point(x - p.x, y - p.y); }
  Point operator*(double k) { return Point(x * k, y * k); }
  Point operator/(double k) { return Point(x / k, y / k); }
  bool operator<(Point p) const { return (x != p.x ? x < p.x : y < p.y); }
  bool operator==(Point p) const {
    return fabs(x - p.x) < eps && fabs(y - p.y) < eps;
  }

  double abs() { return sqrt(norm()); }
  double norm() { return (x * x + y * y); }
};
typedef Point Vector;
typedef vector<Point> Polygon;

class Segment {
public:
  Point p1, p2;
  Segment(Point p1, Point p2) : p1(p1), p2(p2) {}
};
typedef Segment Line;

class Circle {
public:
  Point c;
  double r;
  Circle(Point c = Point(), double r = 0.0) : c(c), r(r) {}
};

double norm(Vector a) { return (a.x * a.x + a.y * a.y); }
double abs(Vector a) { return sqrt(norm(a)); }
double dot(Vector a, Vector b) { return (a.x * b.x + a.y * b.y); }
double cross(Vector a, Vector b) { return (a.x * b.y - a.y * b.x); }

Point project(Segment s, Point p) {
  Vector base = (s.p2 - s.p1);
  double r = (dot(p - s.p1, base) / base.norm());
  return (s.p1 + base * r);
}

Point reflect(Segment s, Point p) { return p + (project(s, p) - p) * 2; }

static const int COUNTER_CLOCKWISE = 1;
static const int CLOCKWISE = -1;
static const int ONLINE_BACK = 2;
static const int ONLINE_FRONT = -2;
static const int ON_SEGMENT = 0;

int ccw(Point p0, Point p1, Point p2) {
  Vector a = p1 - p0;
  Vector b = p2 - p0;
  if (cross(a, b) > eps)
    return COUNTER_CLOCKWISE;
  if (cross(a, b) < -eps)
    return CLOCKWISE;
  if (dot(a, b) < -eps)
    return ONLINE_BACK;
  if (a.norm() < b.norm())
    return ONLINE_FRONT;
  return ON_SEGMENT;
}

double getDistanceLP(Line l, Point p) {
  return abs(cross(l.p2 - l.p1, p - l.p1) / abs(l.p2 - l.p1));
}

Point getCrossPointLL(Line a, Line b) {
  double A = cross(a.p2 - a.p1, b.p2 - b.p1);
  double B = cross(a.p2 - a.p1, a.p2 - b.p1);
  if (abs(B) < eps || abs(A) < eps)
    return b.p1;
  return b.p1 + (b.p2 - b.p1) * (B / A);
}

Point rotate(Point base, Point a, double r) {
  Point b = a - base;
  a.x = b.x * cos((r / 180) * M_PI) - b.y * sin((r / 180) * M_PI);
  a.y = b.x * sin((r / 180) * M_PI) + b.y * cos((r / 180) * M_PI);
  a = a + base;
  return a;
}

int n, w, h, r;

int main() {
  int x, y, vx, vy;

  while (1) {
    cin >> n >> w >> h >> r >> vx >> vy;
    if (n == 0)
      break;
    cin >> x >> y;
    Line s(Point(x, y), Point(x + vx, y + vy));
    vector<Segment> vs;
    vs.push_back(Segment(Point(r, r), Point(r, h - r)));
    vs.push_back(Segment(Point(r, h - r), Point(w - r, h - r)));
    vs.push_back(Segment(Point(w - r, h - r), Point(w - r, r)));
    vs.push_back(Segment(Point(w - r, r), Point(r, r)));
    vector<Point> vp;
    for (int i = 1; i < n; i++) {
      cin >> x >> y;
      vp.push_back(Point(x, y));
    }

    double dis = 0;
    int ans = -1, c = 0;

    while (1) {
      if ((10000 - dis) < eps)
        break;

      double mi = inf;
      for (int i = 0; i < vp.size(); i++) {
        double d1 = getDistanceLP(s, vp[i]);
        Point A = project(s, vp[i]);
        if ((d1 - 2 * r) < eps && ccw(s.p1, s.p2, A) != 2) {
          double d = abs(A - s.p1) - (sqrt(4 * r * r - d1 * d1));
          if ((d + dis - 10000) < eps && (d - mi) < eps) {
            // printf("%.10f %d %d\\n",d1,i, ccw(s.p1,s.p2,A));
            mi = d;
            ans = i + 2;
          }
        }
      }
      if (ans != -1)
        break;

      for (int i = 0; i < vs.size(); i++) {
        Point a = getCrossPointLL(vs[i], s);
        if ((a == vs[0].p1 || a == vs[1].p1 || a == vs[2].p1 ||
             a == vs[3].p1) &&
            !(a == s.p1)) {
          dis += abs(s.p1 - a);
          s.p2 = s.p1;
          s.p1 = a;
          break;
        }
        if (ccw(s.p1, s.p2, a) != 2 && ccw(vs[i].p1, vs[i].p2, a) == 0 &&
            !(s.p1 == a)) {
          Segment tmp(rotate(a, vs[i].p1, 90), rotate(a, vs[i].p2, 90));
          dis += abs(s.p1 - a);
          s.p2 = reflect(tmp, s.p1);
          s.p1 = a;
          break;
        }
      }
    }

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define f first
#define s second
#define mp make_pair
#define pi M_PI
#define inf 1 << 30
#define eps (1e-11)
#define equals(a, b) (fabs((a) - (b)) < eps)
using namespace std;

class Point {
public:
  double x, y;
  Point(double x = 0, double y = 0) : x(x), y(y) {}

  Point operator+(Point p) { return Point(x + p.x, y + p.y); }
  Point operator-(Point p) { return Point(x - p.x, y - p.y); }
  Point operator*(double k) { return Point(x * k, y * k); }
  Point operator/(double k) { return Point(x / k, y / k); }
  bool operator<(Point p) const { return (x != p.x ? x < p.x : y < p.y); }
  bool operator==(Point p) const {
    return fabs(x - p.x) < eps && fabs(y - p.y) < eps;
  }

  double abs() { return sqrt(norm()); }
  double norm() { return (x * x + y * y); }
};
typedef Point Vector;
typedef vector<Point> Polygon;

class Segment {
public:
  Point p1, p2;
  Segment(Point p1, Point p2) : p1(p1), p2(p2) {}
};
typedef Segment Line;

class Circle {
public:
  Point c;
  double r;
  Circle(Point c = Point(), double r = 0.0) : c(c), r(r) {}
};

double norm(Vector a) { return (a.x * a.x + a.y * a.y); }
double abs(Vector a) { return sqrt(norm(a)); }
double dot(Vector a, Vector b) { return (a.x * b.x + a.y * b.y); }
double cross(Vector a, Vector b) { return (a.x * b.y - a.y * b.x); }

Point project(Segment s, Point p) {
  Vector base = (s.p2 - s.p1);
  double r = (dot(p - s.p1, base) / base.norm());
  return (s.p1 + base * r);
}

Point reflect(Segment s, Point p) { return p + (project(s, p) - p) * 2; }

static const int COUNTER_CLOCKWISE = 1;
static const int CLOCKWISE = -1;
static const int ONLINE_BACK = 2;
static const int ONLINE_FRONT = -2;
static const int ON_SEGMENT = 0;

int ccw(Point p0, Point p1, Point p2) {
  Vector a = p1 - p0;
  Vector b = p2 - p0;
  if (cross(a, b) > eps)
    return COUNTER_CLOCKWISE;
  if (cross(a, b) < -eps)
    return CLOCKWISE;
  if (dot(a, b) < -eps)
    return ONLINE_BACK;
  if (a.norm() < b.norm())
    return ONLINE_FRONT;
  return ON_SEGMENT;
}

double getDistanceLP(Line l, Point p) {
  return abs(cross(l.p2 - l.p1, p - l.p1) / abs(l.p2 - l.p1));
}

Point getCrossPointLL(Line a, Line b) {
  double A = cross(a.p2 - a.p1, b.p2 - b.p1);
  double B = cross(a.p2 - a.p1, a.p2 - b.p1);
  if (abs(A) < eps || abs(B) < eps)
    return b.p1;
  return b.p1 + (b.p2 - b.p1) * (B / A);
}

Point rotate(Point base, Point a, double r) {
  Point b = a - base;
  a.x = b.x * cos((r / 180) * M_PI) - b.y * sin((r / 180) * M_PI);
  a.y = b.x * sin((r / 180) * M_PI) + b.y * cos((r / 180) * M_PI);
  a = a + base;
  return a;
}

int n, w, h, r;

int main() {
  int x, y, vx, vy;

  while (1) {
    cin >> n >> w >> h >> r >> vx >> vy;
    if (n == 0)
      break;
    cin >> x >> y;
    Line s(Point(x, y), Point(x + vx, y + vy));
    vector<Segment> vs;
    vs.push_back(Segment(Point(r, r), Point(r, h - r)));
    vs.push_back(Segment(Point(r, h - r), Point(w - r, h - r)));
    vs.push_back(Segment(Point(w - r, h - r), Point(w - r, r)));
    vs.push_back(Segment(Point(w - r, r), Point(r, r)));
    vector<Point> vp;
    for (int i = 1; i < n; i++) {
      cin >> x >> y;
      vp.push_back(Point(x, y));
    }

    double dis = 0;
    int ans = -1, c = 0;

    while (1) {
      /*      c++;
      if(c>=10)break;
      printf("%.10f %.10f ",s.p1.x,s.p1.y);
      printf("%.10f %.10f\\n",s.p2.x,s.p2.y);
      cout<<dis<<endl;*/
      if ((dis - 100000) > eps)
        break;
      // first check all circles that tatched
      for (int i = 0; i < vp.size(); i++) {
        double d1 = getDistanceLP(s, vp[i]), d2 = abs(vp[i] - s.p1), min = inf;
        if ((d1 - 2 * r) < eps) {
          double d = sqrt(d2 * d2 - d1 * d1) - (sqrt(4 * r * r - d1 * d1) + r);
          if (d + dis <= 100000 && d < min) {
            min = d;
            ans = i + 2;
          }
        }
      }
      if (ans != -1)
        break;
      // get next line
      for (int i = 0; i < vs.size(); i++) {
        Point a = getCrossPointLL(vs[i], s);
        // printf("a=(%.10f,%.10f)\\n",a.x,a.y);
        if (a == vs[0].p1 || a == vs[1].p1 || a == vs[2].p1 || a == vs[3].p2) {
          swap(s.p1, s.p2);
          break;
        }
        if (ccw(s.p1, s.p2, a) != 2 && ccw(vs[i].p1, vs[i].p2, a) == 0 &&
            !(s.p1 == a)) {
          Segment tmp(rotate(a, vs[i].p1, 90), rotate(a, vs[i].p2, 90));
          dis += abs(s.p1 - a);
          s.p2 = reflect(tmp, s.p1);
          s.p1 = a;
          break;
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}