BZOJ4821 SDOI2017相关分析(线段树)
纯粹的码农题。维护x的和、y的和、xy的和、x2的和即可。可能会炸long long。
#include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> #include<cstring> #include<algorithm> using namespace std; #define ll long double #define N 100010 char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<'0'||c>'9')) c=getchar();return c;} int gcd(int n,int m){return m==0?n:gcd(m,n%m);} int read() { int x=0,f=1;char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();} while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar(); return x*f; } int n,m,a[N],b[N],L[N<<2],R[N<<2]; ll lazyaddx[N<<2],lazyaddy[N<<2],s[N],t[N]; bool lazyres[N<<2]; struct data { ll sumx,sumy,sumxy,sqr; data operator +(const data&a) const { data c; c.sumx=sumx+a.sumx; c.sumy=sumy+a.sumy; c.sumxy=sumxy+a.sumxy; c.sqr=sqr+a.sqr; return c; } }tree[N<<2]; void up(int k){tree[k]=tree[k<<1]+tree[k<<1|1];} void update(int k,ll x,ll y,bool p) { if (p) { tree[k].sumx=tree[k].sumy=s[R[k]]-s[L[k]-1]; tree[k].sumxy=tree[k].sqr=t[R[k]]-t[L[k]-1]; lazyaddx[k]=lazyaddy[k]=0,lazyres[k]=1; } tree[k].sqr+=x*x*(R[k]-L[k]+1)+2*x*tree[k].sumx; tree[k].sumxy+=x*tree[k].sumy; tree[k].sumx+=(R[k]-L[k]+1)*x; tree[k].sumxy+=y*tree[k].sumx; tree[k].sumy+=(R[k]-L[k]+1)*y; lazyaddx[k]+=x,lazyaddy[k]+=y; } void down(int k) { update(k<<1,lazyaddx[k],lazyaddy[k],lazyres[k]); update(k<<1|1,lazyaddx[k],lazyaddy[k],lazyres[k]); lazyaddx[k]=lazyaddy[k]=0;lazyres[k]=0; } void build(int k,int l,int r) { L[k]=l,R[k]=r; if (l==r) {tree[k].sumx=a[l],tree[k].sumy=b[l],tree[k].sumxy=1ll*a[l]*b[l],tree[k].sqr=1ll*a[l]*a[l];return;} int mid=l+r>>1; build(k<<1,l,mid); build(k<<1|1,mid+1,r); up(k); } data query(int k,int l,int r) { if (L[k]==l&&R[k]==r) return tree[k]; if (lazyaddx[k]||lazyaddy[k]||lazyres[k]) down(k); int mid=L[k]+R[k]>>1; if (r<=mid) return query(k<<1,l,r); else if (l>mid) return query(k<<1|1,l,r); else return query(k<<1,l,mid)+query(k<<1|1,mid+1,r); } void res(int k,int l,int r) { if (L[k]==l&&R[k]==r) {update(k,0,0,1);return;} if (lazyaddx[k]||lazyaddy[k]||lazyres[k]) down(k); int mid=L[k]+R[k]>>1; if (r<=mid) res(k<<1,l,r); else if (l>mid) res(k<<1|1,l,r); else res(k<<1,l,mid),res(k<<1|1,mid+1,r); up(k); } void add(int k,int l,int r,int x,int y) { if (L[k]==l&&R[k]==r) {update(k,x,y,0);return;} if (lazyaddx[k]||lazyaddy[k]||lazyres[k]) down(k); int mid=L[k]+R[k]>>1; if (r<=mid) add(k<<1,l,r,x,y); else if (l>mid) add(k<<1|1,l,r,x,y); else add(k<<1,l,mid,x,y),add(k<<1|1,mid+1,r,x,y); up(k); } int main() { #ifndef ONLINE_JUDGE freopen("bzoj4821.in","r",stdin); freopen("bzoj4821.out","w",stdout); const char LL[]="%I64d\n"; #else const char LL[]="%lld\n"; #endif n=read(),m=read(); for (int i=1;i<=n;i++) a[i]=read(); for (int i=1;i<=n;i++) b[i]=read(); for (int i=1;i<=n;i++) s[i]=s[i-1]+i,t[i]=t[i-1]+1ll*i*i; build(1,1,n); while (m--) { int op=read(); if (op==1) { int l=read(),r=read(); data k=query(1,l,r); ll x=(ll)k.sumx/(r-l+1),y=(ll)k.sumy/(r-l+1); printf("%.6lf\n",(double)((k.sumxy-k.sumx*y-k.sumy*x+x*y*(r-l+1))/(k.sqr-2*k.sumx*x+x*x*(r-l+1)))); } else { int l=read(),r=read(),x=read(),y=read(); if (op==3) res(1,l,r);add(1,l,r,x,y); } } return 0; }