hdu1166 (bit)

基本上即使原来函数有的操作,更新和查询,sub的时候改成负数。

 1 #include<stdio.h>
 2 #include<string.h>
 3 int c[1000005],n;
 4 int lowbit(int x)
 5 {
 6     return x&(-x);
 7 }
 8 int sum(int x)
 9 {
10     int ret=0;
11     while(x>0)
12     {
13         ret+=c[x];
14         x-=lowbit(x);
15     }
16     return ret;
17 }
18 void add(int x,int d)
19 {
20     while(x<=n)
21     {
22         c[x]+=d;
23         x+=lowbit(x);
24     }
25 }
26 int main()
27 {
28     int T,count=1;
29     scanf("%d",&T);
30     while(T--)
31     {
32         printf("Case %d:
",count++);
33         memset(c,0,sizeof(c));
34         scanf("%d",&n);
35         int a;
36         for(int i=0;i<n;i++)
37         {
38             scanf("%d",&a);
39             add(i+1,a);
40         }
41 
42         char str[10];
43         int p1,p2;
44         while(scanf("%s",str))
45         {
46             if(str[0]=='E')
47             break;
48             scanf("%d%d",&p1,&p2);
49             if(str[0]=='A')
50                 add(p1,p2);
51             else if(str[0]=='S')
52                 add(p1,-p2);
53             else
54             printf("%d
",sum(p2)-sum(p1-1));
55         }
56     }
57     return 0;
58 }
原文地址:https://www.cnblogs.com/Acgsws/p/3220087.html