标签:
是一道树状数组的模板题
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <iostream> 5 int b[300000],a[300000],n; 6 int lowbit(int x) 7 { 8 return x&(-x); 9 } 10 int sum(int x) 11 { 12 int sum=0; 13 while(x>0) 14 { 15 sum=sum+b[x]; 16 x=x-lowbit(x); 17 } 18 return sum; 19 } 20 void add(int i,int x) 21 { 22 while(i<=n) 23 { 24 b[i]=b[i]+x; 25 i=i+lowbit(i); 26 } 27 } 28 int main() 29 { 30 int t=1; 31 int cx=0; 32 while(scanf("%d",&n)!=EOF&&n) 33 { 34 if(cx) 35 puts(""); 36 memset(b,0,sizeof(b)); 37 int d,e,x; 38 char c[10]; 39 for(int i=1; i<=n; i++) 40 { 41 scanf("%d",&a[i]); 42 add(i,a[i]); 43 } 44 printf("Case %d:\n",t++); 45 while(1) 46 { 47 scanf("%s",c); 48 if(strcmp(c,"END")==0) 49 break; 50 if(c[0]==‘M‘) 51 { 52 scanf("%d%d",&d,&e); 53 printf("%d\n",sum(e)-sum(d-1)); 54 } 55 if(c[0]==‘S‘) 56 { 57 scanf("%d%d",&d,&e); 58 x=e-a[d]; 59 a[d]=e; 60 add(d,x); 61 } 62 } 63 cx=n; 64 } 65 return 0; 66 }
标签:
原文地址:http://www.cnblogs.com/tianmin123/p/4746222.html