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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define SQ(x) ((x) * (x))
#define Mod(x, mod) (((x)+(mod)%(mod))
#define MP make_pair
#define PB push_back
#define Fi first
#define Se second
#define INF (1 << 29)
#define EPS 1e-10
#define MOD 1000000007

typedef pair<int, int> pi;
typedef pair<pi, int> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int p[50];
int is[50];
int r, n;

int C(double t) {
  if (p[20] < t)
    return 0;
  for (int i = 0; i < 41; i++) {
    int x = i - 20;
    if (SQ((t - r) - p[i]) + SQ(x) < SQ(r))
      return 0;
  }
  return 1;
}
int main() {
  while (1) {
    cin >> r >> n;
    if (r == 0 && n == 0)
      return 0;
    memset(p, 0, sizeof(p));
    memset(is, -1, sizeof(is));
    int maxh = 0;
    int xl[32], xr[32], h[32];
    pii x[32];
    rep(i, n) {
      cin >> xl[i] >> xr[i] >> h[i];
      //      cin >> x[i].first.first >> x[i].first.second >> x[i].second;
      for (int j = xl[i] + 1; j < xr[i]; j++) {
        //      for(int j=x[i].first.first+1;j<x[i].first.second;j++){
        //	  cout << i << " " << j << " " << p[39] << endl;
        if (p[j + 20] < h[i]) {
          //	if(p[j+20] < x[i].second){
          //	  cout << p[j+20] << " " << x[i].second << endl;
          //	  p[j+20] = x[i].second;
          p[j + 20] = h[i];
        }
        //	p[j+20] = max(p[j+20], x[i].second);
      }
      //      for(int j=x[i].first.first;j<=x[i].first.second;j++) is[j] = 0;
      maxh = max(maxh, h[i]);
    }
    sort(x, x + n);
    rep(i, n) rep(j, n) {
      if (i == j)
        continue;
      //      if(x[i].first.second == x[j].first.first)
      //	p[x[i].first.second+20] = max(p[x[i].first.second+20],
      //min(x[i].second, x[j].second));
      if (xr[i] == xl[j])
        p[xr[i] + 20] = max(p[xr[i] + 20], min(h[i], h[j]));
    }
    //    rep(i, 41) cout << p[i] << " "; cout << endl;
    double lb = 0.0, ub = maxh;
    rep(i, 100) {
      double mid = (lb + ub) / 2;
      //      cout << mid << endl;
      if (C(mid))
        lb = mid;
      else
        ub = mid;
    }
    printf("%.12f\\n", lb);
  }
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define SQ(x) ((x) * (x))
#define Mod(x, mod) (((x)+(mod)%(mod))
#define MP make_pair
#define PB push_back
#define Fi first
#define Se second
#define INF (1 << 29)
#define EPS 1e-10
#define MOD 1000000007

typedef pair<int, int> pi;
typedef pair<pi, int> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int p[50];
int is[50];
int r, n;

int C(double t) {
  if (p[20] < t)
    return 0;
  for (int i = 0; i < 41; i++) {
    //    cout << x-r << " " << a.second << endl;
    int x = i - 20;
    if (SQ((t - r) - p[i]) + SQ(x) < SQ(r))
      return 0;
  }
  return 1;
}

int main() {
  while (1) {
    cin >> r >> n;
    if (r == 0 && n == 0)
      return 0;
    memset(p, 0, sizeof(p));
    memset(is, -1, sizeof(is));
    int maxh = 0;
    pii x[32];
    rep(i, n) {
      cin >> x[i].first.first >> x[i].first.second >> x[i].second;
      for (int j = x[i].first.first + 1; j < x[i].first.second; j++)
        p[j + 20] = max(p[j + 20], x[i].second);
      for (int j = x[i].first.first; j <= x[i].first.second; j++)
        is[j] = 0;
      maxh = max(maxh, x[i].second);
    }
    sort(x, x + n);
    rep(i, n - 1) {
      if (x[i].first.second == x[i + 1].first.first) {
        p[x[i].first.second + 20] = min(x[i].second, x[i + 1].second);
      } else {
        //	p[x[i].first.second+20] = 0;
        //	p[x[i+1].first.first+20] = 0;
      }
    }

    //    rep(i, 10) cout << p[i+15] << " "; cout << endl;
    double lb = 0.0, ub = maxh;
    rep(i, 100) {
      double mid = (lb + ub) / 2;
      //      cout << mid << endl;
      if (C(mid))
        lb = mid;
      else
        ub = mid;
    }
    printf("%.12f\\n", lb);
  }
  return 0;
}