HDU - 1724 Ellipse 自适应辛普森模板

OJ

题解传送门

//Achen
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
int T;
double a,b,l,r; 
using namespace std;

double f(double x) {
    return b*sqrt(1.0-(x*x)/(a*a));
}

double sim(double x,double y) {
    double mid=((x+y)/2.0);
    return  (y-x)/6.0*(f(x)+4.0*f(mid)+f(y));
}

double calc(double l,double r,double eps) {
    double mid=((l+r)/2.0);
    double tp=sim(l,mid)+sim(mid,r),tpp=sim(l,r);
    if(tp-tpp<=eps*15.0) return tp+(tp-tpp)/15.0;
    else return calc(l,mid,eps/2.0)+calc(mid,r,eps/2.0);
}

int main() {
    scanf("%d",&T);
    while(T--) {
        scanf("%lf%lf%lf%lf",&a,&b,&l,&r);
        printf("%.3lf\n",calc(l,r,1e-5)*2.0); 
    }
    return 0;
}
View Code

 

这里还有一道自适应辛普森模板

传送门

posted @ 2017-12-25 17:09  啊宸  阅读(138)  评论(0编辑  收藏  举报