码迷,mamicode.com
首页 > 其他好文 > 详细

BZOJ 3155 Preprefix sum

时间:2016-10-22 09:41:41      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:return   name   modify   har   turn   space   ++   type   algorithm   

两个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

标签:return   name   modify   har   turn   space   ++   type   algorithm   

原文地址:http://www.cnblogs.com/ziliuziliu/p/5986696.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!