Problem p00775 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <sys/time.h>
#include <vector>

using namespace std;

typedef long long ll;

#define EPS 1e-9

class biru {
public:
  double xl;
  double xr;
  double h;
  biru(double a, double b, double c) {
    xl = a;
    xr = b;
    h = c;
  }
};

double r;
bool crossCheck(double xc, double yc, double x1, double y1, double x2,
                double y2) {
  return (hypot(xc - x1, yc - y1) - r < EPS ||
          hypot(xc - x2, yc - y2) - r < EPS ||
          (fabs((y2 - y1) * xc + (x1 - x2) * yc + (x2 * y1 - x1 * y2)) /
                       hypot(x1 - x2, y2 - y1) -
                   r <
               EPS &&
           ((fabs(x1 - x2) > EPS &&
             (x1 <= xc && xc <= x2 || x2 <= xc && xc <= x1)) ||
            (fabs(y1 - y2) > EPS &&
             (y1 <= yc && yc <= y2 || y2 <= yc && yc <= y1)))));
}

bool compare(const biru a, const biru b) { return a.xl < b.xl; }

bool collision(double p, vector<biru> mbiru) {
  bool ok = true;
  if (-r < mbiru[0].xl) {
    ok = !crossCheck(0, p, -r, 0, mbiru[0].xl, 0);
  }

  if (crossCheck(0, p, mbiru[0].xl, 0, mbiru[0].xl, mbiru[0].h))
    ok = false;

  if (crossCheck(0, p, mbiru[0].xl, mbiru[0].h, mbiru[0].xr, mbiru[0].h))
    ok = false;
  if (mbiru.size() >= 2) {
    if (fabs(mbiru[0].xr - mbiru[1].xl) < EPS) {
      if (crossCheck(0, p, mbiru[0].xr, mbiru[0].h, mbiru[0].xr, mbiru[1].h)) {
        ok = false;
      }
    } else {
      if (crossCheck(0, p, mbiru[0].xr, mbiru[0].h, mbiru[0].xr, 0)) {
        ok = false;
      }
    }
  }
  for (int j = 1; j < mbiru.size(); j++) {
    if (fabs(mbiru[j].xl - mbiru[j - 1].xr) < EPS) {
      if (crossCheck(0, p, mbiru[j].xl, mbiru[j].h, mbiru[j].xl,
                     mbiru[j - 1].h)) {
        ok = false;
      }

    } else {
      if (crossCheck(0, p, mbiru[j - 1].xr, 0, mbiru[j].xl, 0) ||
          crossCheck(0, p, mbiru[j].xl, 0, mbiru[j].xl, mbiru[j].h)) {
        ok = false;
      }
    }
    if (crossCheck(0, p, mbiru[j].xl, mbiru[j].h, mbiru[j].xr, mbiru[j].h)) {
      ok = false;
    }
    if (j + 1 < mbiru.size()) {
      if (fabs(mbiru[j].xr - mbiru[j + 1].xl) > EPS) {
        if (crossCheck(0, p, mbiru[j].xr, mbiru[j].h, mbiru[j].xr, 0)) {
          ok = false;
        }
      }
    } else {
      if (crossCheck(0, p, mbiru[j].xr, mbiru[j].h, mbiru[j].xr, 0)) {
        ok = false;
      }
    }
    if (!ok)
      break;
  }
  if (crossCheck(0, p, mbiru[mbiru.size() - 1].xr, 0, 1000, 0)) {
    ok = false;
  }
  return !ok;
}

int main() {
  int tr, n;
  while (cin >> tr >> n) {
    if (tr == 0 && n == 0)
      break;
    if (n == 0) {
      std::cout << 0 << std::endl;
      continue;
    }
    r = (double)tr - 0.00001;
    bool field[41][21];
    memset(field, false, sizeof(field));
    for (int i = 0; i < n; i++) {
      int xl, xr, h;
      cin >> xl >> xr >> h;
      for (int j = 0; j < h; j++) {
        for (int k = xl; k < xr; k++) {
          field[k + 20][j] = true;
        }
      }
    }
    // for (int i = 0; i < 20; i++) {
    //   for (int j = 0; j < 40; j++) {
    // 	std::cout << field[j][i];
    //   }
    //   std::cout << "" << std::endl;
    // }

    vector<biru> mbiru;
    for (int i = 0; i < 41; i++) {
      if (field[i][0]) {
        int h;
        for (int j = 0; field[i][j]; j++) {
          h = j + 1;
        }
        mbiru.push_back(biru(i - 20, i - 20 + 1, h));
      }
    }
    // for (int i = 0; i < mbiru.size(); i++) {
    //   std::cout << mbiru[i].xl << " " << mbiru[i].xr << " " << mbiru[i].h <<
    //   std::endl;
    // }

    double lp = -200, rp = 0;
    for (int i = -20; i < 100; i++) {
      rp = double(i);
      if (collision(rp, mbiru)) {
        break;
      }
    }

    double p;
    for (int i = 0; i < 100; i++) {
      p = (lp + rp) / 2;
      if (collision(p, mbiru)) {
        rp = p;
      } else {
        lp = p;
      }
    }
    printf("%lf\\n", lp + r);
  }
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <sys/time.h>
#include <vector>

using namespace std;

typedef long long ll;

#define EPS 1e-9

class biru {
public:
  double xl;
  double xr;
  double h;
  biru(double a, double b, double c) {
    xl = a;
    xr = b;
    h = c;
  }
};

double r;
bool crossCheck(double xc, double yc, double x1, double y1, double x2,
                double y2) {
  return (hypot(xc - x1, yc - y1) - r < EPS ||
          hypot(xc - x2, yc - y2) - r < EPS ||
          (fabs((y2 - y1) * xc + (x1 - x2) * yc + (x2 * y1 - x1 * y2)) /
                       hypot(x1 - x2, y2 - y1) -
                   r <
               EPS &&
           ((fabs(x1 - x2) > EPS &&
             (x1 <= xc && xc <= x2 || x2 <= xc && xc <= x1)) ||
            (fabs(y1 - y2) > EPS &&
             (y1 <= yc && yc <= y2 || y2 <= yc && yc <= y1)))));
}

bool compare(const biru a, const biru b) { return a.xl < b.xl; }

int main() {
  int tr, n;
  while (cin >> tr >> n) {
    if (tr == 0 && n == 0)
      break;
    r = (double)tr - 0.00001;
    bool field[41][21];
    memset(field, false, sizeof(field));
    for (int i = 0; i < n; i++) {
      int xl, xr, h;
      cin >> xl >> xr >> h;
      for (int j = 0; j < h; j++) {
        for (int k = xl; k < xr; k++) {
          field[k + 20][j] = true;
        }
      }
    }
    // for (int i = 0; i < 10; i++) {
    //   for (int j = 15; j < 25; j++) {
    // 	std::cout << field[j][i];
    //   }
    //   std::cout << "" << std::endl;
    // }

    vector<biru> mbiru;
    for (int i = 0; i < 41; i++) {
      if (field[i][0]) {
        int h;
        for (int j = 0; field[i][j]; j++) {
          h = j + 1;
        }
        mbiru.push_back(biru(i - 20, i - 20 + 1, h));
      }
    }
    // for (int i = 0; i < mbiru.size(); i++) {
    //   std::cout << mbiru[i].xl << " " << mbiru[i].xr << " " << mbiru[i].h <<
    //   std::endl;
    // }

    double p;
    for (int i = 0; i < 400000; i++) {
      p = -r + double(i) / 10000.;
      bool ok = true;
      if (-r < mbiru[0].xl) {
        ok = !crossCheck(0, p, -r, 0, mbiru[0].xl, 0);
      }
      if (crossCheck(0, p, mbiru[0].xl, 0, mbiru[0].xl, mbiru[0].h))
        ok = false;
      if (crossCheck(0, p, mbiru[0].xl, mbiru[0].h, mbiru[0].xr, mbiru[0].h))
        ok = false;
      if (mbiru.size() >= 2) {
        if (fabs(mbiru[0].xr - mbiru[1].xl) < EPS) {
          ok = !crossCheck(0, p, mbiru[0].xr, mbiru[0].h, mbiru[0].xr,
                           mbiru[1].h);
        } else {
          ok = !crossCheck(0, p, mbiru[0].xr, mbiru[0].h, mbiru[0].xr, 0);
        }
      }
      for (int j = 1; j < mbiru.size(); j++) {
        if (fabs(mbiru[j].xl - mbiru[j - 1].xr) < EPS) {
          if (crossCheck(0, p, mbiru[j].xl, mbiru[j].h, mbiru[j].xl,
                         mbiru[j - 1].h)) {
            ok = false;
          }

        } else {
          if (crossCheck(0, p, mbiru[j - 1].xr, 0, mbiru[j].xl, 0) ||
              crossCheck(0, p, mbiru[j].xl, 0, mbiru[j].xl, mbiru[j].h)) {
            ok = false;
          }
        }
        if (crossCheck(0, p, mbiru[j].xl, mbiru[j].h, mbiru[j].xr,
                       mbiru[j].h)) {
          ok = false;
        }
        if (j + 1 < mbiru.size()) {
          if (fabs(mbiru[j].xr - mbiru[j + 1].xl) > EPS) {
            if (crossCheck(0, p, mbiru[j].xr, mbiru[j].h, mbiru[j].xr, 0)) {
              ok = false;
            }
          }
        } else {
          if (crossCheck(0, p, mbiru[j].xr, mbiru[j].h, mbiru[j].xr, 0)) {
            ok = false;
          }
        }
        if (!ok)
          break;
      }
      if (crossCheck(0, p, mbiru[mbiru.size() - 1].xr, 0, 1000, 0)) {
        ok = false;
      }
      if (!ok) {
        break;
      }
    }
    printf("%lf\\n", p + r);
  }
}