标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 78691 Accepted Submission(s): 33255
1 模板 2 #include<iostream> 3 #include<string> 4 #include<cstdio> 5 #include<cstring> 6 #include<algorithm> 7 using namespace std; 8 int t,n; 9 int sum[200005]; 10 void pushup(int rt) 11 { 12 sum[rt]=sum[rt<<1]+sum[rt<<1|1]; 13 } 14 void build(int l,int r,int rt) 15 { 16 if(l==r) 17 { 18 scanf("%d",&sum[rt]); 19 return; 20 } 21 int mid=(l+r)>>1; 22 build(l,mid,rt<<1); 23 build(mid+1,r,rt<<1|1); 24 pushup(rt); 25 } 26 void update(int id,int val,int l,int r,int rt) 27 { 28 if(l==r) 29 { 30 sum[rt]+=val; 31 return; 32 } 33 int mid=(l+r)>>1; 34 if(id<=mid) update(id,val,l,mid,rt<<1); 35 else update(id,val,mid+1,r,rt<<1|1); 36 pushup(rt); 37 } 38 int query(int L,int R,int l,int r,int rt) 39 { 40 if(L<=l&&R>=r) 41 { 42 return sum[rt]; 43 } 44 int mid=(l+r)>>1; 45 int ans=0; 46 if(L<=mid) 47 ans+=query(L,R,l,mid,rt<<1); 48 if(R>mid) 49 ans+=query(L,R,mid+1,r,rt<<1|1); 50 return ans; 51 } 52 int main() 53 { 54 int a,b; 55 char ch[10]; 56 scanf("%d",&t); 57 for(int k=1;k<=t;k++) 58 { 59 printf("Case %d:\n",k); 60 scanf("%d",&n); 61 build(1,n,1); 62 while(scanf("%s",&ch)&&ch[0]!=‘E‘) 63 { 64 scanf("%d%d",&a,&b); 65 if(ch[0]==‘A‘) 66 update(a,b,1,n,1); 67 else if(ch[0]==‘S‘) 68 update(a,-b,1,n,1); 69 else if(ch[0]==‘Q‘) 70 printf("%d\n",query(a,b,1,n,1)); 71 } 72 } 73 return 0; 74 }
标签:
原文地址:http://www.cnblogs.com/--ZHIYUAN/p/5879893.html