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

本文详细介绍了如何使用线段树进行区间更新与查询,包括处理加法、平方和乘法运算,以及如何在区间内进行高效的操作。通过具体的代码实现,展示了线段树在复杂操作中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

线段树

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

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

我会因式分解!

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

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

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

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

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

代码:

#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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值