Description
Input
Output
Sample Input
Sample Output
#include
#include
#include
#define N 5000001
int n;
struct node
{
int l,r;
int num;
}q[N];
void build(int l,int r,int i)
{
q[i].l = l;
q[i].r = r;
q[i].num = 0;
if(q[i].l == q[i].r)
{
return ;
}
build(l,(l+r)/2,2*i);
build((l+r)/2+1,r,2*i+1);
}
void insert(int x,int k,int l,int r,int rt)
{
if(x<l || x>r)
{
return ;
}
if(l == r && l == x)
{
q[rt].num += k;
return ;
}
insert(x,k,l,(l+r)/2,rt*2);
insert(x,k,(l+r)/2+1,r,rt*2+1);
q[rt].num = q[rt*2].num + q[rt*2+1].num;
}
int search(int ll,int rr,int l,int r,int rt)
{
if(rr<l || ll>r)
{
return 0;
}
if(ll<=l && rr>=r)
{
return q[rt].num;
}
return search(ll,rr,l,(l+r)/2,rt*2)+search(ll,rr,(l+r)/2+1,r,rt*2+1);
}
int main()
{
int T,i,j,m;
int pp = 0;
scanf("%d",&T);
while(T--)
{
pp++;
scanf("%d",&n);
build(1,n,1);
for(i=1;i<=n;i++)
{
scanf("%d",&m);
insert(i,m,1,n,1);
}
printf("Case %d:\n",pp);
char str[20];
while(scanf("%s",str)!=NULL)
{
if(strcmp(str,"End")==0)
{
break;
}
if(strcmp(str,"Add") == 0)
{
int x,y;
scanf("%d%d",&x,&y);
insert(x,y,1,n,1);
}
if(strcmp(str,"Sub")==0)
{
int x,y;
scanf("%d%d",&x,&y);
insert(x,-y,1,n,1);
}
if(strcmp(str,"Query") == 0)
{
int x,y;
int kk;
scanf("%d%d",&x,&y);
kk = search(x,y,1,n,1);
printf("%d\n",kk);
}
}
}
return 0;
}