线段树全WA求调
  • 板块P1471 方差
  • 楼主_Give_up_
  • 当前回复6
  • 已保存回复6
  • 发布时间2022/9/12 21:57
  • 上次更新2023/10/27 11:46:46
查看原帖
线段树全WA求调
516468
_Give_up_楼主2022/9/12 21:57

样例过了,但是0分

#include<bits/stdc++.h>
#define N 100010

using namespace std;

struct rec
{
	double sum,ans,lazy,l,r;
}t[N<<2];

int a[N];

void pushup(int p)
{
	t[p].sum = t[p<<1].sum+t[p<<1|1].sum;
	t[p].ans = t[p<<1].ans+t[p<<1|1].ans;
}

void pushdown(int p)
{
	if (t[p].lazy)
    {
    	int len = t[p].r-t[p].l+1;
        t[p<<1].ans += 2*t[p].lazy*t[p<<1].sum+(len-len/2)*t[p].lazy*t[p].lazy;
        t[p<<1|1].ans += 2*t[p].lazy*t[p<<1|1].sum+(len/2)*t[p].lazy*t[p].lazy;
        t[p<<1].sum += (len-len/2)*t[p].lazy;
        t[p<<1|1].sum += (len/2)*t[p].lazy;
        t[p<<1].lazy += t[p].lazy;
        t[p<<1|1].lazy += t[p].lazy;
    	t[p].lazy = 0;
    }
}

void build(int l,int r,int p)
{
	t[p].l=l,t[p].r=r;
	if (l==r)
	{
		t[p].sum = a[l];
		t[p].ans = a[l]*a[l];
		return ;
	}
	int mid = (l+r)>>1;
	build(l,mid,p<<1);
	build(mid+1,r,p<<1|1);
	pushup(p);
}

void update(int l,int r,int start,int end,int p,double k)
{
	if (start<=l && r<=end)
	{
		t[p].lazy += k;
		t[p].ans += 2*k*t[p].sum+k*k*(r-l+1);
		t[p].sum += k*(r-l+1);
		return ;
	}
	int mid = (l+r)>>1;
	pushdown(p);
	if (start<=mid) update(l,mid,start,end,p<<1,k);
	if (end>mid) update(mid+1,r,start,end,p<<1|1,k);
	pushup(p);
}

double query1(int l,int r,int start,int end,int p)
{
	if (start<=l && r<=end) return t[p].sum;
	int mid = (l+r)>>1;
	pushdown(p);
	double ans=0;
	if (start<=mid) ans += query1(l,mid,start,end,p<<1);
	if (end>mid) ans += query1(mid+1,r,start,end,p<<1|1);
	return ans;
}

double query2(int l,int r,int start,int end,int p)
{
	if (start<=l && r<=end) return t[p].ans;
	int mid = (l+r)>>1;
	pushdown(p);
	double ans=0;
	if (start<=mid) ans += query2(l,mid,start,end,p<<1);
	if (end>mid) ans += query2(mid+1,r,start,end,p<<1|1);
	return ans;
}

int main()
{
	int n,m;
	cin >> n >> m;
	for (int i=1;i<=n;i++)
		cin >> a[i];
	build(1,n,1);
	while(m--)
	{
		int opt;
		cin >> opt;
		if (opt==1)
		{
			int l,r;
			double k;
			cin >> l >> r >> k;
			update(1,n,l,r,1,k);
		}
		else if (opt==2)
		{
			int l,r;
			cin >> l >> r;
			printf("%.4f\n",query1(1,n,l,r,1)/(r-l+1));
		}
		else
		{
			int l,r;
			cin >> l >> r;
			double d = query1(1,n,l,r,1)/(r-l+1);
			printf("%.4f\n",query2(1,n,l,r,1)/(r-l+1)-d*d);
		}
	}
	return 0;
}
2022/9/12 21:57
加载中...