这题调精度真痛苦啊(向管理员要了数据才调出来)。
用的是hwd在WC2015上讲的方法,考虑将原图分割,根据每个圆的左右边界和圆与圆交点的横坐标来分割,这样原图就被分成很多竖着的长条,并且每一条中间都没有交点,这样就有一个性质:每一条都是"弓形-梯形-弓形 弓形-梯形-弓形..."的形式,然后从一个方向开始,记录当前进入的圆的数量,每当出来就就计算面积。
/**************************************************************
Problem: 2178
User: idy002
Language: C++
Result: Accepted
Time:1248 ms
Memory:48560 kb
****************************************************************/ #include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define eps 1e-11
#define N 2010
using namespace std; inline int sg( long double x ) { return (x>-eps)-(x<eps); }
struct Vector {
long double x, y;
Vector(){}
Vector( long double x, long double y ):x(x),y(y){}
Vector operator+( const Vector & b ) const { return Vector(x+b.x,y+b.y); }
Vector operator-( const Vector & b ) const { return Vector(x-b.x,y-b.y); }
Vector operator*( long double b ) const { return Vector(x*b,y*b); }
Vector operator/( long double b ) const { return Vector(x/b,y/b); }
long double operator^( const Vector & b ) const { return x*b.y-y*b.x; }
long double operator&( const Vector & b ) const { return x*b.x+y*b.y; }
long double ang() { return atan2(y,x); }
long double len() { return sqrt(x*x+y*y); }
long double len2() { return x*x+y*y; }
};
typedef Vector Point;
struct Circle {
Point c;
long double r;
Circle(){}
Circle( Point c, long double r ):c(c),r(r){}
inline bool in( Point &p ) {
Vector vv=p-c;
return (vv.x*vv.x+vv.y*vv.y) < r*r*0.999;
}
Point pt( long double ang ) const {
return c+Vector(cos(ang),sin(ang))*r;
}
};
struct Arc {
int cid, type;
long double al, ar;
Point pa, pb;
Arc(){}
Arc( int cid, int type, const Point &a, const Point &b );
long double area();
}; int n;
bool del[N], has[N];
long double spt[N*N]; int stot;
Circle cir[N];
Arc arc[N+N]; bool cmp_arc( const Arc &a, const Arc &b ) {
if( sg(a.pa.y-b.pa.y)!= ) return sg(a.pa.y-b.pa.y)<;
if( sg(a.pb.y-b.pb.y)!= ) return sg(a.pb.y-b.pb.y)<;
if( a.type!=b.type ) return a.type>b.type;
if( a.type== ) {
return cir[a.cid].r < cir[b.cid].r;
} else {
return cir[a.cid].r > cir[b.cid].r;
}
}
bool cmp_cir_r( const Circle &a, const Circle &b ) { return a.r<b.r; }
Arc::Arc( int cid, int type, const Point &a, const Point &b ) {
this->cid = cid;
this->type = type;
pa = a;
pb = b;
this->al = (a-cir[cid].c).ang();
this->ar = (b-cir[cid].c).ang();
if( pa.x>pb.x ) swap(pa,pb);
}
long double Arc::area() {
long double da = ar-al;
while( sg(da)<= ) da+=M_PI+M_PI;
while( sg(da-M_PI-M_PI)> ) da-=M_PI+M_PI;
return (da-sin(da))*cir[cid].r*cir[cid].r/2.0;
}
int ccinter( int ca, int cb, Point *p ) {
if( cir[ca].r<cir[cb].r ) swap(ca,cb);
long double cd = (cir[ca].c - cir[cb].c).len2();
long double s1 = cir[ca].r-cir[cb].r;
long double s2 = cir[ca].r+cir[cb].r;
s1=s1*s1, s2=s2*s2; if( sg(cd-s1)< || sg(cd-s2)> ) return ;
if( sg(cd-s1)== || sg(cd-s2)== ) {
long double ang = (cir[cb].c-cir[ca].c).ang();
p[] = cir[ca].pt(ang);
return ;
}
long double r1 = cir[ca].r, r2 = cir[cb].r;
long double base = (cir[cb].c-cir[ca].c).ang();
long double delta = acos( (r1*r1+cd-r2*r2)/(2.0*r1*sqrt(cd)) );
p[] = cir[ca].pt( base-delta );
p[] = cir[ca].pt( base+delta );
return ;
}
bool clinter( int c, long double xl, long double xr, Arc *arc ) {
long double xlb = cir[c].c.x-cir[c].r;
long double xrb = cir[c].c.x+cir[c].r;
if( sg(xlb-xr)>= || sg(xrb-xl)<= ) return false;
Point p = cir[c].c;
long double r = cir[c].r;
long double d1, d2;
long double s1 = r*r-(p.x-xl)*(p.x-xl);
long double s2 = r*r-(p.x-xr)*(p.x-xr);
if( s1<0.0 ) s1=0.0;
if( s2<0.0 ) s2=0.0;
d1 = sqrt( s1 );
d2 = sqrt( s2 );
arc[] = Arc( c, -, Point(xr,p.y+d2), Point(xl,p.y+d1) );
arc[] = Arc( c, +, Point(xl,p.y-d1), Point(xr,p.y-d2) );
return true;
}
long double area( long double lf, long double rg ) {
int m=;
for( int i=; i<n; i++ )
if( clinter(i,lf,rg,arc+m) )
m += ;
sort( arc, arc+m, cmp_arc ); int cc = , top;
long double rt = 0.0;
for( int i=; i<m; i++ ) {
if( cc== )
top = i;
cc += arc[i].type;
if( cc== ) {
rt += (rg-lf)*((arc[i].pa+arc[i].pb).y-(arc[top].pa+arc[top].pb).y)/2.0;
rt += arc[top].area()+arc[i].area();
}
}
return rt;
}
bool cmp_eq( long double x, long double y ) {
return sg(x-y)==;
}
void clean() {
int j=;
for( int i=; i<n; i++ )
if( !del[i] ) cir[j++]=cir[i];
n = j;
}
long double area() {
Point ip[];
int ic;
long double rt = 0.0;
for( int i=; i<n; i++ ) {
for( int j=i+; j<n; j++ ) {
ic = ccinter( i, j, ip );
if( ic<= ) continue;
for( int k=; k<ic; k++ ) {
int q=n;
for( q=; q<n; q++ ) {
if( q==i || q==j ) continue;
if( cir[q].in(ip[k]) )
break;
}
if( q==n ) spt[stot++]=ip[k].x;
}
}
}
for( int i=; i<n; i++ ) {
spt[stot++] = cir[i].c.x-cir[i].r;
spt[stot++] = cir[i].c.x+cir[i].r;
}
sort( spt, spt+stot );
stot = unique( spt, spt+stot, cmp_eq ) - spt; for( int i=; i<stot; i++ )
rt += area( spt[i-], spt[i] );
return rt;
}
void init() {
sort( cir, cir+n, cmp_cir_r );
for( int i=; i<n; i++ )
for( int j=i+; j<n; j++ ) {
long double dx = cir[i].c.x-cir[j].c.x;
long double dy = cir[i].c.y-cir[j].c.y;
long double dij = dx*dx+dy*dy;
long double cc = cir[j].r-cir[i].r;
cc = cc*cc;
if( dij<cc ) del[i]=true;
}
clean();
}
int main() {
scanf( "%d", &n );
for( int i=; i<n; i++ )
scanf( "%Lf%Lf%Lf", &cir[i].c.x, &cir[i].c.y, &cir[i].r );
init();
printf( "%.3Lf\n", area() );
}