3069. 圆的面积并

题目链接

3069. 圆的面积并

给出 \(N\) 个圆,求其面积并。

输入格式

第一行一个整数 \(N\)

接下来 \(N\) 行每行包含三个整数 \(x,y,r\),表示其中一个圆的圆心坐标为 \((x,y)\),半径为 \(r\)

输出格式

输出一个实数,表示面积并。

输出结果与标准答案的绝对误差在 \(10^{-2}\) 以内,即视为正确。

数据范围

\(1 \le N \le 1000\),
\(-1000 \le x,y \le 1000\),
\(0 \le r \le 1000\)

输入样例:

3
0 0 1000
1 0 1000
2 0 1000

输出样例:

3145592.653

解题思路

自适应辛普森积分

对于一条竖线来说其覆盖圆的长度是连续的,其表示的函数连续,可以用辛普森积分来处理,其函数值 \(x\) 这条竖线跟圆的交集长度

另外,有一个优化精度的方式,即可能并不是所有的圆的位置都是连续的,即可能在 \(x\) 方向上很多圆是一块一块放置的,不妨先将所有块预处理出来,然后再对每一块执行自适应辛普森积分

  • 时间复杂度:\(O(玄学)\)

代码

// Problem: 圆的面积并
// Contest: AcWing
// URL: https://www.acwing.com/problem/content/3072/
// Memory Limit: 64 MB
// Time Limit: 5000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

// %%%Skyqwq
#include <bits/stdc++.h>
 
//#define int long long
#define help {cin.tie(NULL); cout.tie(NULL);}
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
using namespace std;
 
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
 
template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; }
template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; }
 
template <typename T> void inline read(T &x) {
    int f = 1; x = 0; char s = getchar();
    while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
    while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
    x *= f;
}

const int N=1005;
const double eps=1e-8;
typedef pair<double,double> PDD;
int n;
PDD a[N];
struct Circle
{
	PDD p;
	double r;
}circle[N];
int dcmp(double x,double y)
{
	if(fabs(x-y)<eps)return 0;
	if(x<y)return -1;
	return 1;
}
double f(double x)
{
	double res=0;
	int cnt=0;
	for(int i=1;i<=n;i++)
	{
		double X=fabs(x-circle[i].p.fi),R=circle[i].r;
		if(dcmp(X,R)<0)
		{
			double Y=sqrt(R*R-X*X);
			a[++cnt]={circle[i].p.se-Y,circle[i].p.se+Y};	
		}
	}
	sort(a+1,a+1+cnt);
	double st=a[1].fi,ed=a[1].se;
	for(int i=2;i<=cnt;i++)
		if(a[i].fi<=ed)ed=max(ed,a[i].se);
		else
		{
			res+=ed-st;
			st=a[i].fi,ed=a[i].se;
		}
	res+=ed-st;
	return res;
}
double simpson(double l,double r)
{
	double mid=(l+r)/2;
	return (f(l)+f(mid)*4+f(r))/6*(r-l);
}
double asr(double l,double r,double s)
{
	double mid=(l+r)/2;
	double L=simpson(l,mid),R=simpson(mid,r);
	if(fabs(s-L-R)<eps)return L+R;
	return asr(l,mid,L)+asr(mid,r,R);
}
int main()
{
	scanf("%d",&n);
	double l=2000,r=-2000;
    for(int i=1;i<=n;i++)
    {
    	scanf("%lf%lf%lf",&circle[i].p.fi,&circle[i].p.se,&circle[i].r);
    	l=min(l,circle[i].p.fi-circle[i].r);
    	r=max(r,circle[i].p.fi+circle[i].r);
    }
    printf("%.3lf",asr(l,r,simpson(l,r)));
    return 0;
}
posted @ 2022-11-23 19:38  zyy2001  阅读(20)  评论(0编辑  收藏  举报