alexjercan/bugnet huggingface dataset:
Problem p00774 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;

int main() {

  while (true) {

    long long int h;
    cin >> h;
    if (h == 0)
      break;

    vector<vector<long long int>> v;

    for (long long int y = 0; y < h; y++) {

      vector<long long int> w;
      w.push_back(0);

      for (long long int x = 0; x < 5; x++) {

        long long int in;
        cin >> in;
        w.push_back(in);
      }

      w.push_back(0);

      v.push_back(w);
    }

    long long int ans = 0;

    while (true) {

      bool f = false;

      for (long long int y = 0; y < h; y++) {

        long long int cnt = 0;
        long long int num = 0;

        for (long long int x = 0; x < 7; x++) {

          if (v[y][x] != num) {
            if (cnt >= 3 && num != 0) {
              ans += cnt * num;
              f = true;
              for (long long int k = 0; k < cnt; k++) {
                v[y][x - k - 1] = 0;
              }
            }
            cnt = 1;
            num = v[y][x];
          } else {
            cnt++;
          }
        }
      }

      if (f == false) {
        cout << ans << endl;
        break;
      }

      for (long long int x = 1; x <= 5; x++) {

        long long int m = -1;

        for (long long int y = h - 1; y >= 0; y--) {

          if (v[y][x] == 0 && m == -1) {
            m = y;
          }
          if (v[y][x] != 0 && m != -1) {
            v[m][x] = v[y][x];
            v[y][x] = 0;
            m--;
          }
        }
      }
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

int main() {

  while (true) {

    long long int h;
    cin >> h;
    if (h == 0)
      break;

    vector<vector<long long int>> v;

    for (long long int y = 0; y < h; y++) {

      vector<long long int> w;
      w.push_back(0);

      for (long long int x = 0; x < 5; x++) {

        long long int in;
        cin >> in;
        w.push_back(in);
      }

      w.push_back(0);

      v.push_back(w);
    }

    long long int ans = 0;

    while (true) {

      bool f = false;

      for (long long int y = 0; y < h; y++) {

        long long int cnt = 0;
        long long int num = 0;

        for (long long int x = 0; x < 7; x++) {

          if (v[y][x] != num) {
            if (cnt >= 3) {
              ans += cnt * num;
              f = true;
              for (long long int k = 0; k < cnt; k++) {
                v[y][x - k - 1] = 0;
              }
            }
            cnt = 1;
            num = v[y][x];
          } else {
            cnt++;
          }
        }
      }

      if (f == false) {
        cout << ans << endl;
        break;
      }

      for (long long int x = 1; x <= 5; x++) {

        long long int m = -1;

        for (long long int y = h - 1; y >= 0; y--) {

          if (v[y][x] == 0 && m == -1) {
            m = y;
          }
          if (v[y][x] != 0 && m != -1) {
            v[m][x] = v[y][x];
            m--;
          }
        }
      }
    }
  }

  return 0;
}
Problem p00774 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int H;
  //?????????????????¶????????\\???
  while (cin >> H && H > 0) {
    vector<vector<int>> c(5, vector<int>(H));
    int sum = 0;
    for (int i = H - 1; i >= 0; i--) {
      for (int j = 0; j < 5; j++) {
        cin >> c[j][i];
      }
    }
    //????????°??????3?????\\????¨???????????????´???????????°????¨??????????0?????????
    int flg = 1; //?¶?????????¨???flg == 1
    while (flg) {
      flg = 0;
      for (int i = H - 1; i >= 0; i--) {
        for (int j = 0; j < 3; j++) {
          int cnt = 1; //?¨???????????????°
          if (c[j][i] != 0) {
            int flag = 1;
            for (int k = j + 1; k < 5 && flag;
                 k++) { //????¨??????????????????????
              if (c[j][i] == c[k][i])
                cnt++;
              if (c[j][i] != c[k][i] ||
                  k == 4) { //?????????????????£???????????????????????????
                flag = 0;
                if (cnt >= 3) {         // 3?????\\???????????§?????¨???
                  sum += c[j][i] * cnt; //????????????
                  for (int l = j; l <= j + cnt - 1; l++) { // 0???????????????
                    c[l][i] = 0;
                  }
                  flg = 1;
                }
              }
            }
          }
        }
      }
      for (int j = 0; j < 5; j++) {
        vector<int>::iterator itr = remove(c[j].begin(), c[j].end(), 0);
        for (; itr != c[j].end(); itr++)
          *itr = 0;
      }
    }
    //???????????????????????????0????????£??????flg==0??¨?????????????????±???
    cout << sum << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int H;
  //?????????????????¶????????\\???
  while (cin >> H && H > 0) {
    vector<vector<int>> c(5, vector<int>(H));
    int sum = 0;
    for (int i = H - 1; i >= 0; i--) {
      for (int j = 0; j < 5; j++) {
        cin >> c[j][i];
      }
    }
    //????????°??????3?????\\????¨???????????????´???????????°????¨??????????0?????????
    int flg = 1;
    while (flg) {
      flg = 0;
      for (int i = H - 1; i >= 0; i--) {
        for (int j = 0; j < 3; j++) {
          int cnt = 1;
          for (int k = j + 1; k < 5; k++) {
            if (c[j][i] == c[k][i])
              cnt++;
            if (c[j][i] != c[k][i] || k == 4) {
              if (cnt >= 3) {
                sum += c[j][i] * cnt;
                for (int l = j; l <= k; l++) {
                  c[l][i] = 0;
                }
                flg = 1;
              }
            }
          }
        }
      }
      for (int j = 0; j < 5; j++) {
        remove(c[j].begin(), c[j].end(), 0), c[j].end();
      }
    }
    //???????????????????????????0????????£??????flg==0??¨?????????????????±???
    cout << sum << endl;
  }
}
Problem p00775 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int y[101], r, n;

double mk_dis(int x1, int h, double cy) {
  return sqrt(x1 * x1 + (h - cy) * (h - cy));
}

bool check(double cy) {
  cy = (int)(cy * 100000) / 100000.0;
  double x = fabs(sqrt(r * r - cy * cy));
  if (cy >= 0)
    x = r;
  int L = (-x - 0.999999), R = (x + 0.999999), hb[101] = {};
  for (int i = L + 50; i < R + 50; i++)
    if (i >= 50) {
      if (mk_dis(i - 50, y[i], cy) >= r)
        hb[i]++;
    } else {
      if (mk_dis(i - 49, y[i], cy) >= r)
        hb[i]++;
    }

  for (int i = L + 50; i < R + 50; i++)
    if (!hb[i])
      return false;
  return true;
}

int main() {
  while (1) {
    cin >> r >> n;
    if (!r && !n)
      break;
    memset(y, 0, sizeof(y));
    for (int i = 0; i < n; i++) {
      int x1, x2, h;
      cin >> x1 >> x2 >> h;
      for (int j = x1; j < x2; j++)
        y[j + 50] = max(y[j + 50], h);
    }
    double ans;
    for (ans = 0; ans < 41 && check(ans - r); ans += 0.0001)
      ;
    printf("%lf\\n", ans - 0.0001);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int y[101], r, n;

double mk_dis(int x1, int h, double cy) {
  return sqrt(x1 * x1 + (h - cy) * (h - cy));
}

bool check(double cy) {
  cy = (int)(cy * 100000) / 100000.0;
  double x = fabs(sqrt(r * r - cy * cy));
  if (cy >= 0)
    x = r;
  int L = (-x - 0.999999), R = (x + 0.999999), hb[101] = {};
  for (int i = L + 50; i < R + 50; i++)
    if (i >= 50) {
      if (mk_dis(i - 50, y[i], cy) >= r)
        hb[i]++;
    } else {
      if (mk_dis(i - 49, y[i], cy) >= r)
        hb[i]++;
    }

  for (int i = L + 50; i < R + 50; i++)
    if (!hb[i])
      return false;
  return true;
}

int main() {
  while (1) {
    cin >> r >> n;
    if (!r && !n)
      break;
    memset(y, 0, sizeof(y));
    for (int i = 0; i < n; i++) {
      int x1, x2, h;
      cin >> x1 >> x2 >> h;
      for (int j = x1; j < x2; j++)
        y[j + 50] = max(y[j + 50], h);
    }
    double ans;
    for (ans = 0; ans < 41 && check(ans - r); ans += 0.00001)
      ;
    printf("%lf\\n", ans - 0.00001);
  }
  return 0;
}
Problem p00775 (C++) — buggy submission, verdict: Time Limit Exceeded

// #define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define IN(a, b, x) (a <= x && x < b)
#define MP make_pair
#define PB push_back
#define MOD 1000000007
#define INF (1LL << 30)
#define LLINF (1LL << 60)
#define PI 3.14159265359
#define EPS 1e-12
// #define int ll

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

int xl[25], xr[25], h[25];
signed main(void) {
  while (true) {
    int r, n;
    cin >> r >> n;
    if (!r && !n)
      break;
    REP(i, n) cin >> xl[i] >> xr[i] >> h[i];

    double hi = 60, low = 0;
    REP(i, 20) {
      double mid = (hi + low) / 2;
      // cout << hi << " " << mid << " " << low << endl;
      bool flag = true;
      REP(j, 200000) {
        double x = -r + (double)(2 * r) * j / 200000;
        int ma = 0;
        REP(k, n) {
          if (xl[k] <= x && x <= xr[k]) {
            ma = max(ma, h[k]);
          }
        }
        double H = sqrt(r * r - x * x) + mid - r;
        // cout << ma << " " << H << endls
        if (ma < H) {
          flag = false;
          break;
        }
      }
      if (flag) {
        low = mid;
      } else {
        hi = mid;
      }
    }
    cout << fixed << setprecision(10) << low << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

// #define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define IN(a, b, x) (a <= x && x < b)
#define MP make_pair
#define PB push_back
#define MOD 1000000007
#define INF (1LL << 30)
#define LLINF (1LL << 60)
#define PI 3.14159265359
#define EPS 1e-12
// #define int ll

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

int xl[25], xr[25], h[25];
signed main(void) {
  while (true) {
    int r, n;
    cin >> r >> n;
    if (!r && !n)
      break;
    REP(i, n) cin >> xl[i] >> xr[i] >> h[i];

    double hi = 60, low = 0;
    REP(i, 20) {
      double mid = (hi + low) / 2;
      // cout << hi << " " << mid << " " << low << endl;
      bool flag = true;
      REP(j, 300000) {
        double x = -r + (double)(2 + r) * j / 300000;
        int ma = 0;
        REP(k, n) {
          if (xl[k] <= x && x <= xr[k]) {
            ma = max(ma, h[k]);
          }
        }
        double H = sqrt(r * r - x * x) + mid - r;
        // cout << ma << " " << H << endls
        if (ma < H) {
          flag = false;
          break;
        }
      }
      if (flag) {
        low = mid;
      } else {
        hi = mid;
      }
    }
    cout << fixed << setprecision(10) << low << endl;
  }

  return 0;
}
Problem p00775 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;

int main() {
  int n, r, h, V[41], xl, xr;
  while (cin >> r >> n, r) {
    for (int i = 0; i < 2 * r + 1; ++i)
      V[i] = 0;
    double ans = 20.0;
    for (int i = 0; i < n; ++i) {
      cin >> xl >> xr >> h;
      for (int j = xl; j < xr; ++j) {
        if (j < -r)
          continue;
        if (j > r)
          break;
        V[j + r] = max(h, V[j + r]);
      }
      if (xr == r)
        V[2 * r] = max(V[2 * r], h);
    }
    for (int i = 0; i < 2 * r + 1; ++i) {
      ans = min(ans, V[i] + r - sqrt(r * r - (i - r) * (i - r)));
      if (i + 1 - r <= r)
        ans = min(ans, V[i] + r - sqrt(r * r - (i + 1 - r) * (i + 1 - r)));
      if (abs(i - 1 - r) <= r)
        ans = min(ans, V[i - 1] + r - sqrt(r * r - (i - r) * (i - r)));
    }
    printf("%.4lf\\n", ans);
  }
  return 0;
}

Error: -6
double free or corruption (out)


Fixed submission:

#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n, r, h;
  double xl, xr, ans;
  vector<int> V(41, 0);
  while (cin >> r >> n, r) {
    for (int i = 0; i < 41; ++i)
      V[i] = 0;
    ans = 20;
    for (int i = 0; i < n; ++i) {
      cin >> xl >> xr >> h;
      for (int j = xl; j < xr; ++j) {
        if (j > r)
          break;
        V[j + r] = max(h, V[j + r]);
      }
      if (xr == r)
        V[2 * r] = max(V[2 * r], h);
    }
    for (int i = 0; i < 2 * r + 1; ++i) {
      ans = min(ans, V[i] + r - sqrt(r * r - (i - r) * (i - r)));
      if (i + 1 - r <= r)
        ans = min(ans, V[i] + r - sqrt(r * r - (i + 1 - r) * (i + 1 - r)));
    }
    printf("%.4lf\\n", ans);
  }
  return 0;
}
Problem p00775 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>

using namespace std;

int r, n, L, R, h, i, x;

int main() {

  while (cin >> r >> n, r) {

    int A[41] = {}, B[41] = {};
    for (i = 0; i < n; i++) {
      cin >> L >> R >> h;
      for (x = L; x <= R; x++) {
        if (x != R)
          A[x + 20] = max(A[x + 20], h);
        if (x != L)
          B[x + 20] = max(B[x + 20], h);
      }
    }
    double res = 99;
    for (i = -r; i <= r - 1; i++) {
      res = min(res, min(r - sqrt(r * r - i * i) + A[i + 20],
                         r - sqrt(r * r - (i + 1) * (i + 1)) + B[i + 1 + 20]));
    }
    cout << res << "\\n";
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>

using namespace std;

int r, n, L, R, h, i, x;

int main() {

  while (cin >> r >> n, r) {

    int A[41] = {}, B[41] = {};
    for (i = 0; i < n; i++) {
      cin >> L >> R >> h;
      for (x = L; x <= R;) {
        if (x != R)
          A[x + 20] = max(A[x + 20], h);
        if (x != L)
          B[x + 20] = max(B[x++ + 20], h);
      }
    }
    double res = 99;
    for (i = -r; i <= r - 1;) {
      res =
          min(res, min(r - sqrt(r * r - i * i) + A[i + 20],
                       r - sqrt(r * r - (i + 1) * (i + 1)) + B[i++ + 1 + 20]));
    }
    cout << res << "\\n";
  }

  return 0;
}
Problem p00775 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, a) for (int i = 0; i < a; i++)
#define mp make_pair
#define pb push_back
#define P pair<int, int>
#define ll __int64
int n;
double r;
double x[100], x2[100], h[100];
double EPS = 0.0001;
bool che(double t) {

  for (int i = -r; i < r; i++) {
    int a = i,
        b = i + 1; // aとb両方でのyを求める[a,b]内で最大の高さの長方形を求める
    int in = -1;
    double ma = -1, y1, y2;
    for (int j = 0; j < n; j++) {
      if (x[j] <= a && b <= x2[j] && ma < h[j]) {
        ma = h[j];
        in = j;
      }
    }
    y1 = sqrt(r * r - a * a) + t - r;
    y2 = sqrt(r * r - b * b) + t - r;
    // cout<<a<<" "<<b<<" "<<y1<<" "<<y2<<endl;
    if (!(h[in] >= y1 && h[in] >= y2))
      return 0;
  }

  return 1;
}

/*
for(int i=0;i<n;i++){
        double lx=x[i],rx=x2[i];
        if(lx<-r|| rx>r)continue;
        double ny=t-r;
        double y1,y2;
        y1=sqrt(r*r-lx*lx)+ny;
        y2=sqrt(r*r-rx*rx)+ny;

        //cout<<y1<<" "<<y2<<" "<<h[i]<<endl;
        if(!(h[i]>=y1&&h[i]>=y2))return 0;
        }
*/

int main() {
  while (cin >> r >> n, r || n) {
    rep(i, n) cin >> x[i] >> x2[i] >> h[i];

    double mid = 0, l = 0, r = 100;
    mid = (r + l) / 2;
    for (int i = 0; i < 20; i++) {
      if (che(mid)) {
        l = mid;
      } else {
        r = mid;
      }
      mid = (r + l) / 2;
    }

    printf("%.20lf\\n", mid);
    rep(i, n) x[i] = x2[i] = h[i] = 0;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, a) for (int i = 0; i < a; i++)
#define mp make_pair
#define pb push_back
#define P pair<int, int>
#define ll __int64
int n;
double r;
double x[100], x2[100], h[100];
double EPS = 0.0001;
bool che(double t) {
  double fai;
  int B = 500000;
  double fy;

  for (int i = 0; i <= B; i++) {
    fai = M_PI * (double)i / (double)B;
    double nx = r * cos(fai);         // 円上のx
    double ny = r * sin(fai) + t - r; // 円上のy

    fy = -10000;
    double fx = 0;
    for (int j = 0; j < n; j++) {
      if (x[j] <= nx && nx <= x2[j])
        fy = max(fy, h[j]);
    }
    // cout<<nx<<" "<<ny<<" "<<fy<<endl;

    if (fy <= -9999 || ny > fy)
      return 0;
  }
  return 1;
}
int main() {
  while (cin >> r >> n, r || n) {
    rep(i, n) cin >> x[i] >> x2[i] >> h[i];

    double mid = 0, l = 0, r = 100;
    mid = (r + l) / 2;
    for (int i = 0; i < 30; i++) {
      if (che(mid)) {
        l = mid;
      } else {
        r = mid;
      }
      mid = (r + l) / 2;
    }

    printf("%.20lf\\n", mid);
    rep(i, n) x[i] = x2[i] = h[i] = 0;
  }

  return 0;
}
Problem p00775 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define rp(a) while (a--)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double EPS = 1e-10;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
int main() {
  int n;
  double r;
  while (cin >> r >> n, r || n) {
    double h[50] = {0};
    rep(i, n) {
      int a, b;
      double c;
      cin >> a >> b >> c;
      a += 25;
      b += 25;
      for (int i = a; i < b; i++)
        h[i] = max(h[i], c);
    }
    // loop(i,20,30)cout<<" "<<h[i];
    // cout<<endl;
    double t = 0;
    while (1) {
      t += 0.0001;
      bool ans = true;
      rep(i, 49) {
        if ((double)(i - 25) * (i - 25) + (r + h[i] - t) * (r + h[i] - t) <
            r * r) {
          ans = false;
          break;
        }
        if ((double)(i - 24) * (i - 24) + (r + h[i] - t) * (r + h[i] - t) <
            r * r) {
          ans = false;
          break;
        }
      }
      if (!ans)
        break;
    }
    cout << t - 0.0001 << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define rp(a) while (a--)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double EPS = 1e-10;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
int main() {
  int n;
  double r;
  while (cin >> r >> n, r || n) {
    double h[50] = {0};
    rep(i, n) {
      int a, b;
      double c;
      cin >> a >> b >> c;
      a += 25;
      b += 25;
      for (int i = a; i < b; i++)
        h[i] = max(h[i], c);
    }
    // loop(i,20,30)cout<<" "<<h[i];
    // cout<<endl;
    double t = 0;
    while (1) {
      t += 0.0001;
      bool ans = true;
      rep(i, 49) {
        if (hypot((double)(i - 25), r + h[i] - t) < r) {
          ans = false;
          break;
        }
        if (hypot((double)(i - 24), r + h[i] - t) < r) {
          ans = false;
          break;
        }
      }
      if (!ans)
        break;
    }
    cout << t - 0.0001 << endl;
  }
}
Problem p00775 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)

int s[21][41];
int main(void) {
  int r, n, xl, xr, h;
  while (cin >> r >> n && r) {
    rep(i, 21) rep(j, 41) s[i][j] = 0;
    rep(i, n) {
      cin >> xl >> xr >> h;
      for (int y = 0; y < h; ++y) {
        for (int x = xl + 20; x < xr + 20; ++x) {
          s[y][x] = 1;
        }
      }
    }
    double mint = numeric_limits<double>::max();
    rep(y, 21) {
      for (int x = -r + 20; x < r + 20; ++x) {
        if (s[y][x] == 0) {
          double t;
          if (x < 20)
            t = (double)y + r - sqrt(pow((double)r, 2.0) - pow(x - 19.0, 2.0));
          else
            t = (double)y + r - sqrt(pow((double)r, 2.0) - pow(x - 20.0, 2.0));
          mint = min(mint, t);
        }
      }
    }
    printf("%.4lf\\n", mint);
  }
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)

int s[21][41];
int main(void) {
  int r, n, xl, xr, h;
  while (cin >> r >> n && r) {
    rep(i, 41) rep(j, 41) s[i][j] = 0;
    rep(i, n) {
      cin >> xl >> xr >> h;
      for (int y = 0; y < h; ++y) {
        for (int x = xl + 20; x < xr + 20; ++x) {
          s[y][x] = 1;
        }
      }
    }
    /*for(int y=20; y>=0; --y) {
        rep(x, 41) cout << s[y][x];
        cout << endl;
    }*/
    double mint = numeric_limits<double>::max();
    rep(y, 21) {
      for (int x = -r + 20; x < r + 20; ++x) {
        if (s[y][x] == 0) {
          double t;
          if (x < 20)
            t = (double)y + r - sqrt(pow((double)r, 2.0) - pow(x - 19.0, 2.0));
          else
            t = (double)y + r - sqrt(pow((double)r, 2.0) - pow(x - 20.0, 2.0));
          mint = min(mint, t);
        }
      }
    }
    printf("%.4lf\\n", mint);
  }
}
Problem p00775 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <vector>

using namespace std;

int main() {

  int r, n;
  while (cin >> r >> n && r) {

    map<int, int> d, d2;
    for (int x = -21; x < 21; x++)
      d[x] = 0;
    for (int i = 0; i < n; i++) {
      int xl, xr, h;
      cin >> xl >> xr >> h;
      for (int x = xl + 1; x <= xr; x++)
        d[x] = max(d[x], h);
      for (int x = xl; x < xr; x++)
        d2[x] = max(d2[x], h);
    }

    double ans = 1e9;
    for (double x = -r; x <= r; x++) {

      double ty = double(r) - sin(acos(abs(x / double(r)))) * double(r);

      if (x != -r)
        ans = min(ans, double(d[x] + ty));
      if (x != r)
        ans = min(ans, double(d2[x] + ty));
    }

    cout << setprecision(6) << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <vector>

using namespace std;

int main() {

  int r, n;
  while (cin >> r >> n && r) {

    map<int, int> d;
    for (int x = -21; x < 21; x++)
      d[x] = 0;
    for (int i = 0; i < n; i++) {
      int xl, xr, h;
      cin >> xl >> xr >> h;
      for (int x = xl + 1; x <= xr; x++)
        d[x] = max(d[x], h);
    }

    double ans = 1e9;
    for (double x = -r; x <= r; x += 1e-5) {

      /*
      double y = -double(r) + sqrt(double(r)*double(r) - x*x);
      */
      int tx = (x >= 0 ? int(x + (1.0 - 1e-7)) : int(x));
      if (x == -r)
        tx++;
      double ty = double(r) - sin(acos(abs(x / double(r)))) * double(r);

      ans = min(ans, double(d[tx]) + ty);
    }

    cout << setprecision(6) << ans << endl;
  }

  return 0;
}