HDU 1724 Ellipse
Problem Description
Math is important!! Many students failed in 2+2’s mathematical test, so let's AC this problem to mourn for our lost youth..
Look this sample picture:
![](http://acm.hdu.edu.cn/data/images/C61-1005-1.JPG)
A ellipses in the plane and center in point O. the L,R lines will be vertical through the X-axis. The problem is calculating the blue intersection area. But calculating the intersection area is dull, so I have turn to you, a talent of programmer. Your task is tell me the result of calculations.(defined PI=3.14159265 , The area of an ellipse A=PI*a*b )
Look this sample picture:
A ellipses in the plane and center in point O. the L,R lines will be vertical through the X-axis. The problem is calculating the blue intersection area. But calculating the intersection area is dull, so I have turn to you, a talent of programmer. Your task is tell me the result of calculations.(defined PI=3.14159265 , The area of an ellipse A=PI*a*b )
Input
Input
may contain multiple test cases. The first line is a positive integer
N, denoting the number of test cases below. One case One line. The line
will consist of a pair of integers a and b, denoting the ellipse
equation
, A pair of integers l and r, mean the L is (l, 0) and R is (r, 0). (-a <= l <= r <= a).
Output
For
each case, output one line containing a float, the area of the
intersection, accurate to three decimals after the decimal point.
Sample Input
2
2 1 -2 2
2 1 0 2
Sample Output
6.283
3.142
自适应辛普森的模板
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<cmath> 6 using namespace std; 7 double pi=acos(-1.0); 8 double a,b; 9 double F(double x) 10 { 11 return b*sqrt(1-x*x/(a*a)); 12 } 13 double simpson(double l,double r) 14 { 15 return (r-l)*(F(l)+4*F((l+r)/2.0)+F(r))/6.0; 16 } 17 double asr(double l,double r,double eps,double A) 18 { 19 double mid=(l+r)/2.0; 20 double LS=simpson(l,mid),RS=simpson(mid,r); 21 if (fabs(LS+RS-A)<=15*eps) return LS+RS+(LS+RS-A)/15.0; 22 return asr(l,mid,eps/2,LS)+asr(mid,r,eps/2,RS); 23 } 24 int main() 25 {double eps=1e-6; 26 int T; 27 double l,r; 28 cin>>T; 29 while (T--) 30 { 31 scanf("%lf%lf%lf%lf",&a,&b,&l,&r); 32 printf("%.3lf\n",asr(l,r,eps,simpson(l,r))*2.0); 33 } 34 }