ybtoj&洛谷P3268:圆的异或并(扫描线)

解析

很神奇的一道题。

关键条件:任意两个圆无交
把一个圆分成上下两个圆弧,那么所有圆弧的高度关系不会发生变化。
所以可以开一个 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_JUDGE
	freopen("a.in","r",stdin);
	freopen("a.out","w",stdout);
#endif
	n=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 3
  7 1 4 1 9
  1 3 5 3
  1 1 4 2
  2 3 5
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值