辛普森积分学习笔记

又是咕了很久的东西。

 

柿子很简单啦 就是这个

$(f(l)+f(r)+4*f(mid))*(r-l)/6$

 

然后有时候大部分时候需要自适应

具体见代码吧。

 

洛谷模板1

//Love and Freedom.
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define ll long long
#define inf 20021225
#define db double
using namespace std;
int read()
{
    int s=0,f=1; char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-') f=-1; ch=getchar();}
    while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
    return f*s;
}
db a,b,c,d;
db f(db x){return (c*x+d)/(a*x+b);}
db simpson(db l,db r)
{
    db mid=(l+r)/2;
    return (f(l)+4*f(mid)+f(r))*(r-l)/6;
}
db asr(db l,db r,db eps,db ans)
{
    db mid=(l+r)/2;
    db _l=simpson(l,mid),_r=simpson(mid,r);
    if(abs(_l+_r-ans)<=15*eps)    return _l+_r+(_l+_r-ans)/15;
    return asr(l,mid,eps/2,_l)+asr(mid,r,eps/2,_r);
}
db asr(db l,db r,db eps)
{
    return asr(l,r,eps,simpson(l,r));
}
db l,r;
int main()
{
    scanf("%lf%lf%lf%lf%lf%lf",&a,&b,&c,&d,&l,&r);
    printf("%.6lf",asr(l,r,1e-6));
    return 0;
}
View Code

 

洛谷模板2

//Love and Freedom.
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define ll long long
#define inf 20021225
#define db double
#define il inline
using namespace std;
int read()
{
    int s=0,f=1; char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-') f=-1; ch=getchar();}
    while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
    return f*s;
}
db a;
il db f(db x){return pow(x,a/x-x);}
il db simpson(db l,db r)
{
    db mid=(l+r)/2;
    return (f(l)+4*f(mid)+f(r))*(r-l)/6;
}
il db asr(db l,db r,db eps,db ans)
{
    db mid=(l+r)/2;
    db _l=simpson(l,mid),_r=simpson(mid,r);
    if(abs(_l+_r-ans)<=15*eps)    return _l+_r+(_l+_r-ans)/15;
    return asr(l,mid,eps/2,_l)+asr(mid,r,eps/2,_r);
}
il db asr(db l,db r,db eps)
{
    return asr(l,r,eps,simpson(l,r));
}
int main()
{
    scanf("%lf",&a);
    if(a<0)    return puts("orz"),0;
    printf("%.5lf",asr(1e-9,20,5e-8));
    return 0;
}
View Code

 

圆的面积并(eps死活调不好了 弃疗了)

//Love and Freedom.
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define ll long long
#define inf 20021225
#define db long double
#define eps 3e-15
#define N 4010
#define pa pair<db,db>
#define mp make_pair
#define fs first
#define se second
using namespace std;
int read()
{
    int s=0,f=1; char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-') f=-1; ch=getchar();}
    while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
    return f*s;
}
struct poi
{
    db x,y;
    poi(){}
    poi(db _x,db _y){x=_x,y=_y;}
}p[N];
typedef poi vec;
poi operator+(poi a,poi b){return poi(a.x+b.x,a.y+b.y);}
poi operator-(poi a,poi b){return poi(a.x-b.x,a.y-b.y);}
db cross(poi a,poi b){return a.x*b.y-a.y*b.x;}
db len(vec a){return sqrtl(a.x*a.x+a.y*a.y);}
db dist(poi a,poi b){return len(b-a);}
struct circle{poi o; db r;}c[N];
int sgn(db x){return x<-eps?-1:(x>eps?1:0);}
int n;
pa v[N];
db f(db x)
{
    int cnt=0;
    for(int i=1;i<=n;i++)
    {
        db l=fabs(c[i].o.x-x);
        if(l-c[i].r<-eps)
        {
            db d=sqrtl(c[i].r*c[i].r-l*l);
            v[++cnt]=mp(c[i].o.y-d,c[i].o.y+d);
        }
    }
    sort(v+1,v+cnt+1); int i=1; db ans=0.0;
    while(i<=cnt)
    {
        db k=v[i].fs,mx=v[i].se; i++;
        while(i<=cnt && sgn(v[i].fs-mx)<=0)
            mx=max(mx,v[i].se),i++;
        ans+=mx-k;
    }
    return ans;
}
db simpson(db l,db r)
{
    db mid=(l+r)/2.0;
    return (f(l)+4.0*f(mid)+f(r))*(r-l)/6.0;
}
db asr(db l,db r,db Eps,db ans)
{
    db mid=(l+r)/2.0;
    db l_=simpson(l,mid),r_=simpson(mid,r);
    if(fabs(l_+r_-ans)<=Eps*15.0)    return l_+r_-(l_+r_-ans)/15.0;
    return asr(l,mid,Eps/2.0,l_)+asr(mid,r,Eps/2.0,r_);
}
db asr(db l,db r,db Eps)
{
    return asr(l,r,Eps,simpson(l,r));
}
bool tag[N];
bool cmp(circle x,circle y){return x.r<y.r;}
int main()
{
    n=read(); db mn=inf,mx=-inf;
    for(int i=1;i<=n;i++)
        c[i].o.x=read(),c[i].o.y=read(),c[i].r=read(),
        mx=max(mx,c[i].o.x+c[i].r),mn=min(mn,c[i].o.x-c[i].r);
    sort(c+1,c+n+1,cmp);
    for(int i=1;i<=n;i++)
        for(int j=i+1;j<=n;j++)
            if(sgn(dist(c[i].o,c[j].o)-(c[j].r-c[i].r))<1)
            {
                tag[i]=1; break;
            }
    int nn=0;
    for(int i=1;i<=n;i++)    if(!tag[i])
        c[++nn]=c[i];
    n=nn;
    printf("%.3Lf\n",asr(mn,mx,1e-5));
    return 0;
}
View Code

 

圆的异或并(跟圆的面积并没有本质区别 不写了(大雾))

posted @ 2019-10-09 15:34  寒雨微凝  阅读(210)  评论(0编辑  收藏  举报