敌兵布阵 树状数组

 

 

↑杭电oj ----1166

#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN=50005;
int c[MAXN],t,i,a,b,n,k;
int lowbit(int id){
        return id&-id;
}
void add(int a,int b){
        while(a<=n)
        {
                c[a]+=b;
                a+=lowbit(a);
        }
}
int sum(int x){
        int res=0;
        while(x>0)
        {
                res+=c[x];
                x-=lowbit(x);
        }
        return res;
}
int main()
{
        char m[10];
        int l=1;
        scanf("%d",&t);
        while(t--)
        {
                scanf("%d",&n);
                memset(c,0,sizeof(c));
                for(i=1;i<=n;i++)
                {
                        scanf("%d",&k);
                        add(i,k);
                }
                printf("Case %d:\n",l);
                l++;
                while(scanf("%s",m)!=EOF&&m[0]!='E')
                {
                        if(m[0]=='Q')
                        {
                                scanf("%d%d",&a,&b);
                                printf("%d\n",sum(b)-sum(a-1));
                        }
                        else if(m[0]=='A')
                        {
                                scanf("%d%d",&a,&b);
                                add(a,b);
                        }
                        else if(m[0]=='S')
                        {
                                scanf("%d%d",&a,&b);
                                add(a,-b);
                        }
                }
        }
        return 0;
}

posted @ 2019-04-12 16:40  痞卡皮  阅读(141)  评论(0编辑  收藏  举报