bzoj 3155: Preprefix sum 树状数组

题意

这里写图片描述
1<=N,M<=100000,且在任意时刻0<=Ai<=100000

分析

唉果然像我这种弱鸡就只能切切这种大家都会的sb题了。
把答案的式子展开后用树状数组维护一下对应项就好了。

代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;

typedef long long LL;

const int N=100005;

int n,m,a[N];
LL c1[N],c2[N];

int read()
{
    int x=0,f=1;char ch=getchar();
    while (ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

void ins1(int x,LL y)
{
    while (x<=n) c1[x]+=y,x+=x&(-x);
}

void ins2(int x,LL y)
{
    while (x<=n) c2[x]+=y,x+=x&(-x);
}

LL query1(int x)
{
    LL ans=0;
    while (x) ans+=c1[x],x-=x&(-x);
    return ans;
}

LL query2(int x)
{
    LL ans=0;
    while (x) ans+=c2[x],x-=x&(-x);
    return ans;
}

int main()
{
    n=read();m=read();
    for (int i=1;i<=n;i++) a[i]=read(),ins1(i,a[i]),ins2(i,(LL)a[i]*i);
    while (m--)
    {
        char ch[10];
        scanf("%s",ch);
        if (ch[0]=='M')
        {
            int x=read(),y=read();
            ins1(x,-a[x]);ins2(x,(LL)-a[x]*x);
            a[x]=y;
            ins1(x,a[x]);ins2(x,(LL)a[x]*x);
        }
        else
        {
            int x=read();
            LL s1=query1(x),s2=query2(x);
            printf("%lld\n",(LL)s1*(x+1)-s2);
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值