P1471 0pts
  • 板块学术版
  • 楼主How1ver
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/8/6 00:01
  • 上次更新2024/8/6 09:36:25
查看原帖
P1471 0pts
510823
How1ver楼主2024/8/6 00:01
#include <bits/stdc++.h>
using namespace std;
struct TRee
{
	int l,r;
	double sum,lz,pf;
}t[4000005];
int n,q;
double a[1000005];
void push_down(int p)
{
	if (t[p].lz)
	{
		t[p*2].pf+=2*t[p].lz*t[p*2].sum+(t[p*2].r-t[p*2].l+1)*t[p].lz*t[p].lz;
        t[p*2+1].pf+=2*t[p].lz*t[p*2+1].sum+(t[p*2+1].r-t[p*2+1].l+1)*t[p].lz*t[p].lz;
		t[p*2].sum+=t[p].lz*(t[p*2].r-t[p*2].l+1);
		t[p*2+1].sum+=t[p].lz*(t[p*2+1].r-t[p*2+1].l+1);
		t[p*2].lz+=t[p].lz;
		t[p*2+1].lz+=t[p].lz;
		t[p].lz=0;
	}
}
void push_up(int p)
{
	t[p].sum=t[p*2].sum+t[p*2+1].sum;
	t[p].pf=t[p*2].pf+t[p*2+1].pf;
}
void buildSGM(int p,int l,int r)
{
	t[p].l=l; t[p].r=r;
	if (l==r)
	{
		t[p].sum=a[l];
		t[p].pf=a[l]*a[l];
		return ;
	}
	int mid =(l+r)/2;
	buildSGM(p*2,l,mid);
	buildSGM(p*2+1,mid+1,r);
	push_up(p);
}
void changequ(int p,int l,int r,double x)
{
	if (l<=t[p].l&&r>=t[p].r)
	{
		t[p].lz+=x;
		t[p].pf+=2*x*t[p].sum+x*x*(r-l+1);
		t[p].sum+=(t[p].r-t[p].l+1)*x;
		return;
	}
	push_down(p);
	int mid=(t[p].l+t[p].r)/2;
	if (l<=mid) changequ(p*2,l,r,x);
	if (r>mid) changequ(p*2+1,l,r,x);
	push_up(p);
}
double ask1(int p,int l,int r)
{
	if (l<=t[p].l&&r>=t[p].r)
	{
		return t[p].sum;
	}
	push_down(p);
	int mid=(t[p].l+t[p].r)/2;
	double val=0;
	if (l<=mid) val=val+ask1(p*2,l,r);
	if (r>mid) val=val+ask1(p*2+1,l,r);
	return val;
}
double ask2(int p,int l,int r)
{
	if (l<=t[p].l&&r>=t[p].r)
	{
		return t[p].pf;
	}
	push_down(p);
	int mid=(t[p].l+t[p].r)/2;
	double val=0;
	if (l<=mid) val=val+ask2(p*2,l,r);
	if (r>mid) val=val+ask2(p*2+1,l,r);
	return val;
}
int main()
{
	cin>>n>>q;
	for (int i=1;i<=n;i++) cin>>a[i];
	buildSGM(1,1,n);
	for (int i=1;i<=q;i++) 
	{
		int a,x,y;
		double z;
		cin>>a;
		if (a==1)
		{
			cin>>x>>y>>z;
			changequ(1,x,y,z);
		}
		if (a==2)
		{
			cin>>x>>y;
			double ans=1.0*ask1(1,x,y)/(y-x+1);
			printf("%.4lf\n",ans);
		}
		if (a==3)
		{
			cin>>x>>y;
			double sm1=1.0*ask2(1,x,y)/(y-x+1),sm2=1.0*ask1(1,x,y)/(y-x+1);
			cout<<sm1<<' '<<sm2<<'\n';
			double answ=sm1-sm2*sm2;
			printf("%.4lf\n",answ);
		}
	}
	return 0;
}
2024/8/6 00:01
加载中...