AcWing 3069. 圆的面积并(自适应辛普森积分)

题目链接:点击这里

题目大意:
给出 n n n 个圆的圆心和半径,求这 n n n 个圆的面积并

题目分析:
自适应辛普森积分
考虑构造 f ( x ) f(x) f(x) 表示横坐标为 x x x 时的纵坐标长度
然后套上辛普森积分的板子即可

具体细节见代码:

//#pragma GCC optimize(2)
//#pragma GCC optimize("Ofast","inline","-ffast-math")
//#pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<queue>
#define ll long long
#define inf 0x3f3f3f3f
//#define int  ll
#define endl '\n'
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
using namespace std;
int read()
{
	int res = 0,flag = 1;
	char ch = getchar();
	while(ch<'0' || ch>'9')
	{
		if(ch == '-') flag = -1;
		ch = getchar();
	}
	while(ch>='0' && ch<='9')
	{
		res = (res<<3)+(res<<1)+(ch^48);//res*10+ch-'0';
		ch = getchar();
	}
	return res*flag;
}
const int maxn = 1e3+5;
const int mod = 998244353;
const double pi = acos(-1);
const double eps = 1e-8;
struct Point {
	double x,y;
	bool operator<(const Point &b) const {
		return x==b.x ? y < b.y : x < b.x;
	}
}p[maxn];
struct Circle {
	Point p; double r;
}c[maxn];
int n,m;
int dcmp(double x){
	if(fabs(x) < eps) return 0;
	if(x > 0) return 1;
	return -1;
}
double f(double x)
{
	int cnt = 0;
	for(int i = 1;i <= n;i++)
	{
		double dx = fabs(c[i].p.x-x),r = c[i].r;
		if(r-dx <= eps) continue;
		double d = sqrt(r*r-dx*dx);
		p[++cnt] = {c[i].p.y-d,c[i].p.y+d};
	}
	if(!cnt) return 0;
	sort(p+1,p+cnt+1);
	double l = p[1].x,r = p[1].y,res = 0;
	for(int i = 2;i <= cnt;i++)
	{
		if(p[i].x <= r) r = max(r,p[i].y);
		else {
			res += r-l;
			l = p[i].x,r = p[i].y;
		}
	}
	res += r-l; 
	return res;
}
double simpson(double l,double r)
{
	return (f(l)+f(r)+f((l+r)/2)*4)*(r-l)/6;
}
double solve(double l,double r,double ans)
{
	double mid = (l+r)/2;
	double ls = simpson(l,mid),rs = simpson(mid,r);
	if(fabs(ls+rs-ans) < eps) return ls+rs;
	return solve(l,mid,ls)+solve(mid,r,rs); 
}
int main() 
{
	n = read();
	for(int i = 1;i <= n;i++) c[i].p.x = read(),c[i].p.y = read(),c[i].r = read();
	printf("%.3f\n",solve(-2000,2000,simpson(-2000,2000)));
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值