hdu1166(单点更新,区间求和)

http://acm.hdu.edu.cn/showproblem.php?pid=1166

代码:

#include<iostream>
using namespace std;
int n,a[50005],c[50005];
int lowbit(int x)
{
    return x&(-x);
}
int sum(int x)
{
    int sum=0;
    while(x>0)
    {
        sum+=c[x];
        x=x-lowbit(x);
    }
    return sum;
}
void inster(int i,int j)
{
    while(i<=n)
    {
        c[i]+=j;
        i=i+lowbit(i);
    }
}
int main()
{
    char s[10];
    int i,j,t,k,f=0;
    scanf("%d",&t);
    while(t--)
    {
        memset(c,0,sizeof(c));
        scanf("%d",&n);
        for(i=1;i<=n;i++)
            scanf("%d",&a[i]);
        getchar();
        for(i=1;i<=n;i++)
        {
            j=lowbit(i);
            for(k=i-j+1;k<=i;k++)
                c[i]+=a[k];
        }
    printf("Case %d:\n",++f);
    while(1)
    {
        scanf("%s",s);
        if(s[0]=='E')
            break;
        scanf("%d %d",&i,&j);
        getchar();
        if(s[0]=='A')
                inster(i,j);
            else
                if(s[0]=='S')
                    inster(i,-j);
                else
                    if(s[0]=='Q')
                    {
                        if(i==1)
                        printf("%d\n",sum(j));
                        else
                            printf("%d\n",sum(j)-sum(i-1));
                    }
    }
    }
    return 0;
}

 

posted @ 2012-11-22 15:23  紫忆  阅读(173)  评论(0编辑  收藏  举报