单点更新以及查询区间和,树状数组水过。
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int n;
int a[50005];
int lowbit(int x)
{
return x&(-x);
}
int sum(int pos)
{
int ans=0;
while(pos>0)
{
ans+=a[pos];
pos-=lowbit(pos);
}
return ans;
}
void push(int pos,int num)
{
while(pos<=n)
{
a[pos]+=num;
pos+=lowbit(pos);
}
}
int main()
{
int T,k,c=1;;
int x,y;
char ord[10];
scanf("%d",&T);
while(T--)
{
printf("Case %d:\n",c++);
memset(a,0,sizeof(a));
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&k);
push(i,k);
}
while(1)
{
scanf("%s",ord);
if(ord[0]=='Q')
{
scanf("%d%d",&x,&y);
printf("%d\n",sum(y)-sum(x-1));
}
if(ord[0]=='A')
{
scanf("%d%d",&x,&y);
push(x,y);
}
if(ord[0]=='S')
{
scanf("%d%d",&x,&y);
push(x,-y);
}
if(ord[0]=='E') break;
}
}
return 0;
}