标签:des style blog http color java os io
题目链接 : http://acm.hdu.edu.cn/showproblem.php?pid=1166
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 43689 Accepted Submission(s): 18539
1 #include<iostream> 2 #include<cstring> 3 #include<algorithm> 4 5 using namespace std ; 6 7 int sum[50005]; 8 int n ; 9 10 int lowbit(int x) //取x的最低位1,比如4,则返回4,如5,则返回1 11 { 12 return x&(-x); 13 } 14 15 void update(int i, int val) //将第i个元素增加val 16 { 17 //i的祖先都要增加val 18 while(i <= n) 19 { 20 21 sum[i] += val; 22 i += lowbit(i); //将i的二进制未位补为得到其祖先 23 } 24 } 25 26 int Sum(int i) //求前i项的和 27 { 28 int s = 0; 29 //将前i项分段 30 while(i > 0) 31 { 32 33 s += sum[i]; 34 i -= lowbit(i); //去掉i的二进制最后一个 35 36 } 37 return s; 38 } 39 40 41 int main() 42 { 43 int t,i,j,x,a,b; 44 char str[6]; 45 cin>>t; 46 for(i=1;i<=t;i++) 47 { 48 printf("Case %d:\n",i); 49 50 memset(sum,0,sizeof(sum)); 51 52 cin>>n; 53 54 for(j=1;j<=n;j++) 55 { 56 cin>>x; 57 update(j,x); 58 } 59 60 while(scanf("%s",str)!=EOF&&strcmp(str,"End")!=0) 61 { 62 scanf("%d%d",&a,&b); 63 64 switch(str[0]) 65 { 66 case‘Q‘:printf("%d\n",Sum(b)-Sum(a-1));break; 67 case‘S‘:update(a,-b);break; 68 case‘A‘:update(a,b);break; 69 } 70 71 } 72 } 73 return 0 ; 74 }
HDU 1166 敌兵布阵 (树状数组),布布扣,bubuko.com
标签:des style blog http color java os io
原文地址:http://www.cnblogs.com/ws5167/p/3904004.html