树状数组HDU1166

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

 1 #include<stdio.h>
 2 #include<string.h>
 3 int c[50005];
 4 int n;
 5 int lowbit(int x)
 6 {
 7     return x&(-x);
 8 }
 9 
10 void add(int i,int x){
11 while(i<=n){
12     c[i]+=x;
13     i+=lowbit(i);
14 }
15 }
16 void sub(int i,int x){
17 while(i<=n){
18       c[i]-=x;
19       i+=lowbit(i);
20       }
21     }
22 int sum(int i)
23 {
24     int s=0;
25     while(i>0){
26         s+=c[i];
27         i-=lowbit(i);
28     }
29     return s;
30 }
31 int main()
32 {
33     int a,b,x;
34     char s[15];
35     int t;
36    scanf("%d",&t);
37     int time=0;
38     while(t--){
39             time++;
40             printf("Case %d:
",time);
41 memset(c,0,sizeof(c));
42            scanf("%d",&n);
43     for(int i=1;i<=n;i++){
44         scanf("%d",&x);
45         add(i,x);
46     }
47     while(~scanf("%s",&s)&&strcmp(s,"End")!=0){scanf("%d%d",&a,&b);
48        if(s[0]=='A'){
49 
50         add(a,b);
51        }
52       if(s[0]=='Q'){
53 int f=sum(b)-sum(a-1);
54         printf("%d
",f);
55        }
56      if(s[0]=='S'){
57         sub(a,b);
58        }
59 
60     }
61     }
62 }
View Code

注意呦,同样的代码用c++提交就超时喽……

你若盛开,清风自来...
原文地址:https://www.cnblogs.com/shangjindexiaoqingnian/p/5760845.html