问题:http://acm.hdu.edu.cn/showproblem.php?pid=1166
解决方案:适合用二叉索引树解决
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
const int MAXN=50005;
LL cnum[MAXN];
int lowbit(int x)
{
return x&(-x);
}
LL sum_array(int x)
{
LL csum=0;
while(x!=0)
{
csum+=cnum[x];
x-=lowbit(x);
}
return csum;
}
void addj(int i,int j,int n)
{
while(i<=n)
{
cnum[i]+=j;
i+=lowbit(i);
}
}
int main()
{
int t;
scanf("%d",&t);
for(int i=0;i<t;i++)
{
bool flag=true;
int n;
scanf("%d",&n);
memset(cnum,0,sizeof(LL)*(n+1));
for(int j=1;j<=n;j++)
{
int num;
scanf("%d",&num);
addj(j,num,n);
}
char cmd[10];
while(scanf("%s",cmd)!=EOF&&strcmp(cmd,"End"))
{
int x,y;
scanf("%d%d",&x,&y);
switch(cmd[0])
{
case 'S':
addj(x,-y,n);
break;
case 'A':
addj(x,y,n);
break;
case 'Q':
if(flag)
{
printf("Case %d:\n",i+1);
flag=false;
}
printf("%lld\n",sum_array(y)-sum_array(x-1));
break;
}
}
if(flag)
{
printf("Case %d:\n",i+1);
flag=false;
}
}
return 0;
}