[题解]
/* WA://50分 last:(r-l+1)<-- (r-mid) (r-l+1)<-- (mid-l+1) now:int mid=l+r>>1; tr[lc].sxy+=tr[lc].sx*addy[k]+tr[lc].sy*addx[k]+(real)(mid-l+1)*addx[k]*addy[k]; tr[lc].sx2+=(real)(mid-l+1)*addx[k]*addx[k]+2.0*tr[lc].sx*addx[k]; tr[lc].sx+=(real)(mid-l+1)*addx[k]; tr[lc].sy+=(real)(mid-l+1)*addy[k]; tr[rc].sxy+=tr[rc].sx*addy[k]+tr[rc].sy*addx[k]+(real)(r-mid)*addx[k]*addy[k]; tr[rc].sx2+=(real)(r-mid)*addx[k]*addx[k]+2.0*tr[rc].sx*addx[k]; tr[rc].sx+=(real)(r-mid)*addx[k]; tr[rc].sy+=(real)(r-mid)*addy[k]; addx[k]=addy[k]=0; */ #include<cmath> #include<cstdio> #include<cstdlib> #include<iostream> #define pf(x)((x)*(x)) #define lc k<<1 #define rc k<<1|1 #define setfile(x) freopen(#x".in","r",stdin);freopen(#x".out","w",stdout); using namespace std; typedef double real; const real inf=2e9; const int N=2e5+5; const int M=1e3+5; int n,m,bsize; real ax[N],ay[N]; struct sgt{real sx,sy,sxy,sx2;}tr[N<<2]; real addx[N<<2],addy[N<<2]; real setx[N<<2],sety[N<<2]; bool tag[N<<2]; inline int read(){ int x=0,f=1;char ch=getchar(); while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} return x*f; } sgt operator +(const sgt &a,const sgt &b){ sgt c; c.sx=a.sx+b.sx; c.sy=a.sy+b.sy; c.sxy=a.sxy+b.sxy; c.sx2=a.sx2+b.sx2; return c; } inline real sumsq1(real l,real r){return (l+r)*(r-l+1)/2;} inline real sumsq2(real l,real r){return (r*(r+1)*(r*2+1)-(l-1)*l*(l*2-1))/6;} void updata(int k){ tr[k].sx=tr[lc].sx+tr[rc].sx; tr[k].sy=tr[lc].sy+tr[rc].sy; tr[k].sxy=tr[lc].sxy+tr[rc].sxy; tr[k].sx2=tr[lc].sx2+tr[rc].sx2; } void build(int k,int l,int r){ // tag[k]=0; if(l==r){ tr[k].sx=ax[l]; tr[k].sy=ay[l]; tr[k].sxy=ax[l]*ay[l]; tr[k].sx2=ax[l]*ax[l]; return ; } int mid=l+r>>1; build(lc,l,mid); build(rc,mid+1,r); updata(k); } void pushdown(int k,int l,int r){ if(l==r) return ; int mid=l+r>>1; if(addx[k]!=0||addy[k]!=0){ if(tag[lc]){ setx[lc]+=addx[k]; sety[lc]+=addy[k]; } else{ addx[lc]+=addx[k]; addy[lc]+=addy[k]; } if(tag[rc]){ setx[rc]+=addx[k]; sety[rc]+=addy[k]; } else{ addx[rc]+=addx[k]; addy[rc]+=addy[k]; } int mid=l+r>>1; tr[lc].sxy+=tr[lc].sx*addy[k]+tr[lc].sy*addx[k]+(real)(mid-l+1)*addx[k]*addy[k]; tr[lc].sx2+=(real)(mid-l+1)*addx[k]*addx[k]+2.0*tr[lc].sx*addx[k]; tr[lc].sx+=(real)(mid-l+1)*addx[k]; tr[lc].sy+=(real)(mid-l+1)*addy[k]; tr[rc].sxy+=tr[rc].sx*addy[k]+tr[rc].sy*addx[k]+(real)(r-mid)*addx[k]*addy[k]; tr[rc].sx2+=(real)(r-mid)*addx[k]*addx[k]+2.0*tr[rc].sx*addx[k]; tr[rc].sx+=(real)(r-mid)*addx[k]; tr[rc].sy+=(real)(r-mid)*addy[k]; addx[k]=addy[k]=0; } if(tag[k]){ tag[lc]=tag[rc]=1; setx[lc]=setx[k]; setx[rc]=setx[k]; sety[lc]=sety[k]; sety[rc]=sety[k]; real li1,li2,ri1,ri2; li1=sumsq1(l,mid); li2=sumsq2(l,mid); ri1=sumsq1(mid+1,r); ri2=sumsq2(mid+1,r); tr[lc].sxy=setx[k]*sety[k]*(real)(mid-l+1); tr[lc].sx2=setx[k]*setx[k]*(real)(mid-l+1); tr[lc].sx=setx[k]*(real)(mid-l+1)+li1; tr[lc].sy=sety[k]*(real)(mid-l+1)+li1; tr[lc].sxy+=li1*(setx[k]+sety[k])+li2; tr[lc].sx2+=2*li1*setx[k]+li2; tr[rc].sxy=setx[k]*sety[k]*(real)(r-mid); tr[rc].sx2=setx[k]*setx[k]*(real)(r-mid); tr[rc].sx=setx[k]*(real)(r-mid)+ri1; tr[rc].sy=sety[k]*(real)(r-mid)+ri1; tr[rc].sxy+=ri1*(setx[k]+sety[k])+ri2; tr[rc].sx2+=2*ri1*setx[k]+ri2; tag[k]=0; } } void change(int k,int l,int r,int x,int y,real s,real t){ if(l==x&&r==y){ tr[k].sxy+=tr[k].sx*t+tr[k].sy*s+(real)(r-l+1)*s*t; tr[k].sx2+=(real)(r-l+1)*s*s+2.0*tr[k].sx*s; tr[k].sx+=(real)(r-l+1)*s; tr[k].sy+=(real)(r-l+1)*t; if(tag[k]){ setx[k]+=s; sety[k]+=t; } else{ addx[k]+=s; addy[k]+=t; } return ; } pushdown(k,l,r); int mid=l+r>>1; if(y<=mid) change(lc,l,mid,x,y,s,t); else if(x>mid) change(rc,mid+1,r,x,y,s,t); else change(lc,l,mid,x,mid,s,t),change(rc,mid+1,r,mid+1,y,s,t); updata(k); } void cover(int k,int l,int r,int x,int y,real s,real t){ if(l==x&&r==y){ real li1,li2; li1=sumsq1(l,r); li2=sumsq2(l,r); tr[k].sxy=s*t*(real)(r-l+1); tr[k].sx2=s*s*(real)(r-l+1); tr[k].sx=s*(real)(r-l+1)+li1; tr[k].sy=t*(real)(r-l+1)+li1; tr[k].sxy+=li1*(s+t)+li2; tr[k].sx2+=2*li1*s+li2; tag[k]=1; setx[k]=s; sety[k]=t; addx[k]=addy[k]=0; return ; } pushdown(k,l,r); int mid=l+r>>1; if(y<=mid) cover(lc,l,mid,x,y,s,t); else if(x>mid) cover(rc,mid+1,r,x,y,s,t); else cover(lc,l,mid,x,mid,s,t),cover(rc,mid+1,r,mid+1,y,s,t); updata(k); } sgt query(int k,int l,int r,int x,int y){ if(l==x&&r==y) return tr[k]; pushdown(k,l,r); int mid=l+r>>1; if(y<=mid) return query(lc,l,mid,x,y); else if(x>mid) return query(rc,mid+1,r,x,y); else return query(lc,l,mid,x,mid)+query(rc,mid+1,r,mid+1,y); } real find(int x,int y){ real X=0,Y=0,XY=0,X2=0,Xp,Yp,fz=0,fm=0,A; sgt ans=query(1,1,n,x,y);int l=x,r=y; X=ans.sx; Y=ans.sy; XY=ans.sxy; X2=ans.sx2; Xp=X/(real)(r-l+1); Yp=Y/(real)(r-l+1); fz=XY-X*Yp-Xp*Y+Xp*Yp*(r-l+1); fm=X2-2.0*X*Xp+Xp*Xp*(r-l+1); // if(fz==0||fm==0) return 0; return fz/fm; } void work(){ build(1,1,n); real s,t; for(int i=1,opt,x,y;i<=m;i++){ opt=read();x=read();y=read(); if(opt==1) printf("%.7lf\n",find(x,y)); if(opt==2) scanf("%lf%lf",&s,&t),change(1,1,n,x,y,s,t); if(opt==3) scanf("%lf%lf",&s,&t),cover(1,1,n,x,y,s,t); } } int main(){ setfile(relative); n=read();m=read(); for(int i=1;i<=n;i++) scanf("%lf",&ax[i]); for(int i=1;i<=n;i++) scanf("%lf",&ay[i]); work(); fclose(stdin); fclose(stdout); return 0; }
时间: 2024-12-29 16:57:21