农场阳光

题意

求多个圆和一个矩形的面积并

题解

使用自适应 Simpson 求解

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#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 second
typedef 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;
}
// 2
db 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;
}