首页 > 代码库 > BZOJ 3155 Preprefix sum

BZOJ 3155 Preprefix sum

两个BIT。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 100050
using namespace std;
long long n,m,x,y,t[maxn][3],a[maxn];
char s[12];
long long lowbit(long long x) {return (x&(-x));}
void modify(long long x,long long val,long long type)
{
    for (long long i=x;i<=n;i+=lowbit(i))
        t[i][type]+=val;
}
long long ask(long long x,long long type)
{
    long long ret=0;
    for (long long i=x;i>=1;i-=lowbit(i))
        ret+=t[i][type];
    return ret;
}
int main()
{
    scanf("%lld%lld",&n,&m);
    for (long long i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
        modify(i,a[i],1);modify(i,i*a[i],2);
    }
    for (long long i=1;i<=m;i++)
    {
        scanf("%s",s);
        if (s[0]==M) 
        {
            scanf("%lld%lld",&x,&y);
            modify(x,y-a[x],1);modify(x,x*(y-a[x]),2);
            a[x]=y;
        }
        else
        {
            scanf("%lld",&x);
            printf("%lld\n",(x+1)*ask(x,1)-ask(x,2));
        }
    }
    return 0;
}

 

BZOJ 3155 Preprefix sum