Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 64378 Accepted Submission(s): 27133
#include<bits/stdc++.h> using namespace std; #define maxn 50005 struct ss { int l,r; int sum; }tr[4*maxn]; //int n; int a[maxn]; void build (int k,int s,int t) { tr[k].l=s; tr[k].r=t; if(s==t) { tr[k].sum=a[s]; return ; } int mid=(s+t)>>1; build(k<<1,s,mid); build(k<<1|1,mid+1,t); tr[k].sum=tr[k<<1].sum+tr[k<<1|1].sum; } int ask (int k,int s,int t) { int L=tr[k].l; int R=tr[k].r; if(L==s&&R==t) { return tr[k].sum; } int mid=(L+R)>>1; if(t<=mid) return ask(k<<1,s,t); if(s>mid) return ask(k<<1|1,s,t); return ask(k<<1,s,mid)+ask(k<<1|1,mid+1,t); } void update(int k,int x,int y) { tr[k].sum+=y; if(tr[k].l==x&&tr[k].r==x) return ; int mid=(tr[k].r+tr[k].l)>>1; if(x<=mid) update(k<<1,x,y); else update(k<<1|1,x,y); } int t; int exm; char panduan[20]; int main() { while(scanf("%d",&t)!=EOF) { for(int i=1;i<=t;i++) { scanf("%d",&exm); for(int j=1;j<=exm;j++) scanf("%d",&a[j]); build(1,1,exm); printf("Case %d:\n",i); while(scanf("%s",panduan)) { if(strcmp(panduan,"End")!=0) { int aa,bb; scanf("%d%d",&aa,&bb); if(strcmp(panduan,"Add")==0) update(1,aa,bb); if(strcmp(panduan,"Sub")==0) update(1,aa,-bb); if(strcmp(panduan,"Query")==0) printf("%d\n",ask(1,aa,bb)); } else break; memset(panduan,0,sizeof(panduan)); } } } return 0; }
原文:http://www.cnblogs.com/hsd-/p/5055654.html