3069. 圆的面积并

题目链接

3069. 圆的面积并

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

输入格式

第一行一个整数 N

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

输出格式

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

输出结果与标准答案的绝对误差在 102 以内,即视为正确。

数据范围

1N1000,
1000x,y1000,
0r1000

输入样例:

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; }

__EOF__

本文作者acwing_zyy
本文链接https://www.cnblogs.com/zyyun/p/16919568.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   zyy2001  阅读(27)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示