BZOJ4821: [Sdoi2017]相关分析(洛谷P3707)

版权声明:蒟蒻Blog随意转载 https://blog.csdn.net/a1799342217/article/details/82940916

线段树

BZOJ题目传送门
洛谷题目传送门

这道题最大的难点在于会爆long long和卡精度

我会因式分解!

首先因为要计算 x ˉ \bar x y ˉ \bar y ,肯定要记 x \sum x y \sum y 。把求 a a 的公式展开来,发现上面有 x i y i \sum x_iy_i ,下面有 x i 2 \sum x_i^2 ,都把它们记下来。

对于2,3操作, x i \sum x_i y i \sum y_i 很好维护,主要就是维护 x i 2 \sum x_i^2 x i y i \sum x_iy_i 。以前者为例(后者类似):

对于2操作, x i 2 \sum x_i^2 变成 ( x i + s ) 2 \sum (x_i+s)^2 。展开得 x i 2 + 2 s x i + s 2 \sum x_i^2+2s\sum x_i+\sum s^2 ,也就是加上 2 s x i + s 2 2s\sum x_i+\sum s^2

对于3操作变成 ( s + i ) 2 \sum(s+i)^2 。同样展开得 i 2 + 2 s i + s 2 \sum i^2+2s\sum i+\sum s^2 ,直接算就好了。

注意更新的先后顺序以及区间覆盖的时候要去掉区间修改的标记。

代码:

#include<cctype>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 100005
#define F inline
using namespace std;
typedef double DB;
struct tree{ DB sx,sy,s1,s2,l,r,fx,fy,tx,ty; }t[N<<2];
int n,m; DB a[N],b[N];
F char readc(){
	static char buf[100000],*l=buf,*r=buf;
	if (l==r) r=(l=buf)+fread(buf,1,100000,stdin);
	return l==r?EOF:*l++;
}
F int _read(){
	int x=0,f=1; char ch=readc();
	while (!isdigit(ch)){ if (ch=='-') f=-1; ch=readc(); }
	while (isdigit(ch)) x=(x<<3)+(x<<1)+(ch^48),ch=readc();
	return x*f;
}
#define calc(n) ((n)*(n+1)*(2*(n)+1)/6)
#define cal(n) ((n)*(n+1)/2)
F void add1(int x,DB S,DB T){
	DB d=t[x].r-t[x].l+1;
	t[x].s1+=t[x].sx*S*2+S*S*d;
	t[x].s2+=S*t[x].sy+T*t[x].sx+S*T*d;
	t[x].fx+=S,t[x].fy+=T,t[x].sx+=S*d,t[x].sy+=T*d;
}
F void add2(int x,DB S,DB T){
	DB d=t[x].r-t[x].l+1,s=cal(t[x].r)-cal(t[x].l-1);
	t[x].sx=S*d+s,t[x].sy=T*d+s;
	t[x].s1=calc(t[x].r)-calc(t[x].l-1)+S*s*2+S*S*d;
	t[x].s2=calc(t[x].r)-calc(t[x].l-1)+(S+T)*s+S*T*d;
	t[x].tx=S,t[x].ty=T,t[x].fx=t[x].fy=0;
}
F void pshp(int x){
	int l=x<<1,r=x<<1|1;
	t[x].s1=t[l].s1+t[r].s1,t[x].s2=t[l].s2+t[r].s2;
	t[x].sx=t[l].sx+t[r].sx,t[x].sy=t[l].sy+t[r].sy;
}
F void pshd(int x){
	if (t[x].tx||t[x].ty) add2(x<<1,t[x].tx,t[x].ty),add2(x<<1|1,t[x].tx,t[x].ty);
	if (t[x].fx||t[x].fy) add1(x<<1,t[x].fx,t[x].fy),add1(x<<1|1,t[x].fx,t[x].fy);
	t[x].tx=t[x].ty=t[x].fx=t[x].fy=0;
}
void build(int x,int l,int r){
	t[x].l=l,t[x].r=r; int mid=l+r>>1;
	if (l==r) return void(t[x]=(tree){a[l],b[l],a[l]*a[l],a[l]*b[l],l,r});
	build(x<<1,l,mid),build(x<<1|1,mid+1,r),pshp(x);
}
void mdfy1(int x,int l,int r,DB S,DB T){
	if (t[x].l>r||t[x].r<l) return;
	if (t[x].l>=l&&t[x].r<=r) return add1(x,S,T);
	pshd(x),mdfy1(x<<1,l,r,S,T),mdfy1(x<<1|1,l,r,S,T),pshp(x);
}
void mdfy2(int x,int l,int r,DB S,DB T){
	if (t[x].l>r||t[x].r<l) return;
	if (t[x].l>=l&&t[x].r<=r) return add2(x,S,T);
	pshd(x),mdfy2(x<<1,l,r,S,T),mdfy2(x<<1|1,l,r,S,T),pshp(x);
}
tree srch(int x,int l,int r){
	if (t[x].l>=l&&t[x].r<=r) return t[x];
	tree tmp={0,0,0,0},ls,rs;
	if (t[x].l>r||t[x].r<l) return tmp;
	pshd(x),ls=srch(x<<1,l,r),rs=srch(x<<1|1,l,r);
	tmp.s1=ls.s1+rs.s1,tmp.s2=ls.s2+rs.s2;
	tmp.sx=ls.sx+rs.sx,tmp.sy=ls.sy+rs.sy;
	return tmp;
}
int main(){
	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 (build(1,1,n);m;m--){
		int f=_read(),l=_read(),r=_read(),s,t;
		if (f==1){
			tree tmp=srch(1,l,r);
			DB xx=tmp.sx/(r-l+1),yy=tmp.sy/(r-l+1);
			DB nx=tmp.s1-xx*tmp.sx*2+xx*xx*(r-l+1);
			DB ny=tmp.s2-xx*tmp.sy-yy*tmp.sx+xx*yy*(r-l+1);
			printf("%.10lf\n",ny/nx); continue;
		}
		s=_read(),t=_read(),f==2?mdfy1(1,l,r,s,t):mdfy2(1,l,r,s,t);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/a1799342217/article/details/82940916