标签:des style blog class code java
Time Limit: 2000/1000 MS
(Java/Others) Memory Limit: 65536/32768 K
(Java/Others)
Total Submission(s): 37773 Accepted
Submission(s): 15923
1 #include <stdio.h>
2
3 int lowbit(int x)
4 {
5 return x & -x;
6 }
7 int sum(int a[],int x) //求出第x个元素之前的和
8 {
9 int ans = 0;
10 while(x>0){
11 ans+=a[x];
12 x -= lowbit(x); //向左上爬
13 }
14 return ans;
15 }
16 void add(int a[],int x,int d,int n) //将编号为x的数加d
17 {
18 while(x<=n){
19 a[x]+=d;
20 x+=lowbit(x);
21 }
22 }
23
24 int main()
25 {
26 int Case,i,T,n;
27 scanf("%d",&T);
28 for(Case=1;Case<=T;Case++){
29 int a[50001]={0},d1,d2;
30 char str[30];
31 printf("Case %d:\n",Case);
32 scanf("%d",&n);
33 for(i=1;i<=n;i++){ //输入
34 int t;
35 scanf("%d",&t);
36 add(a,i,t,n);
37 }
38
39 while(1){
40 scanf("%s",str);
41 if(str[0]==‘E‘) //遇到“End”结束
42 break;
43 scanf("%d%d",&d1,&d2);
44 switch(str[0]){
45 case ‘A‘:
46 add(a,d1,d2,n);
47 break;
48 case ‘S‘:
49 add(a,d1,-d2,n);
50 break;
51 case ‘Q‘:
52 printf("%d\n",sum(a,d2)-sum(a,d1-1));
53 break;
54 default:break;
55 }
56 }
57
58 }
59 return 0;
60 }
Freecode : www.cnblogs.com/yym2013
hdu 1166:敌兵布阵(树状数组,练习题),布布扣,bubuko.com
标签:des style blog class code java
原文地址:http://www.cnblogs.com/yym2013/p/3703800.html