线段树模板题
#include
#include
#include
#include
#include
#include
#include
#define max_ 50010
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
struct node
{
int l,r,sum;
};
struct node tree[max_*4];
int n;
void build(int i,int l,int r)
{
if(l==r)
{
scanf("%d",&tree[i].sum);
tree[i].l=tree[i].r=l;
return;
}
int mid=(l+r)/2;
build(i<<1,l,mid);
build(i<<1|1,mid+1,r);
tree[i].sum=tree[i<<1].sum+tree[i<<1|1].sum;
tree[i].l=l;
tree[i].r=r;
}
void change (int i,int id,int w)
{
if(tree[i].l==tree[i].r)
{
tree[i].sum+=w;
return;
}
int mid=(tree[i].l+tree[i].r)>>1;
if(id<=mid)
change(i<<1,id,w);
else
change(i<<1|1,id,w);
tree[i].sum+=w;
}
int query(int i,int l,int r)
{
if(tree[i].l==l&&tree[i].r==r)
return tree[i].sum;
int mid = (tree[i].l+tree[i].r)>>1;
if(r<=mid)
return query(i<<1,l,r);
else if(l>mid)
return query(i<<1|1,l,r);
else
return query(i<<1,l,mid)+query(i<<1|1,mid+1,r);
}
int main(int argc, char const *argv[])
{
int r,t;
scanf("%d",&t);
for(r=1;r<=t;r++)
{
scanf("%d",&n);
memset(tree,0,sizeof(tree));
build(1,1,n);
char sent[10];
printf("Case %d:\n",r);
while(scanf("%s",sent)&&sent[0]!='E')
{
int x,y;
scanf("%d%d",&x,&y);
switch (sent[0])
{
case 'Q' :printf("%d\n",query(1,x,y));break;
case 'S' :y*=-1;
case 'A' :change(1,x,y);break;
}
}
}
return 0;
}