标签:des style blog http io ar color os sp
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 46897 Accepted Submission(s): 19876
#include <stdio.h> #include <string.h> int a[100000],b[100000],n; int lowbit(int t)//求树状数组的管辖域。 { return t&(-t); } int sum(int k)//求和。 { int sum=0; while(k>0) { sum+=b[k]; k-=lowbit(k);//求剩余管辖域内的和 } return sum; } void addorsub(int l,int d)//更新or树状数组赋值 { while(l<=n) { b[l]+=d; l+=lowbit(l);//找后面的跟着一起变。知道最后一个! } } int main() { int T; char s[100],s1[]="Query",s2[]="Add",s4[]="End",s3[]="Sub"; int i,j,k,t,p,m,c,d,o,q,he1,he2,he; int temp; scanf("%d",&T);temp=1; while(T--) { memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); scanf("%d",&n); for(i=1;i<=n;i++) { scanf("%d",&a[i]); addorsub(i,a[i]); } printf("Case %d:\n",temp);//注意! while(1) { scanf("%s",s); if(strcmp(s,s4)==0) break; scanf("%d %d",&c,&d); if(strcmp(s,s1)==0)//这里可以只判断s1[0]. { he2=sum(d); he1=sum(c-1); he=he2-he1; printf("%d\n",he); } if(strcmp(s,s2)==0) { addorsub(c,d); } if(strcmp(s,s3)==0) { addorsub(c,-d); } } temp++; } return 0; }
标签:des style blog http io ar color os sp
原文地址:http://www.cnblogs.com/ikids/p/4129293.html