解析
很神奇的一道题。
关键条件:任意两个圆无交。
把一个圆分成上下两个圆弧,那么所有圆弧的高度关系不会发生变化。
所以可以开一个 set
,维护一个从左往右扫的扫描线,按照当前扫描线的横坐标定义比较符号,在圆的最左处把圆弧放进去,再在最右边把圆弧删掉。每次插入时找到前驱,即可确定其被包含的圆的层数。
注意精度!
代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define debug(...) fprintf(stderr,__VA_ARGS__)
#define ok debug("OK\n")
using namespace std;const int N=4e5+100;
const int M=50050;
const int mod=1e9+7;
const double eps=1e-9;inline ll read() {ll x(0),f(1);char c=getchar();while(!isdigit(c)) {if(c=='-')f=-1;c=getchar();}while(isdigit(c)) {x=(x<<1)+(x<<3)+c-'0';c=getchar();}return x*f;
}int n,m;double x[N],y[N],r[N];
double now;
int num[N];struct cir {int id,op;cir(int a=0,int b=0) {id=a;op=b;}double h(){return y[id]+op*(sqrt(r[id]*r[id]-(x[id]-now)*(x[id]-now))+eps);}
};
bool operator < (cir a,cir b) {return a.h()<b.h();
}
set<cir>s;
struct ope {int x,id,op;bool operator < (const ope oth) {return x<oth.x;}
} o[N];
int tot;int main() {
#ifndef ONLINE_JUDGEfreopen("a.in","r",stdin);freopen("a.out","w",stdout);
#endifn=read();for(int i=1; i<=n; i++) {scanf("%lf%lf%lf",&x[i],&y[i],&r[i]);o[++tot]=(ope) {(int)(x[i]-r[i]),i,1};o[++tot]=(ope) {(int)(x[i]+r[i]),i,-1};}sort(o+1,o+1+tot);for(int i=1; i<=tot; i++) {now=o[i].x;if(o[i].op==1) {//printf("ins: %d\n",o[i].id);set<cir>::iterator it=s.insert((cir) {o[i].id,1}).first; if(it!=s.begin()) {it--;cir c=*it;//printf(" pre=%d\n",c.id);num[o[i].id]=num[c.id]^(c.op==-1);}s.insert((cir){o[i].id,-1});}else {s.erase((cir) {o[i].id,1});s.erase((cir) {o[i].id,-1});}}ll ans=0;for(int i=1; i<=n; i++) {if(num[i]==0) ans+=r[i]*r[i];else ans-=r[i]*r[i];//printf("i=%d num=%d\n",i,num[i]);}printf("%lld\n",ans);return 0;
}
/*5 37 1 4 1 91 3 5 31 1 4 22 3 5
*/