HDU ACM 1166 敌兵布阵(树状数组)

http://acm.hdu.edu.cn/showproblem.php?pid=1166

 

//View Code
 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4 const int MAX = 50000 + 10;
 5 int C[MAX];
 6 int n;
 7 int LowBit(int x)
 8 {
 9     return (x&(-x));
10 }
11 void Update(int num,int k)
12 {
13     while(num <= n)
14     {
15         C[num] += k;
16         num += LowBit(num);
17     }
18 }
19 int CalSum(int num)
20 {
21     int sum = 0;
22     while(num > 0)
23     {
24         sum += C[num];
25         num -= LowBit(num);
26     }
27     return sum;
28 }
29 void Calculate(int i,int j)
30 {
31     cout<<CalSum(j) - CalSum(i-1)<<endl;
32 }
33 int main()
34 {
35     int Case = 0;
36     int T;
37     cin>>T;
38     while(T--)
39     {
40         Case++;
41         cout<<"Case "<<Case<<":"<<endl;
42         memset(C,0,sizeof(C));
43         scanf("%d",&n);
44         int A[MAX];
45         int i;
46         for(i=1;i<=n;i++)
47         {
48             scanf("%d",&A[i]);
49             Update(i,A[i]);
50         }
51         string str;
52         while(cin>>str)
53         {
54             int a,b;
55             if(str == "End")
56             {
57                 break;
58             }
59             if(str == "Query")
60             {
61                 scanf("%d%d",&a,&b);
62                 Calculate(a,b);
63             }
64             if(str == "Add")
65             {
66                 scanf("%d%d",&a,&b);
67                 Update(a,b);
68             }
69             if(str == "Sub")
70             {
71                 scanf("%d%d",&a,&b);
72                 Update(a,(-b));
73             }
74         }
75     }
76     return 0;
77 }

 

posted @ 2012-11-03 17:30  zx雄  阅读(174)  评论(0编辑  收藏  举报