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);
}
}
}