#include<stdio.h>
#include<string.h>
int n;
int q;
int tot;
struct Tnode {
int l;
int r;
int pl;
int pr;
int sum;
}node[200000];
char str[1000000];
int mid(int a,int b)
{
return (a+b)/2;
}
int buildtree(int l,int r)
{
int p=tot++;
node[p].l=l;
node[p].r=r;
node[p].sum=0;
if(l<r){
node[p].pl=buildtree(l,mid(l,r));
node[p].pr=buildtree(mid(l,r)+1,r);
}
return p;
}
void cleartree(int l,int r)
{
tot=0;
buildtree(l,r);
}
void add(int key, int val, int p)
{
if (node[p].l>key || node[p].r<key)
return;
if (node[p].l<node[p].r) {
add(key,val,node[p].pl);
add(key,val,node[p].pr);
node[p].sum=node[node[p].pl].sum+node[node[p].pr].sum;
}
else if(node[p].l==node[p].r && key==node[p].l){
node[p].sum+=val;
}
}
void sub(int key, int val, int p)
{
if (node[p].l>key || node[p].r<key)
return;
if (node[p].l<node[p].r) {
sub(key,val,node[p].pl);
sub(key,val,node[p].pr);
node[p].sum=node[node[p].pl].sum+node[node[p].pr].sum;
}
else if (node[p].l==key && node[p].l==node[p].r) {
node[p].sum-=val;
}
}
int query(int l,int r,int p)
{
if (node[p].l>r || node[p].r<l)
return 0;
else if(node[p].l>=l && node[p].r<=r)
return node[p].sum;
else return query(l,r,node[p].pl) + query(l,r,node[p].pr);
}
int main()
{
int ncase,a,b;
scanf("%d",&ncase);
for (int t=1;t<=ncase;t++) {
scanf("%d",&n);
cleartree(1, n);
for (int i=1; i<=n; i++) {
scanf("%d",&q);
add(i, q, 0);
}
printf("Case %d:\n",t);
while (1) {
scanf("%s",str);
if (strcmp(str, "Query")==0) {
scanf("%d%d",&a,&b);
printf("%d\n",query(a,b,0));
}
if (strcmp(str, "Add")==0) {
scanf("%d%d",&a,&b);
add(a, b, 0);
}
if (strcmp(str, "Sub")==0) {
scanf("%d%d",&a,&b);
sub(a, b, 0);
}
if (strcmp(str, "End")==0) {
break;
}
}
}
}
a
浙公网安备 33010602011771号