洛谷T2005 区间方差(线段树)

T2005 区间方差

题目描述
给定一个序列b,以及两种操作C,x,y,
C=1:将b[x]的值变为y,
C=1:询问区间[x,y]的方差。

分析:线段树维护区间和以及区间平方和即可。

代码

#include <cstdio>
#define N 400005
#define mo 1000000007
#define ll long long
using namespace std;

struct arr
{
	int l,r;
	ll sum,sq;
}tr[N];
ll a[N],sum1,sum2;
int n,m;

void pushup(int p)
{
	tr[p].sum = (tr[p * 2].sum + tr[p * 2 + 1].sum) % mo;
	tr[p].sq = (tr[p * 2].sq + tr[p * 2 + 1].sq) % mo;
}

void build(int p, int l, int r)
{
	tr[p].l = l;
	tr[p].r = r;
	if (l == r)
	{
		tr[p].sum = a[l];
		tr[p].sq = a[l] * a[l] % mo;
		return;
	}
	int mid = (l + r) / 2;
	build(p * 2, l, mid);
	build(p * 2 + 1, mid + 1, r);
	pushup(p);
}

void change(int p, int x, ll y)
{
	if (tr[p].l == x && tr[p].r == x)
	{
		tr[p].sq = y * y % mo;
		tr[p].sum = y;
		return;
	}
	int mid = (tr[p].l + tr[p].r) / 2;
	if (x <= mid) change(p * 2, x, y);
		else change(p * 2 + 1, x, y);
	pushup(p);
}

void find(int p, int x, int y)
{
	if (tr[p].l == x && tr[p].r == y)
	{
		sum1 = (sum1 + tr[p].sum) % mo;
		sum2 = (sum2 + tr[p].sq) % mo;
		return;
	}
	int mid = (tr[p].l + tr[p].r) / 2;
	if (y <= mid) find(p * 2, x, y);
		else if (x > mid) find(p * 2 + 1, x, y);
			else find(p * 2, x, mid), find(p * 2 + 1, mid + 1, y);
	pushup(p);
}

ll ksm(ll x, ll y)
{
	ll base = x, r = 1;
	while (y)
	{
		if (y & 1) r = (r * base) % mo;
		base = (base * base) % mo;
		y /= 2;
	}
	return r;
}

int main()
{
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
	build(1, 1, n);
	while (m--)
	{
		int c, x;
		ll y;
		scanf("%d%d%lld", &c, &x, &y);
		if (c == 1) change(1, x, y);
		if (c == 2) 
		{
			ll ans = 0;
			sum1 = sum2 = 0;
			find(1, x, (int)y);
			ll ny = ksm(y - x + 1, mo - 2);
			ans = (ans + sum2) % mo;
			ans = (ans + sum1 * sum1 % mo * ny % mo) % mo;
			ans = (ans - 2ll * sum1 % mo * sum1 % mo * ny % mo + mo) % mo;
			ans = ans * ny % mo;
			printf("%lld\n", ans);
 		}
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值