农场阳光 Posted on 2019-04-23 | Edited on 2019-06-12 | In 蓝桥杯 | Views: 题意求多个圆和一个矩形的面积并 题解使用自适应 Simpson 求解 代码123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105#include <bits/stdc++.h>#define forl(i, l, r) for (int i = l; i <= r; i++)#define forr(i, r, l) for (int i = r; i >= l; i--)#define for1(i, n) for (int i = 1; i <= n; i++)#define for0(i, n) for (int i = 0; i < n; i++)#define meminf(a) memset(a, inf, sizeof(a))#define mem_1(a) memset(a, -1, sizeof(a))#define mem0(a) memset(a, 0, sizeof(a))#define inlld(lld) scanf("%lld", &lld)#define inlf(f) scanf("%lf", &f)#define ind(d) scanf("%d", &d)#define ins(s) scanf("%s", s)#define mp make_pair#define pb push_back#define fi first#define se secondtypedef std::pair<long long, long long> pll;typedef std::vector<long long> vll;typedef std::pair<int, int> pii;typedef unsigned long long ull;typedef std::vector<int> vii;typedef long double db;typedef long long ll;const db pi = acos((db)-1);const ll inf =0x3f3f3f3f;const ll mod = 1e9+7;const int N = 1.1e5;const db eps = 1e-8;using namespace std;int sign(db a) { return a < -eps ? -1 : a > eps; }int db_cmp(db a, db b){ return sign(a-b); }db g;int n,a,b;struct circle{ db x,y,r; circle(){} circle(int a,int b,int c,int d){ x=a+c/tan(g); y=b; r=d; }}cir_set[N];struct seg{ db top,bottom; seg(){} seg(db a,db b){ top=a; bottom=b; }};bool cmp(seg x,seg y){ return x.bottom<y.bottom;}db F(db x){ vector<seg>v; for0(i,n){ circle ci=cir_set[i]; if(fabs(x-ci.x)<ci.r){ db h=sqrt(ci.r*ci.r-(x-ci.x)*(x-ci.x)); v.pb(seg(ci.y+h,ci.y-h)); } } if(v.size()==0)return b; v.pb(seg(0,b)); sort(v.begin(),v.end(),cmp); db sta=0,ans=0; for0(i,v.size()){ seg se=v[i]; if(se.bottom>sta){ ans+=se.bottom-sta; } sta=max(sta,se.top); if(sta>=b)break; } return ans;}// 2db calc(db len,db fL,db fM,db fR){ //求长度为len的[L,R]区间,中点为M的Simpson近似面积 return (fL+4*fM+fR)*len/6;}db Simpson(db L,db R) {//Simpson积分求区间[L,R]的面积并,F(L)=L,F(R)=R,F(M)=M,把[L,R]当成整体来拟合得到的面积是sqr db M=(L+R)/2,fL=F(L),fM=F(M),fR=F(R),sqr=calc(R-L,fL,fM,fR); db g1=calc(M-L,fL,F((L+M)/2),fM),g2=calc(R-M,fM,F((M+R)/2),fR); if(fabs(sqr-g1-g2)<=eps) //把当前区间分成2半再拟合得到的答案差别很小,就不再递归下去了 return g1+g2; return Simpson(L,M)+Simpson(M,R);}int main() {#ifdef PerpEternal // freopen("/Users/perpeternal/Documents/Sketch/data/in.dat", "r", stdin); // freopen("/Users/perpeternal/Documents/Sketch/data/out.dat", "w", stdout);#endif //ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); scanf("%d%d%Lf%d",&a,&b,&g,&n); g=g*pi/180; for0(i,n){ int x,y,z,r; scanf("%d%d%d%d",&x,&y,&z,&r); cir_set[i]=circle(x,y,z,r); // cout<<cir_set[i].x<<' '<<cir_set[i].y<<' '<<cir_set[i].r<<endl; } printf("%.2Lf\n",Simpson(0,a)); return 0;}