1 #include<cstdio> 2 #include<cstdlib> 3 #include<iostream> 4 #define MAXN 50002 5 int look[MAXN],ans=0; 6 using namespace std; 7 struct node 8 { 9 int l,r,value,add; 10 }; 11 node tree[3*MAXN]; 12 void build(int v,int l,int r) 13 { 14 tree[v].l=l; //初始化 15 tree[v].r=r; 16 tree[v].value=0; 17 tree[v].add=0; 18 if(l==r) 19 { 20 tree[v].value=look[r]; //进行节点的初始化 21 return ; 22 } 23 int mid=(l+r)/2; 24 build(v*2,l,mid); 25 build(v*2+1,mid+1,r); 26 tree[v].value=tree[v*2].value+tree[v*2+1].value; 27 } 28 void update(int v ,int l,int r,int m) 29 { 30 if(tree[v].l==l && tree[v].r==r) 31 { 32 tree[v].value+=m*(r-l+1); 33 tree[v].add=m; 34 return ; 35 } 36 if(tree[v].add) 37 { 38 tree[v*2].add+=tree[v].add; 39 tree[v*2+1].add+=tree[v].add; 40 tree[v].add=0; 41 } 42 int mid=(tree[v].l+tree[v].r)/2; 43 if(r<=mid) 44 { 45 update(v*2,l,r,m); 46 } 47 else 48 { 49 if(l>mid) 50 update(v*2+1,l,r,m); 51 else 52 { 53 update(v*2,l,mid,m); 54 update(v*2+1,mid+1,r,m); 55 } 56 } 57 tree[v].value=tree[v*2].value+tree[v*2+1].value; //将父级更新 58 } 59 60 void query(int v,int l,int r) 61 { 62 if(tree[v].l==l&&tree[v].r==r) 63 { 64 ans+=tree[v].value; //开始的时候,ans=0 65 return ; 66 } 67 if(tree[v].add) 68 { 69 tree[v*2].add+=tree[v].add; 70 tree[v*2+1].add+=tree[v].add; 71 tree[v].add=0; 72 } 73 int mid=(tree[v].l+tree[v].r)/2; 74 if(r<=mid) 75 { 76 query(v*2,l,r); 77 } 78 else 79 { 80 if(l>mid) 81 query(v*2+1,l,r); 82 else 83 { 84 query(v*2,l,mid); 85 query(v*2+1,mid+1,r); 86 } 87 } 88 } 89 int main() 90 { 91 int t,n,i,st,en,j; 92 char ask[10]; 93 scanf("%d",&t); 94 for(j=1;j<=t;j++) 95 { 96 scanf("%d",&n); 97 for(i=1;i<=n;i++) 98 { 99 scanf("%d",look+i); 100 } 101 build(1,1,n); 102 printf("Case %d:\n",j); 103 while(scanf("%s",ask),*ask!='E') 104 { 105 switch(*ask) 106 { 107 case 'A': 108 scanf("%d%d",&st,&en); 109 update(1,st,st,en);break; 110 case 'S': 111 scanf("%d%d",&st,&en); 112 update(1,st,st,-en);break; 113 case 'Q': 114 scanf("%d%d",&st,&en); 115 query(1,st,en); 116 printf("%d\n",ans); ans=0;break; 117 } 118 } 119 } 120 //system("PAUSE"); 121 return 0; 122 }