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

/**
 * Copyright (c) 2014 Authors. All rights reserved.
 *
 * FileName: A.cpp
 * Author: Beiyu Li <sysulby@gmail.com>
 * Date: 2014-09-15
 */
#include <bits/stdc++.h>

using namespace std;

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, s, t) for (int i = (s); i <= (t); ++i)
#define FOREACH(i, c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)

typedef long long LL;
typedef pair<int, int> Pii;

const int inf = 0x3f3f3f3f;
const LL infLL = 0x3f3f3f3f3f3f3f3fLL;

const double g = 9.8;
const double pi = acos(-1);
const double eps = 1e-6;
const int maxn = 50 + 5;

int n, v, x, y;
int l[maxn], b[maxn], r[maxn], t[maxn];

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

inline bool inside(double x, double l, double r) {
  return sgn(l - x) <= 0 && sgn(x - r) <= 0;
}

double A, B;
inline double f(double x) { return A * x * x + B * x; }

bool hit(double h) {
  if (sgn(h - y) < 0)
    return false;
  REP(i, n) if (inside(x, l[i], r[i])) {
    if (inside(b[i], y, h))
      return false;
    if (inside(t[i], y, h))
      return false;
  }
  return true;
}

bool hit(double h, int l, int r) {
  double d = B * B + 4 * A * h;
  if (sgn(d) < 0)
    return false;
  d = sqrt(d);
  double p = (-B - d) / 2 / A;
  if (inside(p, l, r) && sgn(p - x) <= 0)
    return true;
  p = (-B + d) / 2 / A;
  if (inside(p, l, r) && sgn(p - x) <= 0)
    return true;
  return false;
}

bool check(double a) {
  double T = v * sin(a) / g;
  double D = v * cos(a) * T, H = 0.5 * g * T * T;
  A = -H / D / D;
  B = 2 * H / D;
  double h = f(x);
  if (!hit(h))
    return false;
  REP(i, n) {
    if (inside(f(l[i]), b[i], t[i]) && sgn(l[i] - x) <= 0)
      return false;
    if (inside(f(r[i]), b[i], t[i]) && sgn(r[i] - x) <= 0)
      return false;
    if (hit(b[i], l[i], r[i]))
      return false;
    if (hit(t[i], l[i], r[i]))
      return false;
  }
  return true;
}

const double deta = 1e-6;
int main() {
  while (scanf("%d%d%d%d", &n, &v, &x, &y) != EOF) {
    REP(i, n) scanf("%d%d%d%d", &l[i], &b[i], &r[i], &t[i]);
    bool found = false;
    for (double a = deta; a < pi / 2; a += deta) {
      if (check(a)) {
        found = true;
        break;
      }
    }
    puts(found ? "Yes" : "No");
  }

  return 0;
}

Error: TLE

Fixed submission:

/**
 * Copyright (c) 2014 Authors. All rights reserved.
 *
 * FileName: A.cpp
 * Author: Beiyu Li <sysulby@gmail.com>
 * Date: 2014-09-15
 */
#include <bits/stdc++.h>

using namespace std;

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, s, t) for (int i = (s); i <= (t); ++i)
#define FOREACH(i, c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)

typedef long long LL;
typedef pair<int, int> Pii;

const int inf = 0x3f3f3f3f;
const LL infLL = 0x3f3f3f3f3f3f3f3fLL;

const double g = 9.8;
const double pi = acos(-1);
const double eps = 1e-6;
const int maxn = 50 + 5;

int n, v, x, y;
int l[maxn], b[maxn], r[maxn], t[maxn];

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

inline bool inside(double x, double l, double r) {
  return sgn(l - x) <= 0 && sgn(x - r) <= 0;
}

double A, B;
inline double f(double x) { return A * x * x + B * x; }

bool hit(double h, int l, int r) {
  double d = B * B + 4 * A * h;
  if (sgn(h) < 0)
    return false;
  d = sqrt(d);
  double p = (-B - d) / 2 / A;
  if (inside(p, l, r) && sgn(p - x) <= 0)
    return true;
  p = (-B + d) / 2 / A;
  if (inside(p, l, r) && sgn(p - x) <= 0)
    return true;
  return false;
}

bool check(double a) {
  double T = v * sin(a) / g;
  double D = v * cos(a) * T, H = 0.5 * g * T * T;
  A = -H / D / D;
  B = 2 * H / D;
  if (sgn(f(x) - y) < 0)
    return false;
  REP(i, n) {
    if (inside(f(l[i]), b[i], t[i]) && sgn(l[i] - x) <= 0)
      return false;
    if (inside(f(r[i]), b[i], t[i]) && sgn(r[i] - x) <= 0)
      return false;
    if (hit(b[i], l[i], r[i]))
      return false;
    if (hit(t[i], l[i], r[i]))
      return false;
  }
  return true;
}

const double deta = 1e-3;
int main() {
  while (scanf("%d%d%d%d", &n, &v, &x, &y)) {
    REP(i, n) scanf("%d%d%d%d", &l[i], &b[i], &r[i], &t[i]);
    bool found = false;
    for (double a = deta; a < pi / 2; a += deta) {
      if (check(a)) {
        found = true;
        break;
      }
    }
    puts(found ? "Yes" : "No");
  }

  return 0;
}