Problem:
かくれんぼう
たろう君はかくれんぼうが苦手です。隠れるとすぐ見つかっちゃうし、隠れている子をなかなか見つけることができません。見かねたお父さんは、超高性能位置探索システムを作ってくれました。これを使うと自分の位置を含めて、友達の位置を正確に知ることができます。鬼になったら、隠れている子を探すことが簡単にできるようになりました。
たろう君はさらにこのシステムを進化させ、鬼から自分が見えるか否かを判定する機能を付加することを思いつきました。これができれば、「もういいかい」と言われても、見える位置にいれば「まあだだよ」だし、見えない位置にいれば「もういいよ」ですね。いつも遊んでいる公園には大小さまざまな円筒形の壁があります。この壁は外からは中は見えないし、中から外も見えません。鬼と一緒に中に入ると別の壁がなければ見えちゃいます。
たろう君はアイデアはいいのですが、ソフトを作るのが苦手です。そこで親友のあなたはたろう君に代わって、「鬼から見えないこと確認システム」のソフトウェアを作ることになりました。公園の壁は固定されていますが、たろう君と鬼のいろいろな位置情報に対して見えるか否かを判定する必要があります。
公園にある壁の情報(中心の座標 (
wx
,
wy
) と 半径
r
) とたろう君と鬼の位置情報(たろう君の位置の座標 (
tx
,
ty
) と鬼の位置の座標 (
sx
,
sy
) ) を入力とし、その位置で鬼からたろう君が見えるか否かを判定するプログラムを作成してください。
なお、鬼からたろう君が見える場合には Danger、見えない場合には Safe と出力してください。鬼とたろう君の位置を結んだ線分上に壁がある場合には見えないものとし、鬼もたろう君も壁の上にはいないものとします。
Input
複数のデータセットが与えられます。各データセットは以下の形式で与えられます。
n
wx
1
wy
1
r
1
wx
2
wy
2
r
2
:
wx
n
wy
n
r
n
m
tx
1
ty
1
sx
1
sy
1
tx
2
ty
2
sx
2
sy
2
:
tx
m
ty
m
sx
m
sy
m
1 行目に円筒形の壁の数
n
(0 ≤
n
≤ 100)、続く
n
行に壁
i
の中心の座標を表す整数
wx
i
,
wy
i
(0 ≤
wx
i
,
wy
i
≤ 255) と半径を表す整数
r
i
(1 ≤
r
i
≤ 255) が与えられます。
続く行にたろう君と鬼の位置情報の個数
m
(m ≤ 100)、続く
m
行に位置情報
i
のたろう君の位置の座標を表す整数
tx
i
,
ty
i
(0 ≤
tx
i
,
ty
i
≤ 255) と鬼の位置の座標を表す整数
sx
i
,
sy
i
(0 ≤
sx
i
,
sy
i
≤ 255)  が与えられます。
また、円筒形の壁の一部だけが公園にあるということはなく、全ての壁は円筒として、公園の内部に入っています。
n
が 0 のとき入力の終わりを示します。データセットの数は 20 を超えません。
Output
各データセットについて、位置情報
i
の判定結果 Danger または Safe を
i
行目に出力してください。
Sample Input
3
6 6 3
19 7 4
21 8 1
6
5 4 2 11
12 4 2 11
11 9 2 11
14 3 20 5
17 9 20 5
20 10 20 5
0
Output for the Sample Input
Safe
Safe
Danger
Safe
Danger
Safe

Submitted solutions:

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

const double eps = 1e-10;

template <typename T>
struct point{
	T x,y,rad,dir;
	
	point(T a=0.0,T b=0.0){
		x = a;
		y = b;
		fix_rd();
	}
	
	void update_x(T a,T b){
		x = a*x + b;
		fix_rd();
	}
	
	void update_x(T a){
		update_x(0.0,a);
	}
	
	void update_y(T a,T b){
		y = a*y + b;
		fix_rd();
	}
	
	void update_y(T a){
		update_y(0.0,a);
	}
	
	void update_rad(T a,T b){
		rad = a*rad + b;
		fix_xy();
	}
	
	void update_rad(T a){
		update_rad(0.0,a);
	}
	
	void update_dir(T a,T b){
		dir = a*dir + b;
		fix_xy();
	}

	void update_dir(T a){
		update_dir(0.0,a);
	}
	
	void fix_xy(){
		x = rad * cos(dir);
		y = rad * sin(dir);
		fix_rd();
	}
	
	void fix_rd(){
		rad = hypot(x,y);
		if(rad==0.0)dir=0.0;
		else dir = atan2(y,x);
		fix_zero();
	}
	
	void fix_zero(){
		if(abs(x)<eps)x = 0.0;
		if(abs(y)<eps)y = 0.0;
		if(abs(rad)<eps)rad = 0.0;
		if(abs(dir)<eps)dir = 0.0;
	}
	
	void normalize(){
		T s = size();
		update_x(1.0/s,0.0);
		update_y(1.0/s,0.0);
	}
	
	
	T get_dis(point<T> V){
		return hypot(x-V.x,y-V.y);
	}
	
	T size(){
		return get_dis(point<T>());
	}
	
	T angle_difference(point<T> V){
		double ret = dir - V.dir;
		if(ret<-acos(-1.0))ret = acos(-1.0)*2.0+ret;
		if(ret>acos(-1.0))ret=-acos(-1.0)*2.0+ret;
		return ret;
	}
	
	//中点
	point get_midpoint(point<T> V){
		V.update_x(0.5,x/2.0);
		V.update_y(0.5,y/2.0);
		return V;
	}
	
	T get_inner_product(point<T> V){
		return x*V.x+y*V.y;
	}
	
	T get_cross_product(point<T> V){
		return x*V.y-y*V.x;
	}
	
	point &operator+=(const point<T> &another){
		update_x(1,another.x);
		update_y(1,another.y);
		return (*this);
	}
	
	point &operator-=(const point<T> &another){
		update_x(1,-another.x);
		update_y(1,-another.y);
		return (*this);
	}
	
	point operator+(const point<T> &another)const{
		return (point(*this)+=another);
	}
	
	point operator-(const point<T> &another)const{
		return (point(*this)-=another);
	}
	
	void show(){
		cout<<x<<','<<y<<endl;
	}
};

//a+tx
template <typename T>
struct line{
	point<T> a,t;
	
	line(){
	}
	
	line(point<T> V1,point<T> V2){
		a=V1;
		t=V2-V1;
	}

	T get_signed_dis(point<T> V){
		point<double> PA = a-V;
		return PA.get_cross_product(t)/t.size();
	}
	
	T get_dis(point<T> V){
		return abs(get_signed_dis(V));
	}
	
	point<T> get_projection(point<T> P){
		T r = t.get_inner_product(P-a)/t.size();
		point<T> temp = t;
		temp.update_rad(0.0,r);
		return a+temp;
	}	
	
	point<T> get_cross_point(line<T> L){
		point<T> ret(1e20,1e20);
		if(abs(t.get_cross_product(L.t))<eps)return ret;

		T d0 = L.get_signed_dis(a);
		T d1 = L.get_signed_dis(a+t);
		point<T> temp = t;
		temp.x *= d0/(d1-d0);
		temp.y *= d0/(d1-d0);
		ret = a - temp;
		return ret;
	}
	
};
template <typename T>
struct segment{
	point<T> V1,V2;
	segment(point<T> a=point<T>(),point<T> b=point<T>()){
		V1=a;
		V2=b;
	}
	
	T get_dis(point<T> P){
		T ret = 1e20;
		line<T> L(V1,V2);
		point<T> Q = L.get_projection(P);
		if(Q.x+eps>min(V1.x,V2.x)&&Q.y+eps>min(V1.y,V2.y)
		&&Q.x<max(V1.x,V2.x)+eps&&Q.y<max(V1.y,V2.y)+eps)ret = min(ret,Q.get_dis(P));
		else{
			ret = min(ret,P.get_dis(V1));
			ret = min(ret,P.get_dis(V2));
		}
		return ret;
	}
	
	T get_dis(segment<T> l){
		if(get_cross_point(l).x<1e20)return 0.0;
		return min({get_dis(l.V1),get_dis(l.V2),l.get_dis(V1),l.get_dis(V2)});
	}
	
	point<T> get_cross_point(segment<T> l){
		line<T> L1(V1,V2),L2(l.V1,l.V2);
		point<T> P = L1.get_cross_point(L2);
		if(get_dis(P)<eps&&l.get_dis(P)<eps)return P;
		return point<T> (1e20,1e20);
	}
};

int main(){
	while(true){
		int n;
		cin>>n;
		if(n==0)break;
		
		vector<int> x(n),y(n),r(n);
		rep(i,n)cin>>x[i]>>y[i]>>r[i];
		
		int m;
		cin>>m;
		
		rep(_,m){
			int a,b,c,d;
			cin>>a>>b>>c>>d;
			bool f = true;
			rep(i,n){
				bool f0 = (r[i]*r[i]) > (x[i]-a)*(x[i]-a) + (y[i]-b)*(y[i]-b);
				bool f1 = (r[i]*r[i]) > (x[i]-c)*(x[i]-c) + (y[i]-d)*(y[i]-d);
				if(f0!=f1)f = false;
				if(!f0 && !f1){
					segment<double> l(point<double>(a,b),point<double>(c,d));
					point<double> p(x[i],y[i]);
					double dd = l.get_dis(p);
					//if(_==2)cout<<dd<<endl;
					if(dd < r[i] + eps)f=  false;
				}
			}
			if(f)cout<<"Danger"<<endl;
			else cout<<"Safe"<<endl;
		}
		
	}
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3600
final_rank: 13

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

using uint=unsigned;
using ll=long long;
using ull=unsigned long long;
#define int ll

#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,b) rng(i,0,b)
#define gnr(i,a,b) for(int i=int(b)-1;i>=a;i--)
#define per(i,b) gnr(i,0,b)
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define eb emplace_back
#define fs first
#define sc second
#define bg begin()
#define ed end()
#define all(x) x.bg,x.ed
#ifdef LOCAL
#define dmp(x) cerr<<__LINE__<<" "<<#x<<" "<<x<<endl
#else
#define dmp(x) void(0)
#endif
#define tmp template<class t>

tmp void chmax(t&a,t b){if(a<b)a=b;}
tmp void chmin(t&a,t b){if(a>b)a=b;}

tmp using vc=vector<t>;
tmp using vvc=vc<vc<t>>;

using pi=pair<int,int>;
using vi=vc<int>;

template<class t,class u>
ostream& operator<<(ostream& os,const pair<t,u>& p){
	return os<<"{"<<p.fs<<","<<p.sc<<"}";
}

tmp ostream& operator<<(ostream& os,const vc<t>& v){
	os<<"{";
	for(auto e:v)os<<e<<",";
	return os<<"}";
}

constexpr ll ten(int n){
	return n==0?1:ten(n-1)*10;
}

tmp void mkuni(vc<t>&v){
	sort(all(v));
	v.erase(unique(all(v)),v.ed);
}

bool inc(int a,int b,int c){
	return a<=b&&b<=c;
}

ll read(){ll i;cin>>i;return i;}

using ld=long double;
using cm=complex<ld>;
#define x real()
#define y imag()
const ld eps=1e-7;
int sgn(ld a){return a<-eps?-1:(a>eps?1:0);}
auto cmcmp=[](const cm&a,const cm&b){
	if(sgn(a.x-b.x))return a.x<b.x;
	else return sgn(a.y-b.y)<0;
};

ld dot(cm a,cm b){return a.x*b.x+a.y*b.y;}
ld crs(cm a,cm b){return a.x*b.y-a.y*b.x;}
int ccw(cm a,cm b){return sgn(crs(a,b));}
int ccw(cm a,cm b,cm c){return ccw(b-a,c-a);}

//(-2)[a,-1](0)[b,1](2)
int bet(cm a,cm b,cm c){
	cm d=b-a;
	ld e=dot(d,c-a);
	if(sgn(e)<=0)return sgn(e)-1;
	return sgn(e-norm(d))+1;
}

ld tri2(cm a,cm b,cm c){
	return crs(b-a,c-a);
}

//0-no,1-edge,2-in
int cont(cm a,cm b,cm c,cm d){
	if(ccw(a,b,c)==-1)
		swap(b,c);
	return min({ccw(a,b,d),ccw(b,c,d),ccw(c,a,d)})+1;
}

struct ln{
	cm a,b;
	cm dir(){return b-a;}
	cm proj(cm c){
		cm d=dir();
		return a+d*dot(d,c-a)/norm(d);
	}
	cm refl(cm c){
		return ld(2)*proj(c)-c;
	}
	//AOJ0153
	ld dsp(cm c){
		cm d=proj(c);
		if(abs(bet(a,b,d))<=1)return abs(c-d);
		return min(abs(c-a),abs(c-b));
	}
};
int ccw(ln a,cm b){return ccw(a.a,a.b,b);}

struct cr{
	cm c;
	ld r;
	//AOJ0153
	//0-no,1-edge,2-in
	int cont(cm a){
		return sgn(r-abs(a-c))+1;
	}
	//AOJ0153
	//0-no,1-touch,2-cross
	int ids(ln a){
		return sgn(r-a.dsp(c))+1;
	}
	//0-no(in),1-touch(in),2-cross,3-touch(out),4-no(out)
	int ics(ln a){
		int b=ids(a);
		if(b<=1)return 4-b;
		return sgn(max(abs(a.a-c),abs(a.b-c))-r)+1;
	}
};

cm readcm(){
	ld a,b;
	cin>>a>>b;
	return cm(a,b);
}

ld readld(){
	ld a;
	cin>>a;
	return a;
}

signed main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	cout<<fixed<<setprecision(20);
	while(1){
		int n=read();
		if(n==0)break;
		vc<cr> a(n);
		rep(i,n)
			a[i]=cr{readcm(),readld()};
		int m=read();
		rep(_,m){
			cm b=readcm();
			cm c=readcm();
			bool ok=true;
			for(auto d:a)
				if(inc(1,d.ics(ln{b,c}),3))
					ok=false;
			if(ok)
				cout<<"Danger"<<endl;
			else
				cout<<"Safe"<<endl;
		}
	}
}
accuracy: 1
time_ms: 10
memory_kb: 3260
final_rank: 11

code_snippet: #include<iostream>
#include<complex>
#include<algorithm>
#include<vector>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
#define pb push_back
using namespace std;
typedef complex<double> P;
typedef pair<double,double> point;


double dot(P a,P b){
	return real(conj(a)*b);
}

double cross(P a,P b){
	return imag(conj(a)*b);
}

double dist(P a, P b,P c){
	if(dot(b-a,c-a)<0)return abs(c-a);
	if(dot(a-b,c-b)<0)return abs(c-b);
	return abs(cross(c-a,b-a))/abs(b-a);
}

int main(){
	int n,m;
	double x,y,r;
	P taro,oni;
	vector<pair<P,double> > circle; 
	while(cin>>n,n){
		circle.clear();
		rep(i,n){
			cin>>x>>y>>r;
			circle.pb(pair<P,double>(P(x,y),r));
		}
		cin>>m;
		rep(p,m){
			cin>>x>>y;
			taro=P(x,y);
			cin>>x>>y;
			oni=P(x,y);
			bool check=false;
			rep(i,n){
				r=circle[i].second;
				double d=dist(taro,oni,circle[i].first);
				if(d<=r && (abs(taro-circle[i].first)>=r || abs(oni-circle[i].first)>=r)){
					check=true;
					break;
				}
			}
			if(check)cout<<"Safe"<<endl;
			else cout<<"Danger"<<endl;
		}
	}
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1252
final_rank: 6

code_snippet: #include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <functional>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
 
using namespace std;
 
#define REP(i,s,e) for(int i=(s); i<(int)(e); i++)
#define rep(i,n) REP(i, 0, n)
#define pb push_back
#define mp make_pair
#define all(r) (r).begin(),(r).end()
#define rall(r) (r).rbegin(),(r).rend()
#define fi first
#define se second
 
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
 

 
const int INF = 1000000;
const double EPS = 1e-8;
const int mod = 1e9 + 7;

const double PI = acos(-1);

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

struct Point
{
	double x, y;
	Point(){};
	Point(double x, double y):x(x), y(y){};

	Point operator - (const Point& p) const{
		return Point(x-p.x, y-p.y);
	}

	double getAng(){
		return atan2(y, x);
	}
	double getCross(const Point& p) const{
		return x*p.y-y*p.x;
	}
	double getDot(const Point& p) const{
		return x*p.x+y*p.y;
	}
};

struct Circle
{
	Point p;
	double r;
	Circle(){};
	Circle(double x, double y, double r):p(x, y), r(r){};
	Circle(Point p, double r) :p(p),r(r){};

};

struct Line{
	Point p;
	double ang;
	Line(){};
	Line(Point p1, Point p2):p(p1), ang((p2-p1).getAng()){};
	Line(Point p, double ang):p(p), ang(ang){};
};
	
int n;
Circle c[1010];
Point taro, oni;
Line l;
Point lv;

bool checkSigneCross(Point p){
	return lv.getCross(p-l.p) > 0;
}

Point getCrossPoint(Line L){
	Point ret;
	ret.x = L.p.x + 1000000 * cos(L.ang);
	ret.y = L.p.y + 1000000 * sin(L.ang);
	bool sign = checkSigneCross(L.p), sign2 = checkSigneCross(ret);
	sign2 = sign^sign2;
	//cout<<sign2<<endl;
	double l = 0.0, r = 1000000.0, m;
	rep(i, 1000){
		m = (l + r) / 2.0;
		ret.x = L.p.x + m * cos(L.ang) * (sign2?1:-1);
		ret.y = L.p.y + m * sin(L.ang) * (sign2?1:-1);
		if(checkSigneCross(ret) == sign) l = m;
		else r = m;
	}
	return ret;
}

bool checkInCircle(const Circle& C, const Point& P){
	Point p = C.p-P;
	return p.x*p.x+p.y*p.y < C.r*C.r + EPS;
}

bool check(){
	for(int i = 0; i < n; i++){
		if(checkInCircle(c[i], taro)^checkInCircle(c[i], oni)) return true;
		else if(checkInCircle(c[i], taro)&&checkInCircle(c[i], oni)) continue;
		Line l2(c[i].p, l.ang+PI/2.0);

		Point p =  getCrossPoint(l2);
		if(checkInCircle(c[i], p) && (taro-p).getDot(oni-p) < 0) return true;
	}
	return false;
}

int main(){
	int m;
	while(cin>>n && n){
		rep(i, n) cin>>c[i].p.x>>c[i].p.y>>c[i].r;
		cin>>m;
		rep(i, m){
			cin>>taro.x>>taro.y>>oni.x>>oni.y;
			l = Line(taro, oni);
			lv = Point(cos(l.ang), sin(l.ang));
			cout<<(check()?"Safe":"Danger")<<endl;
		}
	}
}
accuracy: 1
time_ms: 30
memory_kb: 1308
final_rank: 10

code_snippet: #include <iostream>
#include <string>
#include <vector>
using namespace std;

struct Wall { int x, y, r; };

bool inside(Wall w, double x, double y) {
	return w.r * w.r >= (w.x - x) * (w.x - x) + (w.y - y) * (w.y - y);
}

int main() {
	int n, wx, wy, r, m, tx, ty, sx, sy;
	while (cin >> n, n) {
		vector<Wall> w;
		for (int i=0; i<n; ++i) {
			cin >> wx >> wy >> r;
			w.push_back((Wall){wx, wy, r});
		}
		cin >> m;
		for (int i=0; i<m; ++i) {
			cin >> tx >> ty >> sx >> sy;
			bool safe = false;
			for (int j=0; j<n && !safe; ++j) {
				int k = inside(w[j], tx, ty) + inside(w[j], sx, sy);
				if (k == 0) {
					double dx, dy, x, y, minx, maxx, miny, maxy;
					dx = (sx - tx) / 10000.0;
					dy = (sy - ty) / 10000.0;
					x = tx;
					y = ty;
					minx = min(sx, tx);
					maxx = max(sx, tx);
					miny = min(sy, ty);
					maxy = max(sy, ty);
					while ( ! safe && minx <= x && x <= maxx && miny <= y && y <= maxy) {
						if (inside(w[j], x, y)) {
							safe = true;
							break;
						}
						x += dx;
						y += dy;
					}
				} else if (k == 1) {
					safe = true;
				}
			}
			cout << (safe ? "Safe" : "Danger") << endl;
		}
	}
	return 0;
}
accuracy: 1
time_ms: 180
memory_kb: 1212
final_rank: 12

code_snippet: #include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <deque>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <ctime>
#include <iterator>
#include <bitset>
#include <numeric>
#include <list>
#include <iomanip>

#if __cplusplus >= 201103L
#include <array>
#include <tuple>
#include <initializer_list>
#include <unordered_set>
#include <unordered_map>
#include <forward_list>

#define cauto const auto&
#else

#endif

using namespace std;


namespace{
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
    v.assign(a, vector<T>(b, t));
}

template <class F, class T>
void convert(const F &f, T &t){
    stringstream ss;
    ss << f;
    ss >> t;
}

#define double long double
#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(),(v).rend()


#define MOD 1000000007LL
#define EPS 1e-8
static const int INF=1<<24;

int check(double x1,double y1,double x2,double y2,double r2){
	double xy=(x2-x1)*(x2-x1)+(y2-y1)*(y2-y1),r=r2*r2;
	if(r-xy<-EPS) return 1;
	else return -1;
}


void mainmain(){
	int n;
	while(cin>>n,n){
		vector<pair<pair<double,double>,double > >  a(n);
		rep(i,n){
			cin>>a[i].F.F>>a[i].F.S>>a[i].S;
		}
		int m;
		cin>>m;
		// vector<pair<double,double> > b(m);
		rep(l,m){
			double b[4];
			cin>>b[0]>>b[1]>>b[2]>>b[3];
			bool f=false;
			double x=b[0],y=b[1];
			double dx=(b[2]-b[0])/10000.0,dy=(b[3]-b[1])/10000.0;
			rep(i,n){
				int t=check(b[0],b[1],a[i].F.F,a[i].F.S,a[i].S);
				reep(j,1,10001){
					int t1=check(x+dx*j,y+dy*j,a[i].F.F,a[i].F.S,a[i].S);
						// cout<<x+dx*j<<" "<<y+dy*j<<" "<<a[i].F.F<<" "<<a[i].F.S<<" "<<a[i].S<<endl;
					if(t!=t1){
						f=true;
						break;
					}	
				}
				if(f) break;
			}
			if(f) cout<<"Safe\n";
			else cout<<"Danger\n";
		}
	}
	// cout<<check(0,3,3,3,3)<<endl;
	// cout<<check(-1,3,3,3,3)<<endl;
	// cout<<check(0.5,0,1,0,1)<<endl;
}



}
main() try{
    mainmain();
}
catch(...){
}
accuracy: 1
time_ms: 270
memory_kb: 1196
final_rank: 14

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define fr first
#define sc second
struct line: public vector< complex<double> >{
  line(){resize(2);};
  line( const complex<double>& a, const complex<double>& b){
    push_back(a);
    push_back(b);
  }
};
struct circle {
  complex<double> p; double r;
  circle():p(0,0),r(0){};
  circle(const complex<double> &p, double r) : p(p),r(r){}
};
typedef complex < double > P;
typedef line               L;
typedef pair < P, P >      Ls;
typedef vector< P >        G;
typedef vector< P >        Ps;
typedef vector< L >        LLL;
typedef circle             C;
const double EPS = 1e-10;
const double INF = 1e8;
bool   eq(P,P); //点:点 同一判定
double cross(P,P); //外積
double dot(P,P); //内積
int    ccw(P,P,P); //3点の位置関係
bool   parallel(L,L); // 直線//直線
bool   orthogonal(L,L); //直線⊥直線
bool   intersect(L,L); //線分:線分交差
bool   intersect(L,P); //線分:点交差
bool   intersect(Ls,Ls); //直線:直線交差
bool   intersect(Ls,L); //直線:線分交差
bool   intersect(Ls,P); //直線:点交差
int    intersect(C,L); //円:線分交点数
bool   intersect(C,Ls); //円:直線交差
bool   intersect(C,C); //円:円交差
bool   intersect(C,P); //円:点交差
double distance(L,L); //線分:線分の距離
double distance(L,P); //線分:点の距離
double distance(P,P); //点:点の距離
double distance(Ls,P); //直線:点距離
double distance(Ls,Ls); //直線:直線距離
double distance(Ls,L); //直線:線分距離
P      crosspoint(L,L); //線分:線分交点計算
L      crosspoint(C,Ls); //円:直線交点計算
L      crosspoint(C,L); //円:線分交点計算
L      crosspoint(C,C); //円:円交点計算
int    contains(G,P); //図形:点内包判定
bool   contains(C,P);
double area2(G); //面積
bool   isconvex(G); //凸性判定
Ps     convex(G); //凸包
   
namespace std {
  bool operator < (const P& a, const P& b) {
    return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);
  }
}
L llcomb(Ls a){
  L line( a.fr, a.sc);
  return line;
}
Ls llrcomb(L a){
  Ls line( a[0], a[1]);
  return line;
}
bool eq( P a, P b){ //OK
  return abs( a - b) < EPS;
}
double cross( P a,  P b){ //OK
  return imag( conj(a) * b);
}
double dot( P a, P b){ //OK
  return real( conj(a) * b);
}
P projection( L l, P p) { //OK
  double t = dot( p - l[0], l[0] - l[1]) / norm( l[0] - l[1]);
  return l[0] + t * ( l[0] - l[1]);
}
P reflection( L l, P p) {
  return p + 2.0 * ( projection( l, p) - p);
}
int ccw( P a, P b, P c){  //OK
  b -= a, c -= a;
  if(cross(b,c) > 0)    return +1;  // a → b で 反時計方向におれて c
  if(cross(b,c) < 0)    return -1; // a → b で 時計方向におれて c
  if(dot(b,c) < 0)      return +2;  // c -- a -- b
  if(norm(b) < norm(c)) return -2; // a -- b -- c
                        return 0;  // a -- c -- b
}
bool intersect( L a, L b){ //OK
  return ccw( a[0], a[1], b[0]) * ccw( a[0], a[1], b[1]) <= 0 &&
    ccw( b[0], b[1], a[0]) * ccw( b[0], b[1], a[1]) <= 0;
}
bool intersect( L a, P p){ //OK
   return abs( a[0] - p) + abs( a[1] - p) - abs( a[1] - a[0]) < EPS;
}
bool intersect( Ls l, Ls m) { //OK
  return abs(cross(l.sc-l.fr, m.sc-m.fr)) > EPS ||
         abs(cross(l.sc-l.fr, m.fr-l.fr)) < EPS;
}
bool intersect(Ls l, L s) { //OK
  return cross( l.sc - l.fr, s[0] - l.fr) *
         cross( l.sc - l.fr, s[1] - l.fr) < EPS;
}
bool intersect(Ls l, P p) { //OK
  return abs( cross( l.sc - p, l.fr - p)) < EPS;
}
bool intersect( C c, Ls s){ //OK
  return distance( s, c.p) <= c.r + EPS;
}
bool intersect( C a, C b){ //OK
  return ( norm( a.p - b.p) - ( a.r + b.r) * ( a.r + b.r) < EPS) &&
    ( norm( a.p - b.p) - ( a.r - b.r) * ( a.r - b.r) > -EPS);
}
int intersect( C c, L l){ //OK
  if( norm( projection( l, c.p) - c.p) - c.r * c.r > EPS) return 0;
  const double d1 = abs( c.p - l[0]), d2 = abs( c.p - l[1]);
  if( d1 < c.r + EPS && d2 < c.r + EPS) return 0;
  if( d1 < c.r - EPS && d2 > c.r + EPS
      || d1 > c.r + EPS && d2 < c.r - EPS ) return 1;
  const P h = projection( l, c.p);
  if( dot( l[0] - h, l[1] - h) < 0) return 2;
  return 0;
}
bool intersect( C c, P p){ //OK
  return abs( abs( p - c.p) - c.r ) < EPS;
}
double distance( L s, P p){ //OK
  P r = projection(s, p);
  if ( intersect( s, r)) return abs( r - p);
  return min( abs( s[0] - p), abs( s[1] - p));
}
double distance( L a, L b){ //OK
  if(intersect( a, b)) return 0;
  return min( min( distance( a, b[0]), distance( a, b[1])),
              min( distance( b, a[0]), distance( b, a[1])));
}
double distance( Ls l, P p) { //OK
  return abs(p - projection( llcomb(l), p));
}
double distance( Ls l, Ls m) { //OK
  return intersect( l, m) ? 0 : distance( l, m.fr);
}
double distance( Ls l, L s) { //OK
  if (intersect(l, s)) return 0;
  return min(distance(l, s[0]), distance(l, s[1]));
}
double distance( P a, P b){ //OK
  return abs( a - b);
}
bool parallel( L a, L b){
  return abs( cross( a[1] - a[0], b[1] - b[0])) < EPS;
}
bool orthogonal( L a, L b){
  return dot( a[0] - a[1], b[0] - b[1]) < EPS;
}
#define curr(P,i) P[i]
#define next(P,i) P[(i+1)%P.size()]
#define prev(P,i) P[(i+P.size()-1) % P.size()]
enum { OUT, ON, IN };
int contains(G Q, P p){ //OK
  bool in = false;
  for(int i = 0 ; i < Q.size() ; i++ ){
    P a = curr(Q,i) - p, b = next(Q,i) - p;
    if(imag(a) > imag(b)) swap(a,b);
    if(imag(a) <= 0 && 0 < imag(b) && cross(a,b) < 0) in = !in;
    if(cross(a,b) == 0 && dot(a,b) <= 0) return ON;
  }
  return in ? IN : OUT;
}
bool contains(C c,P p){
  return abs( c.p - p) < c.r + EPS;
}
double area2(G p){ //OK
  double A = 0;
  for (int i = 0; i < p.size(); ++i){
    A += cross(curr(p, i), next(p, i));
 
    cout << cross(curr(p,i),next(p,i)) << endl;
  }
  return A;
}
bool isconvex(G p) { // OK
  for (int i = 0; i < p.size(); ++i){
    if (ccw(prev(p, i), curr(p, i), next(p, i)) > 0) return false;
  }
  return true;
}
Ps convex(Ps ps) { //n>=3 OK
  int k = 0;
  sort(ps.begin(), ps.end());
  Ps ch(2 * ps.size());
  for (int i = 0; i < ps.size(); ch[k++] = ps[i++]){
    while (k >= 2 and ccw(ch[k-2], ch[k-1], ps[i]) <= 0) --k;
  }
  for (int i = ps.size()-2, t = k+1; i >= 0; ch[k++] = ps[i--]){
    while (k >= t and ccw(ch[k-2], ch[k-1], ps[i]) <= 0) --k;
  }
  ch.resize(k-1);
  return ch;
}
P crosspoint(L l, L m) { //OK
  double A = cross(l[1] - l[0], m[1] - m[0]);
  double B = cross(l[1] - l[0], l[1] - m[0]);
  if (abs(A) < EPS && abs(B) < EPS) return m[0]; // same line
  return m[0] + B / A * (m[1] - m[0]);
}
L crosspoint( C c, Ls l) { //OK
  const P hp = projection( llcomb(l), c.p), h =  hp - c.p;
  const double d2 = norm(h);
  P v = sqrt( c.r * c.r - d2) * ( l.sc - l.fr) / abs( l.sc - l.fr);
  return L(hp - v, hp + v);
}
L crosspoint( C c, L l) { //OK
  if(intersect(c, l) == 2) return crosspoint(c, llrcomb(l));
  L ret = crosspoint(c, llrcomb(l));
  if(dot(l[0] - ret[0], l[1] - ret[0]) < 0) ret[1] = ret[0];
  else ret[0] = ret[1];
  return ret;
}
L crosspoint(C c1, C c2){ //OK
  double d = abs(c1.p - c2.p);
  double s = (c1.r + c2.r + d) / 2;
  double S = sqrt( s * ( s - c1.r) * ( s - c2.r) * ( s - d));
  double h = 2 * S / d;
  P v = ( c2.p - c1.p) / ( abs( c2.p - c1.p));
  double m = sqrt( c1.r * c1.r - h * h);
  return L( c1.p + m * v + h * v * P(0,1), c1.p + m * v - h * v * P(0,1));
}

int main(){
  int n, m;
  while(cin >> n, n){
    vector< C > cs(n);
    for(int i = 0 ; i < n ; i++ ){
      cin >> cs[i].p.real() >> cs[i].p.imag() >> cs[i].r;
    }
    L l;
    cin >> m;
    while(m--){
      cin >> l[0].real() >> l[0].imag() >> l[1].real() >> l[1].imag();
      bool flag = false;
      for(int i = 0 ; i < n ; i++ ){
        flag |= !!intersect( cs[i], l);
      }
      if(flag) cout << "Safe" << endl;
      else cout << "Danger" << endl;
    }
  }
}
accuracy: 1
time_ms: 10
memory_kb: 1256
final_rank: 8

code_snippet: #include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <utility>
#include <numeric>
#include <iterator>

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int,int> PII;
typedef vector<PII> VPII;
typedef istringstream ISS;
typedef ostringstream OSS;

#define REP( i, m, n ) for ( int i = (int)( m ); i < (int)( n ); ++i )
#define FOR( v, c ) for ( auto &v : c )
#define EACH( it, c ) for ( auto it = c.begin(); it != c.end(); ++it )
#define ALL( c ) (c).begin(), (c).end()
#define DRANGE( c, p ) (c).begin(), (c).begin() + p, (c).end()

#define PB( n ) push_back( n )
#define MP( a, b ) make_pair( ( a ), ( b ) )
#define EXIST( c, e ) ( (c).find( e ) != (c).end() )

#define fst first
#define snd second

#define DUMP( x ) cerr << #x << " = " << ( x ) << endl
#define DEBUG( x ) cerr << __FILE__ << ":" << __LINE__ << ": " << #x << " = " << ( x ) << endl

#include <complex>
typedef complex<double> Point;
const double EPS = 1e-8;

// 入力ストリームから実数二つをとって Point へ
istream& operator >> ( istream &s, Point &a )
{
	double r, i;
	s >> r >> i;
	a = Point( r, i );
	return s;
}

// 内積（ドット積）
double dot( const Point &a, const Point &b )
{
	return a.real() * b.real() + a.imag() * b.imag();
}

// 外積（クロス積）
double cross( const Point &a, const Point &b )
{
	return a.real() * b.imag() - a.imag() * b.real();
}

// 直線 ( p1, p2 ) と点 q の距離
// include : cross
double distance_line_point( const Point &p1, const Point &p2, const Point &q )
{
	return abs( cross( p2 - p1, q - p1 ) ) / abs( ( p2 - p1 ) );
}

// 線分 ( p1, p2 ) と点 q の距離
// include : dot, distance_line_point
double distance_segment_point( const Point &p1, const Point &p2, const Point q )
{
	if ( EPS < dot( p2 - p1, q - p1 ) && EPS < dot( p1 - p2, q - p2 ) ) // 垂線が交わる
	{
		return distance_line_point( p1, p2, q );
	}
	else
	{
		return min( abs( p1 - q ), abs( p2 - q ) );
	}
}

// 線分 ( p1, p2 ) と円 ( q, r ) が交差するか
// include : distance_segment_point
bool segment_circle_intersect( const Point &p1, const Point &p2, const Point &q, const double r )
{
	if ( abs( p1 - q ) + EPS < r && abs( p2 - q ) + EPS < r )
	{
		return false;
	}
	else if ( r + EPS < abs( p1 - q ) && r + EPS < abs( p1 - q ) )
	{
		return distance_segment_point( p1, p2, q ) <= r + EPS;
	}
	else
	{
		return true;
	}
};

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

	for ( int n; cin >> n, n; )
	{
		vector<Point> walls( n );
		vector<double> rs( n );
		REP( i, 0, n )
		{
			cin >> walls[i] >> rs[i];
		}

		int m;
		cin >> m;
		REP( i, 0, m )
		{
			Point t, s;
			cin >> t >> s;

			bool safe = false;
			REP( j, 0, n )
			{
				safe |= segment_circle_intersect( t, s, walls[j], rs[j] );
			}
			cout << ( safe ? "Safe" : "Danger" ) << endl;
		}
	}

	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1252
final_rank: 6

code_snippet: #include<iostream>
#include<cstdio>
#define _USE_MATH_DEFINES
#include<cmath>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;


#define INF 1e+10
#define EPS 1e-10
#define EQ(a,b) (abs(a-b)<EPS)

//ティツェツ、テ・ツキツョテ」ツつ津ィツ?εヲツ?ョテ」ツ?療」ツ?ヲティツカツウテ」ツ?療ァツョツ?
double add(double a,double b){
	if(abs(a+b) < EPS*(abs(a)+abs(b)))return 0;
	return a+b;
}
struct P{//2テヲツャツ。テ・ツ?ε」ツδ凖」ツつッテ」ツδ暗」ツδォ
	double x,y;
	P(){}
	P(double x,double y):x(x),y(y){}
	P operator + (P p){
		return P(add(x,p.x),add(y,p.y));
	}
	P operator - (P p){
		return P(add(x,-p.x),add(y,-p.y));
	}
	P operator * (double d){
		return P(x*d,y*d);
	}
	P operator / (double d){
		return P(x/d,y/d);
	}
	double dot(P p){//テ・ツ??ァツゥツ?
		return add(x*p.x,y*p.y);
	}
	double det(P p){//テ・ツ、ツ姪ァツゥツ?
		return add(x*p.y,-y*p.x);
	}
	bool equal(P p){
		return (x-p.x)*(x-p.x)+(y-p.y)*(y-p.y) < EPS*EPS;
	}
};
//typedef pair<P,P> Line;

//テァツキツ堙・ツ按?1-p2テ、ツクツ甘」ツ?ォテァツつケqテ」ツ?古」ツ?づ」ツつ凝」ツ?凝・ツ按、テ・ツョツ?
bool on_seg(P p1,P p2,P q){
	return (p1-q).det(p2-q)==0 && (p1-q).dot(p2-q)<=0;
}
//テァツ崢エテァツキツ嗔1-p2テ」ツ?ィテァツ崢エテァツキツ嘔1-q2テ」ツ?ョテ、ツコツ、テァツつケ
P intersection(P p1,P p2,P q1,P q2){
	return p1+(p2-p1)*( (q2-q1).det(q1-p1)/(q2-q1).det(p2-p1) );
}


//テァツキツ堙・ツ按?1-p2テ」ツ?ィテァツキツ堙・ツ按?1-q2テ」ツ?古、ツコツ、テ・ツキツョテ」ツ?凖」ツつ凝」ツ??
bool exist_intersection(P p1,P p2,P q1,P q2){
	if(on_seg(p1,p2,q1)||on_seg(p1,p2,q2)||on_seg(q1,q2,p1)||on_seg(q1,q2,p2))return true;
	if((p1-p2).det(q1-q2)==0 )return false;//テ・ツケツウティツ。ツ?
	P a=intersection(p1,p2,q1,q2);
	return on_seg(p1,p2,a) && on_seg(q1,q2,a);
}
//テ・ツ篠淌ァツつケテ、ツクツュテ・ツソツε・ツ崢榲ィツサツ「
P rotate(P p,double rad){
	double s=sin(rad),c=cos(rad);
	return P(c*p.x-s*p.y, s*p.x+c*p.y);
}
//originテ」ツつ津、ツクツュテ・ツソツε」ツ?ォテ・ツ崢榲ィツサツ「
P rotate(P p,double rad,P origin){
	return origin + rotate(p - origin,rad);
}
//テ、ツコツ古ァツつケテゥツ鳴禿」ツ?ョティツキツ敕ゥツ崢「
double dist(P p,P q){
	p = p-q;
	return sqrt(p.x*p.x+p.y*p.y);
}
//テ・ツ債佚、ツスツ催」ツδ凖」ツつッテ」ツδ暗」ツδォテ」ツ?ォテ・ツ、ツ嘉ヲツ渉?
P to_unit(P p){
	double d = sqrt(p.x*p.x+p.y*p.y);
	return p/d;
}
//テ・ツ??・ツ青古・ツ」ツォテ」ツ?ョテ、ツコツ、テァツつケテ」ツつ津ィツィツ暗ァツョツ?
pair<P,P> circle_intersection(double x1,double y1,double r1,double x2,double y2,double r2){
	double rad = atan2(y2-y1,x2-x1);
	x2 -= x1;
	y2 -= y1;
	P tmp = rotate(P(x2,y2),-rad);
	x2 = tmp.x;
	y2 = tmp.y;
	double x = (r1*r1-r2*r2+x2*x2)/(2*x2);
	if(abs(r1) < abs(x))return make_pair(P(0,0),P(0,0));//テ、ツコツ、テァツつケテ」ツ?ェテ」ツ??
	double y = sqrt(r1*r1-x*x);
	return make_pair( P(x1,y1)+rotate(P(x,y),rad) ,  P(x1,y1)+rotate(P(x,-y),rad) );
}
//2テァツつケテ」ツつ津ゥツ?堙」ツつ凝・ツ債甘・ツセツвテ」ツ?ョテ・ツ??」ツ?ョテ、ツクツュテ・ツソツε・ツコツァテヲツィツ?
vector<P> circle_on_2P(P p,P q,double r){
	vector<P> res; 
	if(2*r+EPS < dist(p,q))return res;//ティツァツ」テ」ツ?ェテ」ツ??
	if(q.x < p.x)swap(p,q);
	q = q - p;
	double rad = atan2(q.y,q.x);
	P tmp = rotate(q,-rad);
	double x = tmp.x/2;
	double y = sqrt(4*r*r-tmp.x*tmp.x)/2;
	P a = p+rotate(P(x,y),rad) , b = p+rotate(P(x,-y),rad);
	res.push_back(a);
	if(!a.equal(b))res.push_back(b);
	return res;
}
//テァツキツ堙・ツ按?」ツ?ィテァツつケテ」ツ?ョティツキツ敕ゥツ崢「
double dist_LP(P p1,P p2,P q){
	double dx,dy;
	dx = p1.x-p2.x;
	dy = p1.y-p2.y;
	double d = min(dist(p1,q) ,dist(p2,q));
	pair<P,P> normal = make_pair(q+to_unit(P(-dy,dx))*d ,q+to_unit(P(dy,-dx))*d);//テヲツウツ陛ァツキツ堙」ツδ凖」ツつッテ」ツδ暗」ツδォ
	if(exist_intersection(p1,p2,normal.first,normal.second)){
		return dist(q, intersection(p1,p2,normal.first,normal.second));
	}
	return d;
}
//テァツキツ堙・ツ按?ゥツ鳴禿」ツ?ョティツキツ敕ゥツ崢「
double dist_LL(P p1,P p2,P q1,P q2){
	if(exist_intersection(p1,p2,q1,q2))return 0;
	return min( min(dist_LP(p1,p2,q1),dist_LP(p1,p2,q2))
		,min(dist_LP(q1,q2,p1),dist_LP(q1,q2,p2)) );
}
//テ」ツδゥテ」ツつクテ」ツつ「テ」ツδウテ」ツ?ォテ・ツ、ツ嘉ヲツ渉?
double radian(double angle){
	return angle/180.0*M_PI;
}
//テァツつケpテ」ツつ津ゥツ?堙」ツつ凝・ツ??」ツ?ョテヲツ篠・テァツキツ?
pair<P,P> tangent_CP(double x,double y,double r,P p){
	//P q(x,y), m((x+p.x)/2,(y+p.y)/2);
	double d=sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y))/2;
	return circle_intersection(x,y,r,(x+p.x)/2,(y+p.y)/2,d);
}

int main(){
	int n;
	P w[100];
	double r[100];
	while(cin>>n,n){
		vector<P> wall;
		P p,q;
		for(int i=0;i<n;i++){
			cin>>w[i].x>>w[i].y>>r[i];
		}
		int m;
		cin>>m;
		while(m--){
			P p,q;
			cin>>p.x>>p.y>>q.x>>q.y;
			bool f=false;
			for(int i=0;i<n;i++){
				double d1,d2;
				d1=dist(p,w[i]);
				d2=dist(q,w[i]);
				if(d1<r[i] && r[i]<d2)f=true;
				if(r[i]<d1+EPS && d2<r[i]+EPS)f=true;
				if(r[i]<d1+EPS && r[i]<d2+EPS){
					if(dist_LP(p,q,w[i])<r[i]+EPS)f=true;
				}
			}
			cout<<(f?"Safe":"Danger")<<endl;
		}
	}
	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 1232
final_rank: 9

code_snippet: #include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>

using namespace std;

#define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)

template<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cerr<<*i<<" "; cerr<<endl; }
inline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); }

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
#include <complex>
int sign(double x){
  if(x > EPS) return 1;
  if(x < -EPS) return -1;
  return 0;
}
typedef complex<double> Point;
struct Line : public vector<Point> {
  Line(const Point& a, const Point& b){
    push_back(a); push_back(b);
  }
  Point vector() const {
    return back() - front();
  }
};
struct Circle{
  Point c;
  double r;
  Circle() {}
  Circle(Point c, double r) : c(c), r(r) {}
};

double dot(Point a, Point b){
  return real(conj(a) * b);
}
double cross(Point a, Point b){
  return imag(conj(a) * b);
}

int ccw(Point a, Point b, Point c){
  b -= a; c -= a;
  if(cross(b, c) > EPS) return +1;
  if(cross(b, c) < -EPS) return -1;
  if(dot(b, c) < 0) return +2;
  if(norm(b) < norm(c)) return -2;
  return 0;
}


bool intersectSP(const Line& s, const Point& p){
  return ccw(s[0], s[1], p) == 0;
}

Point projection(const Line& l, const Point& p){
  double t = dot(p - l[0], l.vector()) / norm(l.vector());
  return l[0] + t * l.vector();
}

double distanceSP(const Line& s, const Point& p){
  const Point r = projection(s, p);
  if(intersectSP(s, r)) return abs(r - p);
  return min(abs(s[0] - p), abs(s[1] - p));
}

enum{ OUT, ON, IN };
int contains(const Circle& C, const Point& p){
  double d = abs(C.c - p);
  if(sign(d - C.r) > 0) return OUT;
  if(sign(d - C.r) == 0) return ON;
  return IN;
}

bool intersectCS(const Circle& C, const Line& l){
  int b[2];
  REP(i, 2) b[i] = contains(C, l[i]);
  if(b[0] > b[1]) swap(b[0], b[1]);
  if(b[0] == OUT && b[1] == IN) return true;
  if(b[0] == IN && b[1] == IN) return false;
  if(b[0] == ON) return true;
  double d = distanceSP(l, C.c);
  if(sign(d - C.r) > 0) return false;
  return true;
}

int main(){
  int N;
  while(cin >> N && N){
    vector<Circle> WALL(N);
    REP(i, N) cin >> WALL[i].c.real() >> WALL[i].c.imag() >> WALL[i].r;
    int M;
    cin >> M;
    Point p[2];
    REP(i, M){
      REP(j, 2) cin >> p[j].real() >> p[j].imag();
      bool danger = true;
      REP(j, N) if(intersectCS(WALL[j], Line(p[0], p[1]))) danger = false;
      if(danger) cout << "Danger" << endl;
      else cout << "Safe" << endl;
    }
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1248
final_rank: 5

code_snippet: #include <iostream>
#include <complex>
#include <vector>
#include <algorithm>

#define EPS 1e-7

using namespace std;

typedef complex<double> P;

struct L : public vector<P> {
    L(const P &a, const P &b){
        push_back(a);
        push_back(b);
    }
};

struct C{
    P p;
    double r;
    C(const P &p, double r) : p(p), r(r) { }
};

double dot(const P &a, const P &b){
    return real(conj(a) * b);
}

bool intersectSP(const L &s, const P &p) {
    return abs(s[0]-p)+abs(s[1]-p)-abs(s[1]-s[0]) < EPS; // triangle inequality
}

P projection(const L &l, const P &p) {
  double t = dot(p-l[0], l[0]-l[1]) / norm(l[0]-l[1]);
  return l[0] + t*(l[0]-l[1]);
}

double distanceSP(const L &s, const P &p) {
  const P r = projection(s, p);
  if (intersectSP(s, r)) return abs(r - p);
  return min(abs(s[0] - p), abs(s[1] - p));
}

int main(){
    int n,m;                                     
    double x,y,r;
    double a,b;

    while(cin >> n && n){
        vector<C> circles;
        bool isok = false;
        for(int i=0;i<n;i++){
            cin >> x >> y >> r;
            circles.push_back(C(P(x,y), r));
        }
        cin >> m;
        for(int i=0;i<m;i++){
            isok = false;
            cin >> x >> y;
            cin >> a >> b;
            L line = L(P(x,y), P(a,b));
            for(int j=0;j<n;j++){
                if((abs(line[0]-circles[j].p) < circles[j].r &&
                    abs(line[1]-circles[j].p) < circles[j].r) ||
                   distanceSP(line, circles[j].p) > circles[j].r){
                }else{
                    isok = true;
                }
            }
            if(isok){
                cout << "Safe" << endl;
            }else{
                cout << "Danger" << endl;
            }
        }
    }
}
accuracy: 1
time_ms: 10
memory_kb: 0
final_rank: 1

code_snippet: #define _USE_MATH_DEFINES
#include <iostream>
#include <complex>
#include <algorithm>
#include <vector>
#include <stack>
#include <string>
#include <queue>
#include <cmath>
#include <math.h>
#include <numeric>
#include <list>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <climits>
#include <set>
#include <memory.h>
#include <memory>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cassert>
#include <map>
#include <cassert>
#include <time.h>

using namespace std;

typedef complex<double> xy_t;
typedef pair<xy_t, xy_t> line;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int , P> PP;
typedef pair<int, string> Ps;
typedef vector<int> vec;
typedef vector<vec> mat;
const int INF= 1 << 30;
const double EPS = 1e-9;
const double PI = 3.1415926535897932384626433832795;

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

double cross(const xy_t &a, const xy_t &b){
	return imag(conj(a) * b);
}

double dot(const xy_t &a, const xy_t &b){
	return real(conj(a) * b);
}

xy_t projection(const line &l, const xy_t &p){
	double t = dot(p - l.first, l.first - l.second) / norm(l.first - l.second);
	return l.first + t * (l.first - l.second);
}

bool intersectSP(const line &l, const xy_t &p){
	return abs(l.first- p) + abs(l.second - p) - abs(l.first - l.second) < EPS;
}

double distanceSP(line s, xy_t p){
	const xy_t r = projection(s, p);
	if(intersectSP(s, r)) return abs(r - p);
	return min(abs(s.first - p), abs(s.second - p));
}

xy_t center[200];
double r[200];

int main(){
	int n, m;
	double wx, wy, tx, ty, sx, sy;
	while(cin >> n && n){
		rep(i, n){
			cin>> wx >> wy >> r[i];
			center[i] = xy_t(wx, wy);
		}
		cin >> m;
		rep(i, m){
			cin >> tx >> ty >> sx >> sy;
			line ls = line(xy_t(tx, ty), xy_t(sx, sy));
			bool ok = true;
			rep(j, n){
				if(distanceSP(ls, center[j]) < r[j] + EPS &&
					max(abs(center[j] - ls.first), abs(center[j] - ls.second)) > r[j] - EPS){
						//cout << center[j] << " " << distanceSP(ls, center[j]) << endl;
						ok = false;
				}
			}
			if(!ok) cout << "Safe" << endl;
			else cout << "Danger" << endl;
		}
	}
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 0
final_rank: 1

code_snippet: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
using namespace std;
static const double EPS = 1e-8;
typedef long long ll;
#define rep(i,n) for(int i=0;i<n;i++)
#define rev(i,n) for(int i=n-1;i>=0;i--)
#define sz(a) a.size()
#define all(a) a.begin(),a.end()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define SS stringstream
#define DBG1(a) rep(_X,sz(a)){printf("%d ",a[_X]);}puts("");
#define DBG2(a) rep(_X,sz(a)){rep(_Y,sz(a[_X]))printf("%d ",a[_X][_Y]);puts("");}
#define bitcount(b) __builtin_popcount(b)
#define REP(i, s, e) for ( int i = s; i <= e; i++ )  

const double INF = 1e12;
typedef complex<double> P,point;
typedef vector<P> G,polygon;
namespace std {bool operator < (const P& a, const P& b) {return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);}}
double cross(const P& a, const P& b) {return imag(conj(a)*b);}
double dot(const P& a, const P& b) {return real(conj(a)*b);}
struct L : public vector<P> {L(const P &a, const P &b) {push_back(a); push_back(b);}};
struct C {P p; double r;C(const P &p, double r) : p(p), r(r) { }};
int ccw(P a, P b, P c) {b -= a; c -= a;if (cross(b, c) > 0)   return +1;if (cross(b, c) < 0)   return -1;if (dot(b, c) < 0) return +2;if (norm(b) < norm(c)) return -2;return 0;}

// Check Funcs //
bool intersectLL(const L &l, const L &m) {return abs(cross(l[1]-l[0], m[1]-m[0])) > EPS || abs(cross(l[1]-l[0], m[0]-l[0])) < EPS;}
bool intersectLS(const L &l, const L &s) {return cross(l[1]-l[0], s[0]-l[0])*cross(l[1]-l[0], s[1]-l[0]) < EPS;}
bool intersectLP(const L &l, const P &p) {return abs(cross(l[1]-p, l[0]-p)) < EPS;}
bool intersectSS(const L &s, const L &t) {return ccw(s[0],s[1],t[0])*ccw(s[0],s[1],t[1]) <= 0 && ccw(t[0],t[1],s[0])*ccw(t[0],t[1],s[1]) <= 0;}
bool intersectSP(const L &s, const P &p) {return abs(s[0]-p)+abs(s[1]-p)-abs(s[1]-s[0]) < EPS;}

// Where is Point in Polygon? //
#define curr(P, i) P[i]
#define next(P, i) P[(i+1)%P.size()]
enum { OUT, ON, IN };
int contains(const polygon& P, const point& p) {bool in = false;for (int i = 0; i < P.size(); ++i) {point a = curr(P,i) - p, b = next(P,i) - p;if (imag(a) > imag(b)) swap(a, b);if (imag(a) <= 0 && 0 < imag(b))if (cross(a, b) < 0) in = !in;if (cross(a, b) == 0 && dot(a, b) <= 0) return ON;}return in ? IN : OUT;}
// Area of Polygon //
double area2(const polygon& P) {double A = 0;for (int i = 0; i < P.size(); ++i)A += cross(curr(P, i), next(P, i));return A;}
// Totsuhou! Andrew's Monotone Chain //
vector<point> convex_hull(vector<point> ps) {int n = ps.size(), k = 0;sort(ps.begin(), ps.end());vector<point> ch(2*n);for (int i = 0; i < n; ch[k++] = ps[i++])while (k >= 2 && ccw(ch[k-2], ch[k-1], ps[i]) <= 0) --k;for (int i = n-2, t = k+1; i >= 0; ch[k++] = ps[i--])while (k >= t && ccw(ch[k-2], ch[k-1], ps[i]) <= 0) --k;ch.resize(k-1);return ch;}

P projection(const L &l, const P &p) {double t = dot(p-l[0], l[0]-l[1]) / norm(l[0]-l[1]);return l[0] + t*(l[0]-l[1]);}
P reflection(const L &l, const P &p) {return p + 2.0 * (projection(l, p) - p);}
double distanceLP(const L &l, const P &p) {return abs(p - projection(l, p));}
double distanceLL(const L &l, const L &m) {return intersectLL(l, m) ? 0 : distanceLP(l, m[0]);}
double distanceLS(const L &l, const L &s) {if (intersectLS(l, s)) return 0;return min(distanceLP(l, s[0]), distanceLP(l, s[1]));}
double distanceSP(const L &s, const P &p) {const P r = projection(s, p);if (intersectSP(s, r)) return abs(r - p);return min(abs(s[0] - p), abs(s[1] - p));}
double distanceSS(const L &s, const L &t) {if (intersectSS(s, t)) return 0;  return min(min(distanceSP(s, t[0]), distanceSP(s, t[1])),min(distanceSP(t, s[0]), distanceSP(t, s[1])));}
P crosspoint(const L &l, const L &m) {double A = cross(l[1] - l[0], m[1] - m[0]);double B = cross(l[1] - l[0], l[1] - m[0]);if (abs(A) < EPS && abs(B) < EPS) return m[0];return m[0] + B / A * (m[1] - m[0]);}

P getPedal(L l, P p){
	double A;
	if(abs(l[1].real()-l[0].real()) < EPS){
		return P(l[1].real(),p.imag());
	}else{
		A = (l[1].imag()-l[0].imag())/(l[1].real()-l[0].real());
	}
	double a = -A , b = 1 , c = A*l[0].real() - l[0].imag();
	double t = (a*p.real() + b*p.imag() + c)/(a*a+b*b);
	return p-t * P(a,b);
}

vector<P> crosspointCL(const L l, const C c){
	vector<P> ret;
	P p = getPedal(l,c.p);

	if( (abs(c.p-l[0]) < c.r+EPS && abs(c.p-l[1]) < c.r+EPS) &&
		abs(l[1]-l[0]) < c.r+EPS
	){
			for(int i=0;i<3;i++)ret.push_back(P(0,0));
			return ret;
	}
	
	if(	abs(p-c.p) > c.r+EPS)return ret;
	P e = P((l[1]-l[0])/abs(l[1]-l[0]));
	double S = sqrt(c.r*c.r-abs(p-c.p)*abs(p-c.p));
	ret.push_back(p+S*e);
	ret.push_back(p-S*e);
	return ret;
}
int main(){
	int n;
	while(cin >> n , n){
		vector<C> wall;
		rep(i,n){
			double x,y,r;
			cin >> x >> y >> r;
			wall.push_back(C(P(x,y),r));
		}
		int m; cin >> m;
		rep(i,m){
			double x1,y1,x2,y2;
			cin >> x1 >> y1 >> x2 >> y2;
			P hero(x1,y1) , enemy(x2,y2);
			//cout << hero << " " << enemy << endl;
			L line(hero,enemy);
			bool f = false;
			rep(i,wall.size()){
				vector<P> get = crosspointCL(line,wall[i]);
				if(get.size() != 3){
					rep(j,get.size()){
						//cout << get[j] << endl;
						if(min(x1,x2) <= get[j].real() && get[j].real() <= max(x1,x2) && 
						   min(y1,y2) <= get[j].imag() && get[j].imag() <= max(y1,y2)
						){
							f = true;
						}
					}
				}
			}
			cout << (f?"Safe":"Danger") << endl;
		}
	}
}
accuracy: 1
time_ms: 10
memory_kb: 984
final_rank: 4

code_snippet: #include<vector>
#include<complex>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;

const double EPS = 1e-8;
const double INF = 1e12;
typedef complex<double> P;
namespace std {
  bool operator < (const P& a, const P& b) {
    return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);
  }
}
double cross(const P& a, const P& b) {
  return imag(conj(a)*b);
}
double dot(const P& a, const P& b) {
  return real(conj(a)*b);
}

struct L : public vector<P> {
  L(const P &a, const P &b) {
    push_back(a); push_back(b);
  }
};

typedef vector<P> G;

struct C {
  P p; double r;
  //C(const P &p, double r) : p(p), r(r) { }
};
P projection(const L &l, const P &p) {
  double t = dot(p-l[0], l[0]-l[1]) / norm(l[0]-l[1]);
  return l[0] + t*(l[0]-l[1]);
}

bool intersectSP(const L &s, const P &p) {
  return abs(s[0]-p)+abs(s[1]-p)-abs(s[1]-s[0]) < EPS; // triangle inequality
}

bool inner(P &p, C &c){
	return norm(p-c.p)<(c.r)*(c.r)+EPS;
}
int main(){
	int m,n;
	double t1, t2, t3, t4;
	while(cin>>n,n){
		C wall[n];
		rep(i,n)cin>>t1>>t2>>t3,wall[i].p=P(t1,t2),wall[i].r=t3;
		cin>>m;
		rep(i,m){
			cin>>t1>>t2>>t3>>t4;
			P t=P(t1,t2),s=P(t3,t4),h,tmp;
			L l(t,s);
			bool danger=1;
			rep(j,n){
				h=projection(l,wall[j].p);
				if(intersectSP(l,h)){
					tmp=norm(t-wall[j].p)>norm(s-wall[j].p)?t:s;
					danger=danger&&(inner(tmp,wall[j])==inner(h,wall[j]));
				}else{
					danger=danger&&(inner(t,wall[j])==inner(s,wall[j]));
				}
			}
			cout<<(danger?"Danger":"Safe")<<endl;
		}
	}
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 0
final_rank: 1