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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <vector>

using namespace std;

const double EPS = 1e-9;
bool EQ(double a, double b) { return abs(a - b) < EPS; }

double getHeight(double r, double x) { return sqrt(r * r - x * x); }

int main(void) {
  int r, n;
  int T = 0;
  while (scanf("%d %d", &r, &n), r > 0) {
    T++;
    int left[20], right[20], height[20];
    for (int i = 0; i < n; i++) {
      scanf("%d %d %d", left + i, right + i, height + i);
    }

    vector<double> xs;
    const double GAPEPS = 1e-7;
    xs.push_back(-r + GAPEPS);
    xs.push_back(r - GAPEPS);
    xs.push_back(r);
    xs.push_back(-r);
    for (int i = 0; i < n; i++) {
      double cand = left[i] - GAPEPS;
      if (-r <= cand and cand <= r)
        xs.push_back(cand);
      cand = left[i] + GAPEPS;
      if (-r <= cand and cand <= r)
        xs.push_back(cand);
      cand = right[i] - GAPEPS;
      if (-r <= cand and cand <= r)
        xs.push_back(cand);
      cand = right[i] + GAPEPS;
      if (-r <= cand and cand <= r)
        xs.push_back(cand);
      cand = left[i];
      if (-r <= cand and cand <= r)
        xs.push_back(cand);
      cand = right[i];
      if (-r <= cand and cand <= r)
        xs.push_back(cand);
    }
    for (int i = 1; i < 10001; i++)
      xs.push_back(-r + 2 * r / 10001.0 * i);

    sort(xs.begin(), xs.end());

    double s = 0, e = 100;
    for (int lv = 0; lv < 100; lv++) {
      double mid = (s + e) / 2;
      bool isAble = true;

      for (double x : xs) {
        bool isIncluded = false;

        double curHeight = getHeight(r, x) - r + mid;
        if (curHeight <= 0 or EQ(curHeight, 0))
          continue;
        for (int i = 0; i < n; i++)
          if (left[i] <= x and x <= right[i] and height[i] > curHeight and
              !EQ(height[i], curHeight)) {
            isIncluded = true;
            break;
          }

        if (!isIncluded) {
          isAble = false;
          break;
        }
      }

      if (isAble)
        s = mid;
      else
        e = mid;
    }

    printf("%.4lf\\n", s);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <math.h>
#include <stdio.h>
#define MAX 30

int R, n;
int term = 100000;
double answer;
struct data {
  int l, r, h;
};
data b[MAX];

bool go(int m) {
  int i, j;
  double x, y, t;
  bool flag;
  t = (double)m / term;

  for (i = -R * term; i <= R * term; i++) { // x??  i/10000
    x = (double)i / term;
    y = -((double)R - t) + sqrt((double)(R * R) - (x * x));
    if (y <= 0)
      continue;
    flag = false;
    for (j = 1; j <= n; j++) {
      if (b[j].l * term <= i && b[j].r * term >= i) {
        if (y <= b[j].h) {
          flag = true;
          break;
        }
      }
    }
    if (flag == false)
      return 0;
  }
  return 1;
}

int main(void) {
  int i;
  int f, l, mid;
  for (;;) {
    scanf("%d %d", &R, &n);
    if (R + n == 0)
      break;
    for (i = 1; i <= n; i++)
      scanf("%d %d %d", &b[i].l, &b[i].r, &b[i].h);
    f = 0, l = 50 * term;
    for (;;) {
      mid = (f + l) / 2;
      if (go(mid)) {
        answer = (double)mid / term;
        f = mid + 1;
      } else {
        l = mid - 1;
      }
      if (f > l)
        break;
    }
    printf("%.4lf\\n", answer);
  }
  return false;
}