标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 69494 Accepted Submission(s): 29214
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 const int maxn = 5e4+5; 7 int a[maxn]; 8 struct node{ 9 int l,r,v; 10 }; 11 node T[maxn*4]; 12 void build(int l,int r,int q){ 13 T[q].l = l; 14 T[q].r = r; 15 if(l == r){ 16 T[q].v = a[l]; 17 return; 18 } 19 int mid = (l+r)/2; 20 build(l,mid,q*2); 21 build(mid+1,r,q*2+1); 22 T[q].v = T[q*2].v + T[q*2+1].v; 23 } 24 int Query(int l,int r,int q){ 25 if(l == T[q].l&&r == T[q].r) return T[q].v; 26 int mid = (T[q].l+T[q].r)/2; 27 if(r<=mid) return Query(l,r,q*2); 28 else if(l>mid) return Query(l,r,q*2+1); 29 return Query(l,mid,q*2)+Query(mid+1,r,q*2+1); 30 } 31 void Add(int i,int j,int q){ 32 if(T[q].l == T[q].r){ 33 T[q].v += j; 34 return; 35 } 36 int mid = (T[q].l+T[q].r)/2; 37 if(i<=mid) Add(i,j,q*2); 38 else Add(i,j,q*2+1); 39 T[q].v = T[q*2].v+T[q*2+1].v; 40 } 41 void Sub(int i,int j,int q){ 42 if(T[q].l == T[q].r){ 43 T[q].v -= j; 44 return; 45 } 46 int mid = (T[q].l+T[q].r)/2; 47 if(i<=mid) Sub(i,j,q*2); 48 else Sub(i,j,q*2+1); 49 T[q].v = T[q*2].v+T[q*2+1].v; 50 } 51 void solve(){ 52 int t,n,count = 0; 53 scanf("%d",&t); 54 while(t--){ 55 scanf("%d",&n); 56 for(int i = 1; i<=n; i++) scanf("%d",&a[i]); 57 printf("Case %d:\n",++count); 58 build(1,n,1); 59 char s[10]; 60 int x,y; 61 for(;;){ 62 scanf("%s",s); 63 if(s[0] == ‘E‘) break; 64 else if(s[0] == ‘Q‘){ 65 scanf("%d%d",&x,&y); 66 printf("%d\n",Query(x,y,1)); 67 } 68 else if(s[0] == ‘A‘){ 69 scanf("%d%d",&x,&y); 70 Add(x,y,1); 71 } 72 else{ 73 scanf("%d%d",&x,&y); 74 Sub(x,y,1); 75 } 76 } 77 } 78 } 79 int main() 80 { 81 solve(); 82 return 0; 83 }
标签:
原文地址:http://www.cnblogs.com/littlepear/p/5396626.html