【玲珑学院OJ1063】Variance(线段树)

记录一个菜逼的成长。。

比赛的时候看到这题,一看就是经典线段树。。然而方差公式已经还给老师了。。。一脸懵逼。
现在补上。
方差公式D(x) = E(x^2) - E(x)^2;
E是平均数
因为本题要求结果乘上(r-l+1)^2
所以ans = (r-l+1)^2 * (E(x^2) - E(x)^2) = (r-l+1)*sigma(x^2) - sigma(x)*sigma(x)(l <= x <= r);
所以线段树结点只要保存区间和和区间平方和。

//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <deque>
#include <cctype>
#include <bitset>
#include <cmath>
#include <cassert>
using namespace std;
#define ALL(v) (v).begin(),(v).end()
#define cl(a,b) memset(a,b,sizeof(a))
#define bp __builtin_popcount
#define pb push_back
#define mp make_pair
#define fin freopen("D://in.txt","r",stdin)
#define fout freopen("D://out.txt","w",stdout)
#define lson t<<1,l,mid
#define rson t<<1|1,mid+1,r
#define seglen(t) (node[t].r-node[t].l+1)
#define pi 3.1415926
#define exp  2.718281828459
#define lowbit(x) (x)&(-x)
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
typedef vector<PII> VPII;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
template <typename T>
inline void read(T &x){
    T ans=0;
    char last=' ',ch=getchar();
    while(ch<'0' || ch>'9')last=ch,ch=getchar();
    while(ch>='0' && ch<='9')ans=ans*10+ch-'0',ch=getchar();
    if(last=='-')ans=-ans;
    x = ans;
}
const int maxn = 100000 + 10;
int a[maxn];
LL sum,sum2;
struct Node {
    int l,r;
    LL sum2,sum;
}node[maxn*4];
void pushup(int t)
{
    node[t].sum = node[t<<1].sum + node[t<<1|1].sum;
    node[t].sum2 = node[t<<1].sum2 + node[t<<1|1].sum2;
}
void build(int t,int l,int r)
{
    node[t].l = l;
    node[t].r = r;
    if( l == r ){
        node[t].sum = (LL)a[l];
        node[t].sum2 = (LL)a[l] * a[l];
        return ;
    }
    int mid = (l + r)>>1;
    build(lson);
    build(rson);
    pushup(t);
}
void update(int t,int ind, int c)
{
    if(node[t].l == node[t].r && node[t].l == ind){
        node[t].sum = c;
        node[t].sum2 = c*c;
        return ;
    }
    int mid = (node[t].l + node[t].r) >> 1;
    if(ind <= mid)update(t<<1,ind,c);
    else update(t<<1|1,ind,c);
    pushup(t);
}
void query(int t,int l,int r)
{
    if(node[t].l == l && node[t].r == r){
        sum += node[t].sum;
        sum2 += node[t].sum2;
        return ;
    }
    int mid = (node[t].l + node[t].r) >> 1;
    if(r <= mid)query(t<<1,l,r);
    else if(l > mid)query(t<<1|1,l,r);
    else {
        query(t<<1,l,mid);
        query(t<<1|1,mid+1,r);
    }
}
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m)){
        for( int i = 1; i <= n; i++ ){
            scanf("%d",a+i);
        }
        build(1,1,n);
        while(m--){
            int ope,l,r;
            scanf("%d%d%d",&ope,&l,&r);
            if(ope == 1){
                update(1,l,r);
            }
            else {
                sum = sum2 = 0;
                query(1,l,r);
                LL ans = (r - l + 1) * sum2 - sum * sum;
                printf("%lld\n",ans);
            }
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值