树状数组 HDU 1166

tree[i] 代表以i为根的和

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<set>
#include<string>
#include<map>

using namespace std;
typedef long long LL;

#define inf  2000000000
#define MAXN 50010

int tree[MAXN];

int lowbit(int t)
{
    return t&(-t);
}
void Insert(int n,int t,int d)
{
    while(t<=n)
    {
        tree[t]+=d;
        t=t+lowbit(t);
    }
}
LL Ques(int t)
{
    LL ans=0;
    while(t>0)
    {
        ans+=tree[t];
        t=t-lowbit(t);
    }
    return ans;
}
int main()
{
    int t,ca=1;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        memset(tree,0,sizeof(tree));
        for(int i=1;i<=n;i++)
        {
            int a;
            scanf("%d",&a);
            Insert(n,i,a);
        }
        printf("Case %d:\n",ca++);
        while(1)
        {
            char s[10];
            scanf("%s",s);
            if(s[0]=='E')
                break;
            int a,b;
            scanf("%d%d",&a,&b);
            if(s[0]=='A')
                Insert(n,a,b);
            else if(s[0]=='S')
            {
                Insert(n,a,-b);
            }
            else
            {
                printf("%lld\n",Ques(b)-Ques(a-1));
            }
        }

    }
    return 0;
}

 

posted on 2017-02-24 13:02  HelloWorld!--By-MJY  阅读(131)  评论(0编辑  收藏  举报

导航