题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166
思路:简单的线段树
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=50005;
struct node
{
int x,y;
int sum;
}list[4*N];
int num[N];
void create(int k,int x,int y)
{
list[k].x=x;
list[k].y=y;
if(list[k].x==list[k].y)
{
list[k].sum=num[x];
return;
}
int mid=(x+y)>>1;
create(k<<1,x,mid);
create(k<<1|1,mid+1,y);
list[k].sum=list[k<<1].sum+list[k<<1|1].sum;
}
int find(int k,int x,int y)
{
int mid;
if(list[k].x==x&&list[k].y==y)
return list[k].sum;
mid=(list[k].x+list[k].y)>>1;
if(x>mid)
return find(k<<1|1,x,y);
else if(y<=mid)
return find(k<<1,x,y);
return find(k<<1,x,mid)+find(k<<1|1,mid+1,y);
}
void newdata(int k,int x,int sum)
{
int mid;
if(list[k].x==x&&list[k].y==x)
{
list[k].sum=sum;
return;
}
mid=(list[k].x+list[k].y)>>1;
if(x<=mid) newdata(k<<1,x,sum);
else newdata(k<<1|1,x,sum);
list[k].sum=list[k<<1].sum+list[k<<1|1].sum;
}
int main()
{
int T,n,m,a,b,i,tb=1;
char ch[10];
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d",&num[i]);
create(1,1,n);
printf("Case %d:\n",tb++);
while(scanf("%s",ch))
{
if(ch[0]=='E') break;
else
{
scanf("%d%d",&a,&b);
if(ch[0]=='Q')
{
printf("%d\n",find(1,a,b));
}
else if(ch[0]=='A')
{
num[a]+=b;
m=num[a];
newdata(1,a,m);
}
else if(ch[0]=='S')
{
num[a]-=b;
m=num[a];
newdata(1,a,m);
}
}
}
}
return 0;
}