[模板 辛普森积分] Grazed Grains | NCPC2021 | 辛普森积分求圆的面积并

Posted PushyTao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[模板 辛普森积分] Grazed Grains | NCPC2021 | 辛普森积分求圆的面积并相关的知识,希望对你有一定的参考价值。

评测地址可以到:传送门

题目描述

This year, there have been unusually many UFO sightings reported. Nobody knows if they are caused by optical illusions, weather phenomena, or secret technology being tested by foreign powers (terrestrial or not). UFO enthusiasts across the world rejoice, and speculations run wild.
But someone who is not impressed is the farmer Celeste. Her large grain field has repeatedly been used as a landing site by the UFOs, destroying her crops. Celeste would like to bring whoever is responsible to justice, but before she can do so she must assess the damage caused by the unidentified flying offenders.
In total n circular UFOs have landed in Celeste’s field. The ith one left a crop circle which destroyed all crops within radius r i of the point x i , y i . The field is very large, so you can assume that it extends infinitely in all directions. Your task is to estimate the total area of crops destroyed by the UFOs. Celeste only needs a rough estimate of the true answer, and your answer will be counted as correct if its relative error is less than 10%.

输入

The first line of input contains one integer n (1 ≤ n ≤ 10), the number of UFOs. Then follow n lines, the ith of which contains the three integers xi , yi , and ri (0 ≤ xi , yi ≤ 10, 1 ≤ ri ≤ 10),
describing the crop circle left by the ith UFO.

输出

Output the total area covered by the crop circles in the input, with a relative error of at most 1/10.

样例输入 Copy

【样例1】

1
0 0 1

【样例2】

4
0 0 2
1 0 1
2 2 2
10 10 1

样例输出 Copy

【样例13.1415926535
【样例225.991148

学习参考自博客
模板来自博客

struct Point {
	double x,y;
	Point() {}
	Point(double x,double y):x(x),y(y) {}
	bool operator < (const Point &p)const {
		return x<p.x;
	}
} q[maxn];
struct circle {
	double x,y,r;
	bool operator < (const circle &p)const {
		return r>p.r;
	}
} a[maxn];
const double eps = 1e-8, inf = 1e20, Pi = acos(-1);
int n;
#define sqr(x) (x)*(x)
double f(double x) {
	int top=0;
	for(int i=1; i<=n; i++) if(fabs(x-a[i].x)<a[i].r) {
			double len=sqrt(sqr(a[i].r)-sqr(x-a[i].x));
			q[++top]=Point(a[i].y-len,a[i].y+len);
		}
	double l=-inf,r=-inf,ret=0;
	sort(q+1,q+1+top);
	for(int i=1; i<=top; i++)
		if(q[i].x>r) ret+=r-l,l=q[i].x,r=q[i].y;
		else r=max(r,q[i].y);
	return ret+=r-l;
}
double Simpson(double L,double M,double R,double fL,double fM,double fR,int dep) {
	double M1=(L+M)/2,M2=(M+R)/2,fM1=f(M1),fM2=f(M2);
	double g1=(M-L)*(fL+4*fM1+fM)/6, g2=(R-M)*(fM+4*fM2+fR)/6, g=(R-L)*(fL+4*fM+fR)/6;
	if(dep>11&&fabs(g-g1-g2)<1e-8) return g;
	return Simpson(L,M1,M,fL,fM1,fM,dep+1)+Simpson(M,M2,R,fM,fM2,fR,dep+1);
}
inline double dist(circle &a,circle &b) {
	return sqrt(sqr(a.x-b.x)+sqr(a.y-b.y));
}
void prework() {
	int cnt=0;
	sort(a+1,a+1+n);
	for(int i=1; i<=n; i++) {
		for(int j=1; j<=cnt; j++) if(a[i].r+dist(a[i],a[j])<=a[j].r) goto A;
		a[++cnt]=a[i];
A:
		;
	}
	n=cnt;
}
int main() {
	scanf("%d",&n);
	double l=inf,r=-inf,mid;
	for(int i=1; i<=n; i++) scanf("%lf%lf%lf",&a[i].x,&a[i].y,&a[i].r),l=min(l,a[i].x-a[i].r),r=max(r,a[i].x+a[i].r);
	mid=(l+r)/2;
	prework();
	printf("%.3f",Simpson(l,mid,r,f(l),f(mid),f(r),0));
	return 0;
}

以上是关于[模板 辛普森积分] Grazed Grains | NCPC2021 | 辛普森积分求圆的面积并的主要内容,如果未能解决你的问题,请参考以下文章

计算几何_求面积_辛普森积分公式

复合辛普森公式求积分

Learning辛普森积分

辛普森积分

Matlab数值积分

并不对劲的辛普森积分