线段树的单点更新,区域查询操作。
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<math.h>
using namespace std;
#define lmin 1
#define rmax n
#define lson l,(l+r)/2,rt<<1
#define rson (l+r)/2+1,r,rt<<1|1
#define root lmin,rmax,1
#define maxn 55000
int cl[maxn*4];
int num[maxn];
void push_up(int rt)
{
cl[rt]=cl[rt<<1]+cl[rt<<1|1];
}
void push_down(int rt)
{
if(cl[rt]!=-1)
{
cl[rt<<1]=cl[rt];
cl[rt<<1|1]=cl[rt];
cl[rt]=-1;
}
}
void creat(int l,int r,int rt)
{
if(l==r)
{
scanf("%d",&cl[rt]);
return;
}
creat(lson);
creat(rson);
push_up(rt);
}
void update(int ll,int rr,int x,int l,int r,int rt)
{
if(l>rr||r<ll)return;
if(ll<=l&&rr>=r)
{
cl[rt]+=x;
return;
}
// push_down(rt);
update(ll,rr,x,lson);
update(ll,rr,x,rson);
push_up(rt);
}
int query(int ll,int rr,int l,int r,int rt)
{
if(r<ll)return 0;
if(l>rr)return 0;
if(ll<=l&&rr>=r)return cl[rt];
return query(ll,rr,lson)+query(ll,rr,rson);
}
int main()
{
int T,_,n,l,r,st,x;
char str[110];
scanf("%d",&T);
for(_=1;_<=T;_++)
{
printf("Case %d:\n",_);
scanf("%d",&n);
creat(root);
while(1)
{
scanf("%s",str);
if(str[0]=='E')break;
else if(str[0]=='Q')
{
scanf("%d%d",&l,&r);
printf("%d\n",query(l,r,root));
}
else
{
scanf("%d%d",&st,&x);
if(str[0]=='A')update(st,st,x,root);
else update(st,st,-x,root);
}
}
}
return 0;
}