Problem:
Vampire
Mr. C is a vampire. If he is exposed to the sunlight directly, he turns 
into ash.
Nevertheless, last night, he attended to the meeting of Immortal and 
Corpse Programmers
Circle, and he has to go home in the near dawn.
Fortunately, there are many tall buildings around Mr. C's home, and 
while the sunlight is blocked by the buildings, he can move around 
safely.
The top end of the sun has just reached the horizon now.
In how many seconds does Mr. C have to go into his safe coffin?
To simplify the problem, we represent the eastern dawn sky as a 2-dimensional
x
-
y
plane, where the
x
axis is horizontal and the
y
axis
is vertical, and approximate building silhouettes by rectangles
and the sun by a circle on this plane.
The
x
axis represents the horizon.
We denote the time by
t
, and the current time is
t
=0.
The radius of the sun is
r
and
its center is at (0, -
r
) when the time
t
=0.
The sun moves on the
x
-
y
plane in a uniform linear motion at
a constant velocity of (0, 1) per second.
The sunlight is blocked if and only if the entire region of the sun 
(including its edge) is included in the union of the silhouettes 
(including their edges) and the region below the horizon (y ≤ 0).
Write a program that computes the time of the last moment when the sunlight is blocked.
The following figure shows the layout of silhouettes and the position of the sun at
the last moment when the sunlight is blocked, that corresponds to the
first dataset of Sample Input below. As this figure indicates, there are possibilities
that the last moment when the sunlight is blocked can be the time
t
=0.
The sunlight is blocked even when two silhouettes share parts of their edges.

The following figure shows the layout of silhouettes and the position of the sun at the
last moment when the sunlight is blocked, corresponding to the second
dataset of Sample Input.
In this dataset the radius of the sun is 2 and there are two silhouettes:
the one with height 4 is in -2 ≤
x
≤ 0, and
the other with height 3 is in 0 ≤
x
≤ 2.
Input
The input consists of multiple datasets.
The first line of a dataset contains two
integers
r
and
n
separated by a space.
r
is the radius of the sun and
n
is the number of silhouettes
of the buildings.
(1 ≤
r
≤ 20, 0 ≤
n
≤ 20)
Each of following
n
lines contains three integers
x
li
,
x
ri
,
h
i
(1 ≤
i
≤
n
) separated by a space.
These three integers represent a silhouette rectangle of a building.
The silhouette rectangle is parallel to the horizon, and its left and right edges
are at
x
=
x
li
and
x
=
x
ri
, its top
edge is at
y
=
h
i
, and its bottom edge is on
the horizon.
(-20 ≤
x
li
<
x
ri
≤ 20,
0 <
h
i
≤ 20)
The end of the input is indicated by a line containing two zeros separated by a space.
Note that these silhouettes may overlap one another.
Output
For each dataset, output a line containing the number indicating the time
t
of the last moment when the sunlight is blocked.
The value should not have an error greater than 0.001.
No extra character
...(truncated)

Submitted solutions:

code_snippet: #include <cstdio>
#include <cmath>
#include <algorithm>
#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;
}
accuracy: 1
time_ms: 630
memory_kb: 3008
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;

const long long MOD1 = 1000000007;
const long long MOD2 = 998244353;
#define all(v) v.begin(), v.end()
#define rall(a) a.rbegin(), a.rend()
#define fi first
#define se second
#define endl "\n"

#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)

template <typename T>
using vc = vector<T>; // prioriy_queueに必要なのでここにこれ書いてます
template <typename T>
using vv = vc<vc<T>>;
#define int long long
using ll = long long;
ll INF = 2e18;

#ifdef ONLINE_JUDGE
// ジャッジ環境ではデバッグマクロを無効化
#define debug_x(x) \
    do             \
    {              \
    } while (0)
#define debug_vc(v) \
    do              \
    {               \
    } while (0)
#define debug_vv(v) \
    do              \
    {               \
    } while (0)
#else
// ローカルではデバッグ出力
#define debug_x(x) cout << #x << " = " << (x) << endl
#define debug_vc(v)                    \
    {                                  \
        cout << #v << endl;            \
        ll n = size(v);                \
        rep(i, n) cout << v[i] << ' '; \
        cout << endl;                  \
    } // 一次元配列を出力する
#define debug_vv(v)                                        \
    {                                                      \
        cout << #v << endl;                                \
        ll n = size(v);                                    \
        rep(i, n)                                          \
        {                                                  \
            rep(j, size(v[i])) { cout << v[i][j] << ' '; } \
            cout << endl;                                  \
        }                                                  \
    } // 二次元配列を出力する
#endif

#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rrep(i, n) for (ll i = 1; i <= (n); ++i)
#define drep(i, n) for (ll i = (n) - 1; i >= 0; --i)
#define nfor(i, s, n) for (ll i = s; i < n; i++)        // i=s,s+1...n-1 ノーマルfor
#define dfor(i, s, n) for (ll i = (s) - 1; i >= n; i--) // s-1スタートでnまで落ちる

using ld = long double;
using bl = bool;

template <class T>
using pq = priority_queue<T, vc<T>>; // 大きい順
template <class T>
using pq_g = priority_queue<T, vc<T>, greater<T>>; // 小さい順

using vl = vc<ll>;
using vvl = vv<ll>;
using vvvl = vv<vl>;
using vvvvl = vv<vvl>;
using vs = vc<string>;
using vvs = vv<string>;
using vb = vc<bl>;
using vvb = vv<bl>;
using vvvb = vv<vb>;
using vld = vc<ld>;
using vvld = vv<ld>;
using vvvld = vv<vld>;
using P = pair<ll, ll>;
using vP = vc<P>;
using vvP = vc<vP>;
using vvvP = vv<vP>;

#define pb push_back

#define YES cout << "Yes" << endl
#define NO cout << "No" << endl
#define YN                     \
    {                          \
        cout << "Yes" << endl; \
    }                          \
    else                       \
    {                          \
        cout << "No" << endl;  \
    } // if(a==b)YN;
#define dame cout << -1 << endl

string dir = "DRUL";   // 第4象限の場合
vl di = {1, 0, -1, 0}; // vl di={1,1,0,-1,-1,-1,0,1};
vl dj = {0, 1, 0, -1}; // vl dj={0,1,1,1,0,-1,-1,-1};

bool out_grid(ll i, ll j, ll h, ll w)
{ // trueならcontinue
    return (!(0 <= i && i < h && 0 <= j && j < w));
}

bl solve()
{
    ll r, n;
    cin >> r >> n;
    if (n == 0 && r == 0)
        return 0;
    vl xl(n), xr(n), h(n);
    rep(i, n)
    {
        cin >> xl[i] >> xr[i] >> h[i];
    }
    // すべてのありうる頂点に対して全探索
    // vl maxH(41);
    // rep(i, n)
    // {
    //     if(xl[i]<0){
    //         xl[i]--;
    //     }
    //     if(xr[i]<0){
    //         xr[i]--;
    //     }
    //     for (ll j = xl[i] + 1; j <= xr[i]; j++)
    //     {
    //         maxH[j+20] = max(maxH[j+20],h[i]);
    //     }
    // }
    // debug_vc(maxH);
    // maxH[20] = min(maxH[19],maxH[21]);
    set<P> coord;
    set<P> isRight;
    set<P> isLeft;
    for (ll x = -20; x <= 20; x++)
    {
        for (ll y = 0; y <= 20; y++)
        {
            coord.insert({x, y});
        }
    }
    rep(i, n)
    {
        for (ll x = xl[i] + 1; x < xr[i]; x++)
        {
            for (ll y = 0; y < h[i]; y++)
            {
                coord.erase({x, y});
            }
        }
        for (ll y = 0; y < h[i]; y++)
        {
            isLeft.insert({xl[i], y});
            isRight.insert({xr[i], y});
        }
    }
    for (ll x = -20; x <= 20; x++)
    {
        for (ll y = 0; y <= 20; y++)
        {
            if (isRight.count({x, y}) && isLeft.count({x, y}))
                coord.erase({x, y});
        }
    }
    // for (ll x = -2; x <= 2; x++)
    // {
    //     for (ll y = 0; y <= 4; y++)
    //     {
    //         if (coord.count({x,y})){
    //             cout << x << y << endl;
    //         }
    //     }
    // }
    ld ans = 1e4;
    for (auto [x, y] : coord)
    {
        // debug_x(x);
        // debug_x(y);
        if (x <= -r || r <= x)
            continue;
        ans = min((ld)y - sqrt(r * r - x * x), ans);
    }
    cout << fixed << setprecision(10) << ans + r << endl;
    return 1;
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    while (true)
    {
        if (!solve())
            break;
    }

    return 0;
}

/*
shift+alt+Fで成形
f2で変数名変更

スニペット
basic
 m:atcoder以外用
 ma:ABC/ARC用
 mahc:AHC用
math
 floor:床関数
 ceil:天井関数
 comv:二項係数
 modpow: a^b mod m
 GCD:最大公約数
 extGCD:拡張ユークリッド互除法
 PFD:素因数分解
 isprime:エラトステネスの篩
 matrixpow:行列累乗
data_structure
 seg:セグメント木
technic
 Compress:座標圧縮
 runlength:ランレングス圧縮
graph
 warshall:ワーシャルフロイド法
 dfs:深さ優先探索
 bfs:幅優先探索
 LCA:最近共通祖先
*/
accuracy: 1
time_ms: 10
memory_kb: 3508
final_rank: 6

code_snippet: #include<bits/stdc++.h>
using namespace std;
const int MAX = 1000000005;

bool solve() {
    int r, n;
    cin >> r >> n;
    if (r == 0 && n == 0) return false;
    vector<tuple<int, int, int>> rect(n);
    for (int i = 0; i < n; i++) {
        int xl, xr, h;
        cin >> xl >> xr >> h;
        rect[i] = {xl, xr, h};
    }
    double ans = 1e+10;
    for (int i = 1; i < 100000; i++) {
        double x = r*cos(i*M_PI/100000), y = r*sin(i*M_PI/100000);
        int maxh = 0;
        for (int j = 0; j < n; j++) {
            auto [xl, xr, h] = rect[j];
            if (x >= (double)xl && x <= (double)xr) {
                maxh = max(maxh, h);
            }
        }
        ans = min(ans, r + maxh - y);
    }
    cout << fixed << setprecision(10) << ans << endl;

    return true;
}

int main() {
    ios::sync_with_stdio(0); cin.tie();
    while (solve()) {}
}
accuracy: 1
time_ms: 380
memory_kb: 3688
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vb = vector<bool>;
using vvb = vector<vb>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, s, n) for(int i = (int)(s); i < (int)(n); ++i)

ll INF = 1ll << 60;

long double solve(long double cy, int dr, int x){
    if(abs(x) == dr){
        return -1;
    }
    long double r = dr;
    long double x1 = x, x2 = x;
    long double cx = 0;
    long double y1 = 0;
    long double y2 = 1;
    long double xd = x2 - x1, yd = y2 - y1;
    long double X = x1 - cx, Y = y1 - cy;
    long double a = xd*xd + yd*yd;
    long double b = xd * X + yd * Y;
    long double c = X*X + Y*Y - r*r;
    // D = 0の時は1本で、D < 0の時は存在しない
    long double D = b*b - a*c;
    if(D <= 0){
        return -1;
    }
    long double s1 = (-b + sqrt(D)) / a;
    long double s2 = (-b - sqrt(D)) / a;
    return max(y1 + yd*s1, y1 + yd*s2);
}

int main(){
    vector<long double> ret;
    while(true){
        int r, n;
        cin >> r >> n;
        if(r == n && r == 0){
            break;
        }
        vi xl(n), xr(n), h(n);
        rep(i, 0, n){
            cin >> xl[i] >> xr[i] >> h[i];
        }
        map<pii, int> mx;
        rep(i, 0, n){
            rep(l, xl[i], xr[i]){
                if(!mx.count({l,l+1})){
                    mx[{l,l+1}] = h[i];
                }
                else{
                    mx[{l,l+1}] = max(mx[{l,l+1}], h[i]);
                }
            }
        }
        rep(i, -30, 30){
            if(!mx.count({i, i+1})){
                mx[{i,i+1}] = 0;
            }
            //cout << mx[{i,i+1}] << " ";
        }
        //cout << endl;

        long double ans = 0;
        bool ck = true;

        rep(time, 0, 300000){
            long double t = 1.0*time/10000;
            rep(x, -r-1, r+2){
                if(solve(t-r, r, x) > 0 || solve(t-r, r, x+1) > 0){
                    long double mxh = max(solve(t-r, r, x), solve(t-r, r, x+1));
                    if(mxh > mx[{x,x+1}]){
                        ck = false;
                        /*
                        cout << t << "/" << x << endl;
                        cout << solve(t-r, r, x) <<" " << solve(t-r, r, x+1) << endl;
                        */
                    }
                }
            }
            if(ck){
                ans = t;
            }
            else{
                break;
            }
        }
        ret.push_back(ans);
    }

    cout << fixed << setprecision(10);
    for(auto r : ret){
        cout << r << endl;
    }
    
    return 0;
    
}
accuracy: 1
time_ms: 7250
memory_kb: 3512
final_rank: 19

code_snippet: #include <bits/stdc++.h>
using namespace std;

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vb = vector<bool>;
using vvb = vector<vb>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, s, n) for(int i = (int)(s); i < (int)(n); ++i)

ll INF = 1ll << 60;

long double solve(long double cy, int dr, int x){
    if(abs(x) == dr){
        return -1;
    }
    long double r = dr;
    long double x1 = x, x2 = x;
    long double cx = 0;
    long double y1 = 0;
    long double y2 = 1;
    long double xd = x2 - x1, yd = y2 - y1;
    long double X = x1 - cx, Y = y1 - cy;
    long double a = xd*xd + yd*yd;
    long double b = xd * X + yd * Y;
    long double c = X*X + Y*Y - r*r;
    // D = 0の時は1本で、D < 0の時は存在しない
    long double D = b*b - a*c;
    if(D <= 0){
        return -1;
    }
    long double s1 = (-b + sqrt(D)) / a;
    long double s2 = (-b - sqrt(D)) / a;
    return max(y1 + yd*s1, y1 + yd*s2);
}

int main(){
    vector<long double> ret;
    while(true){
        int r, n;
        cin >> r >> n;
        if(r == n && r == 0){
            break;
        }
        vi xl(n), xr(n), h(n);
        rep(i, 0, n){
            cin >> xl[i] >> xr[i] >> h[i];
        }
        map<pii, int> mx;
        rep(i, 0, n){
            rep(l, xl[i], xr[i]){
                if(!mx.count({l,l+1})){
                    mx[{l,l+1}] = h[i];
                }
                else{
                    mx[{l,l+1}] = max(mx[{l,l+1}], h[i]);
                }
            }
        }
        rep(i, -30, 30){
            if(!mx.count({i, i+1})){
                mx[{i,i+1}] = 0;
            }
            //cout << mx[{i,i+1}] << " ";
        }
        //cout << endl;

        long double ans = 0;
        bool ck = true;

        rep(time, 1, 300000){
            long double t = 1.0*time/10000;
            long double  dx;
            if(t > 2*r){
                dx = r;
            }
            else{
                dx = sqrt(2*t*r-t*t);
            }
            rep(x, ceil(-dx-2), dx+3){
                if(solve(t-r, r, x) > 0 || solve(t-r, r, x+1) > 0){
                    long double mxh = max(solve(t-r, r, x), solve(t-r, r, x+1));
                    if(mxh > mx[{x,x+1}]){
                        ck = false;
                        /*
                        cout << t << "/" << x << endl;
                        cout << solve(t-r, r, x) <<" " << solve(t-r, r, x+1) << endl;
                        */
                    }
                }
            }
            if(ck){
                ans = t;
            }
            else{
                break;
            }
        }
        ret.push_back(ans);
    }

    cout << fixed << setprecision(10);
    for(auto r : ret){
        cout << r << endl;
    }
    
    return 0;
    
}
accuracy: 0.5
time_ms: 6750
memory_kb: 3404
final_rank: 20

code_snippet: #include <bits/stdc++.h>
using namespace std;
bool solve() {
  int n;
  double r;
  cin >> r >> n;
  if( abs(r) < 1e-3 ) return false;
  vector<pair<int, int>> event;
  for( int i = 0; i < n; i++ ) {
    int xl, xr, h;
    cin >> xl >> xr >> h;
    event.push_back(make_pair(xl, -h));
    event.push_back(make_pair(xr, h));
  }
  sort(event.begin(), event.end());
  vector<pair<int, int>> boundary;
  multiset<int> Sh;
  Sh.insert(0);
  boundary.push_back(make_pair(-40, 0));
  for( int i = 0; i < event.size(); i++ ) {
    int h_pre = *Sh.rbegin();
    auto [x, h] = event[i];
    if( h > 0 ) {
      Sh.erase(Sh.find(h));
    }else {
      Sh.insert(-h);
    }
    int h_cur = *Sh.rbegin();
    boundary.push_back(make_pair(x, h_pre));
    boundary.push_back(make_pair(x, h_cur));
  }
  boundary.push_back(make_pair(40, 0));
  double t = 0, step = 0.0001;
  auto dist = [&](double xl, double yl, double xr, double yr) -> double {
    if( xl > xr ) swap(xl, xr);
    if( yl > yr ) swap(yl, yr);
    if( xl == xr ) {
      double dy = max({0.0, yl-(-r+t), (-r+t)-yr});
      return hypot(xl, dy);
    }else {
      double dx = max({0.0, xl, -xr});
      return hypot(dx, yl-(-r+t));
    }
  };
  for( ; ; t+=step ) {
    bool is_covered = true;
    for( int i = 0; i < boundary.size()-1; i++ ) {
      auto [xl, yl] = boundary[i];
      auto [xr, yr] = boundary[i+1];
      if( dist(xl, yl, xr, yr) < r ) is_covered = false;
    }
    if( !is_covered ) break;
  }
  cout << fixed << setprecision(8) << t-step << endl;
  return true;
}
int main() {
  while( solve() ) {}
}
accuracy: 1
time_ms: 5690
memory_kb: 3472
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;

bool solve() {
    int r, n;
    cin >> r >> n;
    if (r == 0 && n == 0) return false;
    set<pair<int, int>> s;
    for (int i = 0; i < n; i++) {
        int l, r, h;
        cin >> l >> r >> h;
        for (int j = l; j < r; j++) {
            for (int k = 0; k < h; k++) {
                s.emplace(j, k);
            }
        }
    }
    long double ans = 1e18;
    for (int x = -r + 1; x < r; x++) {
        for (int y = 0; y <= 30; y++) {
            long double dy = y - (sqrtl(r * r - x * x) - r);
            if (s.count(make_pair(x, y)) == 0 || s.count(make_pair(x - 1, y)) == 0) {
                ans = std::min(ans, dy);
            }
        }
    }
    cout << fixed << setprecision(10); 
    cout << ans << '\n';
    return true;
}

int main() {
    while (solve()) ;    
}
accuracy: 1
time_ms: 10
memory_kb: 3424
final_rank: 2

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
using ull = unsigned long long;
typedef pair<ll,ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;
template<class T> using pqmin = priority_queue<T, vector<T>, greater<T>>;
template<class T> using pqmax = priority_queue<T>;
const ll inf=LLONG_MAX/3;
const ll dx[] = {0, 1, 0, -1, 1, -1, 1, -1};
const ll dy[] = {1, 0, -1, 0, 1, 1, -1, -1};
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define fi first
#define se secondf
#define all(x) x.begin(),x.end()
#define si(x) ll(x.size())
#define rep(i,n) for(ll i=0;i<n;i++)
#define per(i,n) for(ll i=n-1;i>=0;i--)
#define rng(i,l,r) for(ll i=l;i<r;i++)
#define gnr(i,l,r) for(ll i=r-1;i>=l;i--)
#define fore(i, a) for(auto &&i : a)
#define fore2(a, b, v) for(auto &&[a, b] : v)
#define fore3(a, b, c, v) for(auto &&[a, b, c] : v)
template<class T> bool chmin(T& a, const T& b){ if(a <= b) return 0; a = b; return 1; }
template<class T> bool chmax(T& a, const T& b){ if(a >= b) return 0; a = b; return 1; }
template<class T, class U> bool chmin(T& a, const U& b){ return chmin(a, (T)b); }
template<class T, class U> bool chmax(T& a, const U& b){ return chmax(a, (T)b); }
#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)
#define STR(...) string __VA_ARGS__;in(__VA_ARGS__)
#define CHR(...) char __VA_ARGS__;in(__VA_ARGS__)
#define vec(type,name,...) vector<type>name(__VA_ARGS__)
#define VEC(type,name,size) vector<type>name(size);in(name)
#define VLL(name,size) vector<ll>name(size);in(name)
#define vv(type,name,h,...) vector<vector<type>> name(h,vector<type>(__VA_ARGS__))
#define VV(type,name,h,w) vector<vector<type>> name(h,vector<type>(w));in(name)
#define vvv(type,name,h,w,...) vector<vector<vector<type>>> name(h,vector<vector<type>>(w,vector<type>(__VA_ARGS__)))
#define SUM(...) accumulate(all(__VA_ARGS__),0LL)
template<class T> auto min(const T& a){ return *min_element(all(a)); }
template<class T> auto max(const T& a){ return *max_element(all(a)); }
template<class T, class F = less<>> void sor(T& a, F b = F{}){ sort(all(a), b); }
template<class T> void uniq(T& a){ sor(a); a.erase(unique(all(a)), end(a)); }
void outb(bool x){cout<<(x?"Yes":"No")<<"\n";}
ll max(int x, ll y) { return max((ll)x, y); }
ll max(ll x, int y) { return max(x, (ll)y); }
int min(int x, ll y) { return min((ll)x, y); }
int min(ll x, int y) { return min(x, (ll)y); }
#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define lbg(c, x) distance((c).begin(), lower_bound(all(c), (x), greater{}))
#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))
#define ubg(c, x) distance((c).begin(), upper_bound(all(c), (x), greater{}))
ll gcd(ll a,ll b){return (b?gcd(b,a%b):a);}
vector<pll> factor(ull x){ vector<pll> ans; for(ull i = 2; i * i <= x; i++) if(x % i == 0){ ans.push_back({i, 1}); while((x /= i) % i == 0) ans.back().second++; } if(x != 1) ans.push_back({x, 1}); return ans; }
vector<ll> divisor(ull x){ vector<ll> ans; for(ull i = 1; i * i <= x; i++) if(x % i == 0) ans.push_back(i); per(i,ans.size() - (ans.back() * ans.back() == x)) ans.push_back(x / ans[i]); return ans; }
vll prime_table(ll n){vec(ll,isp,n+1,1);vll res;rng(i,2,n+1)if(isp[i]){res.pb(i);for(ll j=i*i;j<=n;j+=i)isp[j]=0;}return res;}

template <class T, class S> pair<T, S> operator-(const pair<T, S> &x) { return pair<T, S>(-x.first, -x.second); }
template <class T, class S> pair<T, S> operator-(const pair<T, S> &x, const pair<T, S> &y) { return pair<T, S>(x.fi - y.fi, x.se - y.se); }
template <class T, class S> pair<T, S> operator+(const pair<T, S> &x, const pair<T, S> &y) { return pair<T, S>(x.fi + y.fi, x.se + y.se); }
template <class T> pair<T, T> operator&(const pair<T, T> &l, const pair<T, T> &r) { return pair<T, T>(max(l.fi, r.fi), min(l.se, r.se)); }
template <class T, class S> pair<T, S> operator+=(pair<T, S> &l, const pair<T, S> &r) { return l = l + r; }
template <class T, class S> pair<T, S> operator-=(pair<T, S> &l, const pair<T, S> &r) { return l = l - r; }
template <class T> bool intersect(const pair<T, T> &l, const pair<T, T> &r) { return (l.se < r.se ? r.fi < l.se : l.fi < r.se); }

template <class T> vector<T> &operator++(vector<T> &v) {
        fore(e, v) e++;
        return v;
}
template <class T> vector<T> operator++(vector<T> &v, int) {
        auto res = v;
        fore(e, v) e++;
        return res;
}
template <class T> vector<T> &operator--(vector<T> &v) {
        fore(e, v) e--;
        return v;
}
template <class T> vector<T> operator--(vector<T> &v, int) {
        auto res = v;
        fore(e, v) e--;
        return res;
}
template <class T> vector<T> &operator+=(vector<T> &l, const vector<T> &r) {
        fore(e, r) l.eb(e);
        return l;
}

template<class... Ts> void in(Ts&... t);
[[maybe_unused]] void print(){}
template<class T, class... Ts> void print(const T& t, const Ts&... ts);
template<class... Ts> void out(const Ts&... ts){ print(ts...); cout << '\n'; }
namespace IO{
#define VOID(a) decltype(void(a))
struct S{ S(){ cin.tie(nullptr)->sync_with_stdio(0); fixed(cout).precision(12); } }S;
template<int I> struct P : P<I-1>{};
template<> struct P<0>{};
template<class T> void i(T& t){ i(t, P<3>{}); }
void i(vector<bool>::reference t, P<3>){ int a; i(a); t = a; }
template<class T> auto i(T& t, P<2>) -> VOID(cin >> t){ cin >> t; }
template<class T> auto i(T& t, P<1>) -> VOID(begin(t)){ for(auto&& x : t) i(x); }
template<class T, size_t... idx> void ituple(T& t, index_sequence<idx...>){ in(get<idx>(t)...); }
template<class T> auto i(T& t, P<0>) -> VOID(tuple_size<T>{}){ ituple(t, make_index_sequence<tuple_size<T>::value>{}); }
template<class T> void o(const T& t){ o(t, P<4>{}); }
template<size_t N> void o(const char (&t)[N], P<4>){ cout << t; }
template<class T, size_t N> void o(const T (&t)[N], P<3>){ o(t[0]); for(size_t i = 1; i < N; i++){ o(' '); o(t[i]); } }
template<class T> auto o(const T& t, P<2>) -> VOID(cout << t){ cout << t; }
template<class T> auto o(const T& t, P<1>) -> VOID(begin(t)){ bool first = 1; for(auto&& x : t) { if(first) first = 0; else o(' '); o(x); } }
template<class T, size_t... idx> void otuple(const T& t, index_sequence<idx...>){ print(get<idx>(t)...); }
template<class T> auto o(T& t, P<0>) -> VOID(tuple_size<T>{}){ otuple(t, make_index_sequence<tuple_size<T>::value>{}); }
#undef VOID
}
#define unpack(a) (void)initializer_list<int>{(a, 0)...}
template<class... Ts> void in(Ts&... t){ unpack(IO::i(t)); }
template<class T, class... Ts> void print(const T& t, const Ts&... ts){ IO::o(t); unpack(IO::o((cout << ' ', ts))); }
#undef unpack

int main(){
    cin.tie(0);
    ios::sync_with_stdio(0);
    while(1){
        LL(r,n);
        if(n==0&&r==0)break;
        map<pll,bool> col;
        rep(i,n){
            LL(xl,xr,h);
            rng(x,xl,xr)rng(y,0,h){
                col[{x,y}]=1;
            }
        }
        auto inc=[&](double p,double x,double y){
            double d=pow(x,2)+pow(y-p,2);
            d=sqrt(d);
            return d<r;
        };
        auto f=[&](double t){
            double p=t-r;
            bool res=1;
            rng(x,-20,20)rng(y,0,20){
                bool ex=0;
                rng(dx,0,2)rng(dy,0,2){
                    ex|=inc(p,x+dx,y+dy);
                }
                if(ex){
                    res&=col[{x,y}];
                }
            }
            return res;
        };
        {
        double l=0,r=20;
        while(l<r-0.0001){
            double mid=(l+r)/2.0;
            (f(mid)?l:r)=mid;
        }
        cout<<fixed<<setprecision(4);
        out(l);
        }
    }
}
accuracy: 1
time_ms: 30
memory_kb: 3548
final_rank: 8

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <deque>
#include <queue>
#include <string>
#include <iomanip>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <utility>
#include <stack>
#include <random>
#include <complex>
#include <functional>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <assert.h>
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#endif
#if __has_include(<boost/multiprecision/cpp_int.hpp>)
#include <boost/multiprecision/cpp_int.hpp>
using cpp_int=boost::multiprecision::cpp_int;
#endif
#if __has_include(<boost/multiprecision/cpp_dec_float.hpp>)
#include <boost/multiprecision/cpp_dec_float.hpp>
template<unsigned size>using cpp_float=boost::multiprecision::number<boost::multiprecision::cpp_dec_float<size>>;
template<unsigned size>using cpp_double=boost::multiprecision::number<boost::multiprecision::cpp_dec_float<size,long long>>;
#endif
using namespace std;
using ll=long long;
#define read(x) cin>>(x);
#define readll(x) ll (x);cin>>(x);
#define readS(x) string (x);cin>>(x);
#define readvll(x,N) vector<ll> (x)((N));for(int i=0;i<(N);i++){cin>>(x)[i];}
#define rep(i,N) for(ll (i)=0;(i)<(N);(i)++)
#define rep2d(i,j,H,W) for(ll (i)=0;(i)<(H);(i)++)for(ll (j)=0;(j)<(W);j++)
#define is_in(x,y) (0<=(x) && (x)<H && 0<=(y) && (y)<W)
#define yn {cout<<"Yes"<<endl;}else{cout<<"No"<<endl;}
#define double_out(x) fixed << setprecision(x)
template<class T> inline void erase_duplicate(vector<T>& A){sort(A.begin(),A.end());A.erase(unique(A.begin(),A.end()),A.end());}
inline ll powll(ll x,ll n){ll r=1;while(n>0){if(n&1){r*=x;};x*=x;n>>=1;};return r;}

void solve(ll r,ll n){
  vector<ll> xl(n),xr(n),h(n);
  for(ll i=0;i<n;i++){
    cin>>xl[i]>>xr[i]>>h[i];
  }
  // vector<ll> hmax(41);
  // for(ll i=0;i<n;i++){
  //   for(ll j=xl[i]+20;j<xr[i]+20;j++){
  //     hmax[j]=max(hmax[j],h[i]);
  //   }
  // }
  // double ans=100000;
  // for(ll j=0;j<40;j++){
  //   ll x1=j-20;
  //   ll x2=j-20+1;
  //   ll hh=hmax[j];
  //   if(abs(x1)>abs(x2)){
  //     swap(x1,x2);
  //     hh=hmax[j+1];
  //   }
  //   if(abs(x1)>r || (abs(x1)==r && hh==0)){
  //     continue;
  //   }
  //   ans=min(ans,hh-sqrt(r*r-x1*x1)+r);
  //   cerr<<x1<<" "<<hh<<" "<<ans<<endl;
  // }
  double ans=1000000;
  for(double x=-20.0;x<=20.0;x+=0.00001){
    if(abs(x)>r){
      continue;
    }
    ll hh=0;
    for(ll i=0;i<n;i++){
      if(xl[i]<=x && x<=xr[i]){
        hh=max(hh,h[i]);
      }
    }
    ans=min(ans,hh-sqrt(r*r-x*x)+r);
  }
  cout<<double_out(10)<<ans<<endl;
}

int main(){
  ll r,n;
  while(cin>>r>>n,r){
    solve(r,n);
  }
}
accuracy: 1
time_ms: 2960
memory_kb: 3568
final_rank: 11

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll=long long;

void solve(){
    while(1){
        int r,n;
        cin>>r>>n;
        if(r==0&&n==0) break;
        map<pair<double,double>,bool> mp;
        for(int i=0;i<n;i++){
            int xl,xr,h;
            cin>>xl>>xr>>h;
            for(double x=xl;x<=xr;x+=0.5){
                for(int y=0;y<=h;y++){
                    mp[{x,y}]=true;
                }
            }
        }
        long double ans=1000000;
        for(int x=-20;x<=20;x++){
            for(int y=1;y<=20;y++){
                if(!mp[{x+0.5,y+1}]||!mp[{x-0.5,y+1}]){
                    if(r*r-x*x>0){
                        ans=min(ans,(long double)y+r-sqrt(r*r-x*x));
                    }
                }
            }
        }
        for(int x=-20;x<=20;x++){
            if(!mp[{x-0.5,0}]||!mp[{x+0.5,0}]){
                if(r*r-x*x>0){
                    ans=min(ans,(long double)r-sqrt(r*r-x*x));
                }
            }
        }
        cout<<fixed<<setprecision(6)<<ans<<endl;
    }
}
int main(){
    solve();
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3504
final_rank: 5

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <deque>
#include <queue>
#include <string>
#include <iomanip>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <utility>
#include <stack>
#include <random>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <assert.h>
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#endif
using namespace std;
using ll=long long;
#define read(x) cin>>(x);
#define readll(x) ll (x);cin>>(x);
#define readS(x) string (x);cin>>(x);
#define readvll(x,N) vector<ll> (x)((N));for(int i=0;i<(N);i++){cin>>(x)[i];}
#define rep(i,N) for(ll (i)=0;(i)<(N);(i)++)
#define rep2d(i,j,H,W) for(ll (i)=0;(i)<(H);(i)++)for(ll (j)=0;(j)<(W);j++)
#define is_in(x,y) (0<=(x) && (x)<H && 0<=(y) && (y)<W)
#define yn {cout<<"Yes"<<endl;}else{cout<<"No"<<endl;}

void solve(ll n,ll r){
  vector<ll> X1(n),X2(n),H(n);
  for(ll i=0;i<n;i++){
    cin>>X1[i]>>X2[i]>>H[i];
  }
  double ans=40.0;
  for(ll i=-r;i<r;i++){
    ll u=0;
    for(ll j=0;j<n;j++){
      if(X1[j]<=i && i<X2[j]){
        //cerr<<j<<endl;
        u=max(u,H[j]);
      }
    }
    //cerr<<"i: "<<i<<" u:"<<u<<endl;
    for(double j=0;j<1.0;j+=0.0001){
      double x=i+j;
      double t=(double)u-sqrt(r*r-x*x);
      ans=min(ans,(double)r+t);
    }
  }
  cout<<ans<<endl;
}

int main(){
  ll n;
  ll r;
  while(1){
    cin>>r>>n;
    if(n==0 && r==0){
      return 0;
    }
    solve(n,r);
  }
}
accuracy: 1
time_ms: 40
memory_kb: 3448
final_rank: 3

code_snippet: #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>

using namespace std;
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;

const long double EPS = 1e-4;
int r, n;

int main() {
    cin >> r >> n;
    cout << fixed << setprecision(6);
    while (r != 0 || n != 0) {
        VV<int> shadow(41, V<int>(41));
        V<int> xls(n), xrs(n), hs(n);
        for (int i = 0; i < n; i++) {
            cin >> xls[i] >> xrs[i] >> hs[i];
            for (int x = xls[i] + 20; x < xrs[i] + 20; x++) {
                for (int y = 0; y < hs[i]; y++) {
                    shadow[y][x] = 1;
                }
            }
        }
        // for (auto &&line : shadow) {
        //     for (auto &&s : line) {
        //         cout << s << " ";
        //     }
        //     cout << "\n";
        // }

        // 0.0005 ずつ yを上昇させてシミュレーション
        double t = 0.0;

        for (;;) {
            double new_t = t + 0.0005;
            bool ok = true;
            for (int i = -r; i <= r; i++) {
                int x = i;
                int y = floor(sqrt(r * r - x * x) - r + new_t);
                if (y < 0) continue;
                if (i == -r) {
                    if (!shadow[y][x + 20]) ok = false;
                } else if (i == r) {
                    if (!shadow[y][x + 20 - 1]) ok = false;
                } else if (!shadow[y][x + 20 - 1] || !shadow[y][x + 20]) {
                    ok = false;
                }
            }
            if (!ok) break;
            t = new_t;
        }
        cout << t << "\n";
        cin >> r >> n;
    }
}
accuracy: 1
time_ms: 400
memory_kb: 3584
final_rank: 9

code_snippet: #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>

using namespace std;
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;

const long double EPS = 1e-4;
int r, n;

int main() {
    cin >> r >> n;
    cout << fixed << setprecision(6);
    while (r != 0 || n != 0) {
        VV<int> shadow(41, V<int>(41));
        V<int> xls(n), xrs(n), hs(n);
        for (int i = 0; i < n; i++) {
            cin >> xls[i] >> xrs[i] >> hs[i];
            for (int x = xls[i] + 20; x < xrs[i] + 20; x++) {
                for (int y = 0; y < hs[i]; y++) {
                    shadow[y][x] = 1;
                }
            }
        }
        // for (auto &&line : shadow) {
        //     for (auto &&s : line) {
        //         cout << s << " ";
        //     }
        //     cout << "\n";
        // }

        // 0.0005 ずつ yを上昇させてシミュレーション
        double t = 0.0;

        for (;;) {
            double new_t = t + 0.00005;
            bool ok = true;
            for (int i = -r; i <= r; i++) {
                int x = i;
                int y = floor(sqrt(r * r - x * x) - r + new_t);
                if (y < 0) continue;
                if (i == -r) {
                    if (!shadow[y][x + 20]) ok = false;
                } else if (i == r) {
                    if (!shadow[y][x + 20 - 1]) ok = false;
                } else if (!shadow[y][x + 20 - 1] || !shadow[y][x + 20]) {
                    ok = false;
                }
            }
            if (!ok) break;
            t = new_t;
        }
        cout << t << "\n";
        cin >> r >> n;
    }
}
accuracy: 1
time_ms: 4010
memory_kb: 3504
final_rank: 13

code_snippet: #include <bits/stdc++.h>

using namespace std;
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;

const long double EPS = 1e-4;
int r, n;

int main() {
    cin >> r >> n;
    cout << fixed << setprecision(6);
    while (r != 0 || n != 0) {
        VV<int> shadow(41, V<int>(41));
        V<int> xls(n), xrs(n), hs(n);
        for (int i = 0; i < n; i++) {
            cin >> xls[i] >> xrs[i] >> hs[i];
            for (int x = xls[i] + 20; x < xrs[i] + 20; x++) {
                for (int y = 0; y < hs[i]; y++) {
                    shadow[y][x] = 1;
                }
            }
        }
        // for (auto &&line : shadow) {
        //     for (auto &&s : line) {
        //         cout << s << " ";
        //     }
        //     cout << "\n";
        // }

        // 0.0005 ずつ yを上昇させてシミュレーション
        double t = 0.0;

        for (;;) {
            double new_t = t + 0.00005;
            bool ok = true;
            for (int i = -r; i <= r; i++) {
                int x = i;
                int y = floor(sqrt(r * r - x * x) - r + new_t);
                if (y < 0) continue;
                if (i == -r) {
                    if (!shadow[y][x + 20]) ok = false;
                } else if (i == r) {
                    if (!shadow[y][x + 20 - 1]) ok = false;
                } else if (!shadow[y][x + 20 - 1] || !shadow[y][x + 20]) {
                    ok = false;
                }
            }
            if (!ok) break;
            t = new_t;
        }
        cout << t << "\n";
        cin >> r >> n;
    }
}
accuracy: 1
time_ms: 4900
memory_kb: 3516
final_rank: 15

code_snippet: #include <bits/stdc++.h>
using namespace std;


template<typename F>
pair<double,double> Binary(double ok, double ng, const F &isok, int m = 200){
  for(int t = 0; t < m; t++){
    double wj = ((ok - ng)/2) + ng;
    (isok(wj)?ok : ng) = wj;
  }
  return {ok, ng};
}

namespace geometry{
  constexpr double eps = 1e-9;
  bool eq(double a, double b){ return fabs(a - b) < eps; }
  template<typename T>  bool eq(T a, T b){ return a == b; }
  bool is_zero(double a){ return fabs(a) < eps; }
  template<typename T> bool is_zero(T a){ return a == 0; }
  bool le(double a, double b){ return a <= b + eps; }
  template<typename T> bool le(T a, T b){ return a <= b; }
  bool lt(double a, double b){ return a < b - eps; }
  template<typename T> bool lt(T a, T b){ return a < b; }
};


template<class T> struct Point{
  T x,y;
  Point(){}
  Point(T x, T y) : x(x), y(y) {}
  Point(const pair<T,T> &p) : x(p.first), y(p.second) {}

  Point operator*(const T b) const { return Point(x * b, y * b); }
  Point operator/(const T b) const { return Point(x / b, y / b); }
  Point operator+(const Point &b) const { return Point(x + b.x, y + b.y); }
  Point operator-(const Point &b) const { return Point(x - b.x, y - b.y); }
  Point operator*(const Point &b) const { return Point(x*b.x - y*b.y, x*b.y + y*b.x); }
  Point operator/(const Point &b) const { return Point(x*b.x + y*b.y, y*b.x - x*b.y)/(b.x*b.x + b.y*b.y); }

  Point &operator*=(const T b){ return (*this) = (*this) * b; }
  Point &operator/=(const T b){ return (*this) = (*this) / b; }
  Point &operator+=(const Point &b){ return (*this) = (*this) + b; }
  Point &operator-=(const Point &b){ return (*this) = (*this) - b; }
  Point &operator*=(const Point &b){ return (*this) = (*this) * b; }
  Point &operator/=(const Point &b){ return (*this) = (*this) / b; }
  
  bool operator==(const Point &b) const {
    return geometry::eq(x, b.x) && geometry::eq(y, b.y);
  }
  bool operator<(const Point &b) const { 
    if(geometry::eq(x, b.x)) return y < b.y;
    return x < b.x;
  }
  T Norm() const { return x * x + y * y; }
  double Abs() const { return hypot<double>(x, y); }
  double dist(const Point &b) const { return hypot<double>(x - b.x, y - b.y); }
  double arg() const { return atan2<double>(y, x); }
  
  Point ArgVec() const {
    if((*this) == Point(0, 0)) return (*this);
    if(geometry::is_zero(x)) return Point(0, -1);
    return (*this) / gcd(x, y) * (x < 0 ? -1 : 1);
  }

  int ort() const {
    if(geometry::is_zero(x) && geometry::is_zero(y)) return 0;
    if(geometry::is_zero(y)) return x > 0 ? 1 : 3;
    if(geometry::is_zero(x)) return y > 0 ? 2 : 4;
    if (y > 0) return x > 0 ? 1 : 2;
    else return x > 0 ? 4 : 3;
  }
  Point rotate90() const { return Point(-y, x);}
  Point rotate180() const { return Point(-x, -y);}
  Point rotate270() const { return Point(y, -x);}

  Point flip_x(T p = 0) const { return Point(p*2 - x, y);}
  Point flip_y(T p = 0) const { return Point(x, p*2 - y);}

  Point rotate(double theta) const {
    return Point(cos(theta)*x - sin(theta)*y, sin(theta)*x + cos(theta)*y);
  }

  Point flip(double theta) const {
    return (*this).rotate(-theta).flip_y().rotate(theta);
  }

  friend ostream &operator<<(ostream &os, const Point &p) {
    return os << p.x << " " << p.y;
  }
  friend istream &operator>>(istream &is, Point &p) {
    T a, b; is >> a >> b;
    p = Point<T>(a, b);
    return (is);
  }
};

template<typename T> Point<T> Polar(const T& rho, const T& theta = 0){
  return Point<T>(rho * cos(theta), rho * sin(theta));
}


template<typename T> using Polygon = vector<Point<T>>;

template<class T> T Cross(const Point<T> &a, const Point<T> &b) { return a.x * b.y - a.y * b.x; }

template<class T> T Dot(const Point<T> &a, const Point<T> &b) { return a.x * b.x + a.y * b.y; }

template<class T> bool Intersect(const Point<T> &a, const Point<T> &b, const Point<T> &c, const Point<T> &d) {
  return (iSP(a, b, c)*iSP(a, b, d) <= 0) && (iSP(c, d, a)*iSP(c, d, b) <= 0);
}

template<class T> int iSP(const Point<T> &a, const Point<T> &b, const Point<T> &c){
  T fl = Cross(b-a, c-a);
  if(geometry::lt(T(0), fl)) return 1; // CCW
  if(geometry::lt(fl, T(0))) return -1; // CW
  if(geometry::lt(T(0), Dot(b-a, c-b))) return 2; //abc
  if(geometry::lt(T(0), Dot(a-b, c-a))) return -2; //bac
  return 0; // acb
}

template<class T> int angletype(const Point<T> &a, const Point<T> &b, const Point<T> &c){
  T v = Dot(a-b, c-a);
  if(geometry::is_zero(v)) return 0; // right angle
  if(v > 0) return 1; // acute angle
  return -1; // obtuse angle
}

template<typename T>
bool cmp_y(const Point<T>& p1, const Point<T> &p2){
  if(p1.y == p2.y) return p1.x < p2.x;
  return p1.y < p2.y;
}

template<typename T>
bool cmp_arg(const Point<T>& p1, const Point<T> &p2){
  if(p1.ort() != p2.ort()) return p1.ort() < p2.ort();
  return iSP(Point<T>(0,0), p1, p2) > 0;
}

template<typename T>
void ArgSort(Polygon<T> &ps){
  sort(begin(ps), end(ps), cmp_arg<T>);
}

template<class T> struct Line{
  Point<T> a, b;
  Line() {}
  Line(const Point<T> &a, const Point<T> &b): a(a), b(b) {}
  Line(T A, T B, T C){ // Ax + By = C
    if(A == 0){ a = Point<T>(0, C/B); b = Point<T>(1, C/B); }
    else if(B == 0){ a = Point<T>(C/A, 0); b = Point<T>(C/A, 1); }
    else{ a = Point<T>(0, C/B); b = Point<T>(C/A, 0); }
  }
  //Line(const Segment<T> &s): a(s.a), b(s.b) {}
  Point<T> projection(const Point<T> &p) const {
    return a + (b - a)*Dot(p - a, b - a)/(b - a).Norm();
  }
  Point<T> reflection(const Point<T> &p) const {
    return p + (projection(p) - p)*2;
  }
  int angletype(const Line &l) const {
    if(geometry::is_zero(Cross(b-a, l.b-l.a))) return 1;
    if(geometry::is_zero(Dot(a-b, l.b-l.a))) return -1;
    return 0;
  }
  bool Intersect(const Line &l) const {
    return angletype(l) != 1;
  }
  Point<T> Cross_Point(const Line &l) const {
    if(geometry::is_zero(Cross(b-a,l.b-l.a))) return l.a;
    return (b - a)*Cross(l.a - a, l.b - l.a)/Cross(b - a,l.b - l.a) + a;
  }
  double dist(const Point<T> &p) const {
    return abs(Cross(p - a, b - a)/(b - a).Abs());
  }
  double dist(const Line &l) const {
    if(Intersect(l)) return 0;
    return dist(l.a);
  }
};

template<class T> struct Segment{
  Point<T> a, b;
  Segment() {}
  Segment(const Point<T> &a, const Point<T> &b): a(a), b(b) {}

  double length() const { return a.dist(b); }
  bool Intersect(const Segment &s) const {
    return iSP(a, b, s.a)*iSP(a, b, s.b) <= 0 && iSP(s.a, s.b, a)*iSP(s.a, s.b, b) <= 0;
  }
  Point<T> Center() const { return (a + b) / 2; }
  Line<T> PerpBisector() const {
    Point<T> center = this->Center();
    return Line<T>((a - center).rotate90() + center, center);
  }
  int angletype(const Segment &s) const {
    if(geometry::is_zero(Cross(b-a, s.b-s.a))) return 1;
    if(geometry::is_zero(Dot(a-b, s.b-s.a))) return -1;
    return 0;
  }
  Point<T> Cross_Point(const Segment &s) const {
    if(geometry::is_zero(Cross(b-a,s.b-s.a))) return s.a;
    return (b - a)*Cross(s.a - a, s.b - s.a)/Cross(b - a, s.b - s.a) + a;
  }
  double dist(const Point<T> &p) const {
    if(Dot(b-a, p-a) < 0) return p.dist(a);
    if(Dot(a-b, p-b) < 0) return p.dist(b);
    return abs(Cross(p-a, b-a)/ (b-a).Abs());
  }
  double dist(const Segment &s) const {
    if(Intersect(s)) return 0;
    return min({dist(s.a), dist(s.b), s.dist(a), s.dist(b)});
  }
};

template<typename T>
struct Triangle{
  Point<T> a, b, c;
  Triangle() {}
  Triangle(const Point<T> &a, const Point<T> &b, const Point<T> &c): a(a), b(b), c(c){}

  double angle_a() const {
    double ab = a.dist(b), bc = b.dist(c), ca = c.dist(a);
    return acos((ca*ca + ab*ab - bc*bc)/(2*ca*ab));
  }
  double angle_b() const {
    double ab = a.dist(b), bc = b.dist(c), ca = c.dist(a);
    return acos((ab*ab + bc*bc - ca*ca)/(2*ab*bc));
  }
  double angle_c() const {
    double ab = a.dist(b), bc = b.dist(c), ca = c.dist(a);
    return acos((bc*bc + ca*ca - ab*ab)/(2*bc*ca));
  }
  Point<T> divpoint(double p, double q, double r) const {
    return (a*p + b*q + c*r)/(p + q + r);
  }

  Point<T> centerofgravity() const {
    return (a + b + c)/3;
  }
  Point<T> circumcenter() const {
    Segment<T> s1(a, b), s2(a, c);
    return s1.PerpBisector().Cross_Point(s2.PerpBisector());
  }
  Point<T> orthocenter() const {
    return centerofgravity()*3 - circumcenter()*2;
  }
  Point<T> innercenter() const {
    return divpoint(b.dist(c), c.dist(a), a.dist(b));
  }
  Point<T> excenterA() const {
    return divpoint(-b.dist(c), c.dist(a), a.dist(b));
  }
  Point<T> excenterB() const {
    return divpoint(b.dist(c), -c.dist(a), a.dist(b));
  }
  Point<T> excenterC() const {
    return divpoint(b.dist(c), c.dist(a), -a.dist(b));
  }
};

template<class T> struct Circle{
  Point<T> o;
  T r;
  Circle() {}
  Circle(const Point<T> &o, const T &r): o(o), r(r) {}
  Circle(const Point<T> &a, const Point<T> &b): o((a + b)/2), r(a.dist(b)/2) {}
  Circle(const Point<T> &a, const Point<T> &b, const Point<T> &c)
    : o(Triangle(a,b,c).circumcenter()), r(o.dist(a)) {}

  int Intersect(const Circle&c) const {
    if(r < c.r) return c.Intersect(*this);
    auto od = (o - c.o).Norm();
    auto ra = (r + c.r)*(r + c.r), rs = (r - c.r)*(r - c.r);
    if(geometry::lt(ra, od)) return 4;
    if(geometry::eq(ra, od)) return 3;
    if(geometry::lt(rs, od)) return 2;
    if(geometry::eq(rs, od)) return 1;
    return 0;
  }

  int Intersect(const Line<T> &l) const {
    double d = l.dist(o);
    if(geometry::lt<double>(d, r)) return 2;
    return geometry::eq(d, r);
  }

  int Contains(const Point<T> &p) const {
    auto od = (o - p).Norm();
    if(geometry::eq(od, r*r)) return 1; // on
    return (od > r*r) ? 2 : 0; // out/in
  }

  Polygon<T> Cross_Points(const Line<T> &l) const {
    int k = this->Intersect(l);
    if(k == 0) return {};
    Point<T> p = l.projection(o);
    if(k == 1) return {p};
    Point<T> d = (l.b - l.a)/l.a.dist(l.b);
    double base = sqrt(r*r - (p - o).Norm());
    return {p + d*base, p - d*base};
  }

  Polygon<T> Cross_Points(const Circle &c) const {
    int k = this->Intersect(c);
    if(k == 0 || k == 4) return {};
    double t = (c.o - o).arg();
    if(k == 1 || k == 3) return {o + Polar<T>(r, t)};
    double d = o.dist(c.o), a = acos((r*r + d*d - c.r*c.r) / (r * d * 2));
    return {o + Polar<T>(r, t + a), o + Polar<T>(r, t - a)};
  }

  friend ostream &operator<<(ostream &os, const Circle &c) {
    return os << c.o << " " << c.r;
  }
  friend istream &operator>>(istream &is, Circle &c) {
    Point<T> p; T l; is >> p >> l;
    c = Circle<T>(p, l);
    return (is);
  }
};



int main() {
  cout << fixed << setprecision(12);
  
  while(true){
    int r, n; cin >> r >> n;
    if(r+n == 0) return 0;

    int m = 40;

    vector<int> h(m);

    for(int i = 0; i < n; ++i){
      int l, r, y; cin >> l >> r >> y;
      for(int j = l; j < r; ++j) h[j+20] = max(h[j+20], y);
    }

    auto f = [&](double t){
      Point<double> c(0, t-r);
      Circle<double> sun(c, r);

      for(int i = 0; i < m-1; ++i){
        double y = min(h[i], h[i+1]), my = max(h[i], h[i+1]);
        double x = i - 19;
        /*
        中心が (0, t-r) のとき, x = i-19での高さ
        //*/
        if(i-19 == r && t-r <= h[i]) continue;
        if(i-19 == -r && t-r <= h[i+1]) continue;

        Line<double> l(1, 0, i-19);
        auto ps = sun.Cross_Points(l);
        for(auto p : ps){
          if(p.y > y) return false;
        }
      }
      return true;
    };

    auto[ok,ng] = Binary(0, 40, f);
    cout << ok << "\n";
  }
}
accuracy: 1
time_ms: 20
memory_kb: 3480
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;


int main() {
  cout << fixed << setprecision(12);
  
  while(true){
    int r, n; cin >> r >> n;
    if(r+n == 0) return 0;

    int m = 40;

    vector<int> h(m);

    for(int i = 0; i < n; ++i){
      int l, r, y; cin >> l >> r >> y;
      for(int j = l; j < r; ++j) h[j+20] = max(h[j+20], y);
    }
    bool fl = true;
    double t = 0;
    while(fl){
      t += 0.0001;
      for(int i = 0; i < m-1; ++i){
        int y = min(h[i], h[i+1]);
        int x = i - 19;

        if(hypot(x, y+r-t) < r) fl = false;
      }
      
    }
    t -= -0.0001;
    cout << t << "\n";
  }
}
accuracy: 1
time_ms: 3940
memory_kb: 3556
final_rank: 14

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

double p(double a) { return a * a; }

bool solve() {
    int rad, n;
    cin >> rad >> n;
    if(rad + n == 0) return false;

    map<int, int> mp;
    for(int x = -30; x < 30; ++x) mp[x] = rad;
    rep(_, n) {
        int l, r, h;
        cin >> l >> r >> h;
        for(int i = l; i < r; ++i) {
            mp[i] = max(mp[i], h + rad);
        }
    }

    double t = 0, d = 0.0001;
    for(bool ng = false; ng == false && t < 30; t += d) {
        for(auto &[x, h] : mp) if(p(x) + p(t - h) < p(rad)) ng = true;
        for(auto &[x, h] : mp) if(p(x + 1) + p(t - h) < p(rad)) ng = true;
    }
    cout << fixed << setprecision(10) << t - d << '\n';
    return true;
}

int main() {
    while(solve());
    return 0;
}
accuracy: 1
time_ms: 5050
memory_kb: 3576
final_rank: 17

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for(int64 i = 0;i < (n);i++)
#define FOR(i, a, b) for(int64 i = (a);i < (b);i++)
#define all(x) x.begin(),x.end()
#define fs first
#define sc second

using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;

const double eps = 1e-10;

template<typename A, typename B>inline void chmin(A &a, B b){if(a > b) a = b;}
template<typename A, typename B>inline void chmax(A &a, B b){if(a < b) a = b;}

template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
	return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value!=0>::type
fill_v(U &u,const V... v){u=U(v...);}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value==0>::type
fill_v(U &u,const V... v){
	for(auto &e:u) fill_v<T>(e,v...);
}

template <typename F>
class
FixPoint : private F
{
public:
  explicit constexpr FixPoint(F&& f) noexcept
    : F{std::forward<F>(f)}
  {}

  template <typename... Args>
  constexpr decltype(auto)
  operator()(Args&&... args) const
  {
    return F::operator()(*this, std::forward<Args>(args)...);
  }
};  // class FixPoint


namespace
{
  template <typename F>
  inline constexpr decltype(auto)
  makeFixPoint(F&& f) noexcept
  {
    return FixPoint<F>{std::forward<F>(f)};
  }
}  // namespace

template<typename T>
class Matrix{
private:
  using size_type = ::std::size_t;
  using Row = ::std::vector<T>;
  using Mat = ::std::vector<Row>;

  size_type R, C; // row, column
  Mat A;

  void add_row_to_another(size_type r1, size_type r2, const T k){ // Row(r1) += Row(r2)*k
    for(size_type i = 0;i < C;i++)
      A[r1][i] += A[r2][i]*k;
  }

  void scalar_multiply(size_type r, const T k){
    for(size_type i = 0;i < C;i++)
      A[r][i] *= k;
  }

  void scalar_division(size_type r, const T k){
    for(size_type i = 0;i < C;i++)
      A[r][i] /= k;
  }

public:
  Matrix(){}
  Matrix(size_type r, size_type c) : R(r), C(c), A(r, Row(c)) {}
  Matrix(const Mat &m) : R(m.size()), C(m[0].size()), A(m) {}
  Matrix(const Mat &&m) : R(m.size()), C(m[0].size()), A(m) {}
  Matrix(const Matrix<T> &m) : R(m.R), C(m.C), A(m.A) {}
  Matrix(const Matrix<T> &&m) : R(m.R), C(m.C), A(m.A) {}
  Matrix<T> &operator=(const Matrix<T> &m){
    R = m.R; C = m.C; A = m.A;
    return *this;
  }
  Matrix<T> &operator=(const Matrix<T> &&m){
    R = m.R; C = m.C; A = m.A;
    return *this;
  }
  static Matrix I(const size_type N){
    Matrix m(N, N);
    for(size_type i = 0;i < N;i++) m[i][i] = 1;
    return m;
  }

  const Row& operator[](size_type k) const& { return A.at(k); }
  Row& operator[](size_type k) & { return A.at(k); }
  Row operator[](size_type k) const&& { return ::std::move(A.at(k)); }

  size_type row() const { return R; } // the number of rows
  size_type column() const { return C; }

  T determinant(){
    assert(R == C);
    Mat tmp = A;
    T res = 1;
    for(size_type i = 0;i < R;i++){
      for(size_type j = i;j < R;j++){ // satisfy A[i][i] > 0
        if (A[j][i] != 0) {
          if (i != j) res *= -1;
          swap(A[j], A[i]);
          break;
        }
      }
      if (A[i][i] == 0) return 0;
      res *= A[i][i];
      scalar_division(i, A[i][i]);
      for(size_type j = i+1;j < R;j++){
        add_row_to_another(j, i, -A[j][i]);
      }
    }
    swap(tmp, A);
    return res;
  }

  Matrix inverse(){
    assert(R == C);
    assert(determinant() != 0);
    Matrix inv(Matrix::I(R)), tmp(*this);
    for(size_type i = 0;i < R;i++){
      for(size_type j = i;j < R;j++){
        if (A[j][i] != 0) {
          swap(A[j], A[i]);
          swap(inv[j], inv[i]);
          break;
        }
      }
      inv.scalar_division(i, A[i][i]);
      scalar_division(i, A[i][i]);
      for(size_type j = 0;j < R;j++){
        if(i == j) continue;
        inv.add_row_to_another(j, i, -A[j][i]);
        add_row_to_another(j, i, -A[j][i]);
      }
    }
    (*this) = tmp;
    return inv;
  }

  Matrix& operator+=(const Matrix &B){
    assert(column() == B.column() && row() == B.row());
    for(size_type i = 0;i < R;i++)
      for(size_type j = 0;j < C;j++)
        (*this)[i][j] += B[i][j];
    return *this;
  }

  Matrix& operator-=(const Matrix &B){
    assert(column() == B.column() && row() == B.row());
    for(size_type i = 0;i < R;i++)
      for(size_type j = 0;j < C;j++)
        (*this)[i][j] -= B[i][j];
    return *this;
  }

  Matrix& operator*=(const Matrix &B){
    assert(column() == B.row());
    Matrix M(R, B.column());
    for(size_type i = 0;i < R;i++) {
      for(size_type j = 0;j < B.column();j++) {
        M[i][j] = 0;
        for(size_type k = 0;k < C;k++) {
          M[i][j] += (*this)[i][k] * B[k][j];
        }
      }
    }
    swap(M, *this);
    return *this;
  }

  Matrix& operator/=(const Matrix &B){
    assert(C == B.row());
    Matrix M(B);
    (*this) *= M.inverse();
    return *this;
  }

  Matrix operator+(const Matrix &B) const { return (Matrix(*this) += B); }
  Matrix operator-(const Matrix &B) const { return (Matrix(*this) -= B); }
  Matrix operator*(const Matrix &B) const { return (Matrix(*this) *= B); }
  Matrix operator/(const Matrix &B) const { return (Matrix(*this) /= B); }

  bool operator==(const Matrix &B) const {
    if (column() != B.column() || row() != B.row()) return false;
    for(size_type i = 0;i < row();i++)
      for(size_type j = 0;j < column();j++)
        if ((*this)[i][j] != B[i][j]) return false;
    return true;
  }
  bool operator!=(const Matrix &B) const { return !((*this) == B); }

  Matrix pow(size_type k, int64 m){
    assert(R == C);
    Matrix M(Matrix::I(R));
    while(k){
      if (k & 1) M *= (*this);
      k >>= 1;
      (*this) *= (*this);
      REP(i, R) {
        REP(j, C) {
          A[i][j] %= m;
          M[i][j] %= m;
        }
      }
    }
    A.swap(M.A);
    return *this;
  }

  friend ::std::ostream &operator<<(::std::ostream &os, Matrix &p){
    for(size_type i = 0;i < p.row();i++){
      for(size_type j = 0;j < p.column();j++){
        os << p[i][j] << " ";
      }
      os << ::std::endl;
    }
    return os;
  }
};

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int64 n, r;
  while (cin >> r >> n && r + n) {
    vector<int64> h(41, 0);
    REP(i, n) {
      int64 xl, xr, hi;
      cin >> xl >> xr >> hi;
      FOR(j, xl+20, xr+20) {
        chmax(h[j], hi);
      }
    }

    auto ok = [&](double x) {
      REP(j, 40) {
        double offl = j - 20, offr = j - 19;
        if (j-19 <= -r || r <= j-20) continue;
        double hmax = x + r * max(sin(atan2(sqrt(r * r - offl * offl), offl)), sin(atan2(sqrt(r * r - offr * offr), offr)));
        if (hmax > h[j]) return false;
      }
      return true;
    };
    double lo = -r, hi = 21, mi;
    REP(i, 100) {
      mi = (lo + hi) / 2;
      if (ok(mi)) lo = mi;
      else hi = mi;
    }
    cout << fixed << setprecision(10) << lo + r << endl;
  }
}
accuracy: 1
time_ms: 10
memory_kb: 4008
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define rrep(i,n) for(int (i)=((n)-1);(i)>=0;(i)--)
#define itn int
#define miele(v) min_element(v.begin(), v.end())
#define maele(v) max_element(v.begin(), v.end())
#define SUM(v) accumulate(v.begin(), v.end(), 0LL)
#define lb(a, key) lower_bound(a.begin(),a.end(),key)
#define ub(a, key) upper_bound(a.begin(),a.end(),key)
#define COUNT(a, key) count(a.begin(), a.end(), key) 
#define BITCOUNT(x) __builtin_popcount(x)
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define F first
#define S second
using P = pair <int,int>;
using WeightedGraph = vector<vector <P>>;
using UnWeightedGraph = vector<vector<int>>;
using Real = long double;
using Point = complex<Real>; //Point and Vector2d is the same!
using Vector2d = complex<Real>;
const long long INF = 1LL << 60;
const int MOD = 1000000007;
const double EPS = 1e-15;
const double PI=3.14159265358979323846;
template <typename T> 
int getIndexOfLowerBound(vector <T> &v, T x){
    return lower_bound(v.begin(),v.end(),x)-v.begin();
}
template <typename T> 
int getIndexOfUpperBound(vector <T> &v, T x){
    return upper_bound(v.begin(),v.end(),x)-v.begin();
}
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
#define DUMPOUT cerr
#define repi(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++)
istream &operator>>(istream &is, Point &p) {
    Real a, b; is >> a >> b; p = Point(a, b); return is;
}
template <typename T, typename U>
istream &operator>>(istream &is, pair<T,U> &p_var) {
    is >> p_var.first >> p_var.second;
    return is;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &vec) {
    for (T &x : vec) is >> x;
    return is;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, pair<T, U> &pair_var) {
    DUMPOUT<<'{';
    os << pair_var.first;
    DUMPOUT<<',';
    os << " "<< pair_var.second;
    DUMPOUT<<'}';
    return os;
}
template <typename T>
ostream &operator<<(ostream &os, vector<T> &vec) {
    DUMPOUT<<'[';
    for (int i = 0; i < vec.size(); i++) 
    os << vec[i] << (i + 1 == vec.size() ? "" : " ");
    DUMPOUT<<']';
    return os;
}
template <typename T>
ostream &operator<<(ostream &os, vector<vector<T>> &df) {
  for (auto& vec : df) os<<vec;
  return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, map<T, U> &map_var) {
    DUMPOUT << "{";
    repi(itr, map_var) {
        os << *itr;
        itr++;
        if (itr != map_var.end()) DUMPOUT << ", ";
        itr--;
    }
    DUMPOUT << "}";
    return os;
}
template <typename T>
ostream &operator<<(ostream &os, set<T> &set_var) {
    DUMPOUT << "{";
    repi(itr, set_var) {
        os << *itr;
        itr++;
        if (itr != set_var.end()) DUMPOUT << ", ";
        itr--;
    }
    DUMPOUT << "}";
    return os;
}
void print() {cout << endl;}
template <class Head, class... Tail>
void print(Head&& head, Tail&&... tail) {
  cout << head;
  if (sizeof...(tail) != 0) cout << " ";
  print(forward<Tail>(tail)...);
}
void dump_func() {DUMPOUT << '#'<<endl;}
template <typename Head, typename... Tail>
void dump_func(Head &&head, Tail &&... tail) {
    DUMPOUT << head;
    if (sizeof...(Tail) > 0) DUMPOUT << ", ";
    dump_func(std::move(tail)...);
}
#ifdef DEBUG_
#define DEB
#define dump(...)                                                              \
    DUMPOUT << "  " << string(#__VA_ARGS__) << ": "                            \
            << "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]"        \
            << endl                                                            \
            << "    ",                                                         \
        dump_func(__VA_ARGS__)
#else
#define DEB if (false)
#define dump(...)
#endif

int main(void) { cin.tie(0); ios::sync_with_stdio(false);
    while(1){
        double r; int n; 
        cin>>r>>n;
        if(r == 0 && n == 0) exit(0);
        vector <tuple <double,double,double>> p(n);
        double d = (r+r)/100000;
        for(int i=0;i<n;i++){
            int x1, x2, h;
            cin>>x1>>x2>>h;
            p[i] = {x1, x2, h};
        }
        double ans = 1145141919;
        for(double i=-r;i<=r;i+=d){
            double height = 0;
            for(int j=0;j<n;j++){
                if(get <0>(p[j]) <= i && get <1>(p[j]) >= i) chmax(height, get <2>(p[j]));
            }
            chmin(ans, height - (sqrt(r*r - i*i) - r));
        }
        printf("%.4f\n", ans);
    }
}
accuracy: 1
time_ms: 110
memory_kb: 3532
final_rank: 7

code_snippet: #include <bits/stdc++.h>

#define rep(i, n) for(int i = 0; i < n; i++)
using namespace std;

typedef long long ll;

const int INF = 1 << 30;
const ll LLINF = 1LL << 62;

const double eps = 0.00001;

int mod = 1000000007;

double deg_to_rad(double deg){
    return deg * M_PI / 180;
}

double calcheight(vector<pair<pair<double, double>, double>>& towers, double x){
    double ret = 0;
    rep(i, towers.size()){
        if(towers[i].first.first <= x && x <= towers[i].first.second) ret = max(ret, towers[i].second);
    }
    return ret;
}

int main(void){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    vector<double> ans;
    while(true){
        double R; int N; cin >> R >> N;
        if(R <= 0 && N == 0) break;
        vector<pair<pair<double, double>, double>> towers(N);
        rep(i, N) cin >> towers[i].first.first >> towers[i].first.second >> towers[i].second;
        double ok = 0, ng = 1000;
        double m = (ok+ng)/2;
        while(abs(ok-ng) > eps){
            double c = m - R;
            double inc = 0.002;
            double nowDeg = 0;
            bool hide = true;

            while(nowDeg <= 180){
                bool res;
                double x = R * cos(deg_to_rad(nowDeg));
                double y = m - R + R * sin(deg_to_rad(nowDeg));
                double height = calcheight(towers, x);

                //cout << "deg: " << nowDeg << ", " << y << " " << height << endl;
                if(height < y){
                    hide = false;
                    break;
                }
                nowDeg += inc;
            }
            if(hide) ok = m;
            else ng = m;
            m = (ok+ng)/2;
        }
        ans.push_back(ok);
    }
    rep(i, ans.size()) printf("%.6f\n", ans[i]);
    return 0;
}
accuracy: 1
time_ms: 4840
memory_kb: 3836
final_rank: 18