hdu1166 树状数组

 1 #include <stdio.h>
 2 int n,c[1000005];
 3 int lowbit(int x)
 4 {
 5                 return(x&-x);
 6 }
 7 void add(int x,int d)
 8 {
 9                 while (x<=n)
10                 {
11                                 c[x]+=d;
12                                 x+=lowbit(x);
13                 }
14 }
15 int sum(int x)
16 {
17                 int res=0;
18                 while (x>0)
19                 {
20                                 res+=c[x];
21                                 x-=lowbit(x);
22                 }
23                 return(res);
24 }
25 int main()
26 {
27                 int t,j,i,a,x,y;
28                 char c1,ch[10];
29                 while (~scanf("%d",&t))
30                 for (j=1;j<=t;j++)
31                 {
32                                 scanf("%d",&n);
33                                 for (i=1;i<=n;i++) c[i]=0;
34                                 for (i=1;i<=n;i++)
35                                 {
36                                                 scanf("%d",&a);
37                                                 add(i,a);
38                                 }
39                                 getchar(); 
40                                 printf("Case %d:\n",j);
41                                 while (~scanf("%s",ch)&&ch[0]!='E')
42                                 {
43                                                 scanf("%d%d%c",&x,&y,&c1);
44                                                 if (ch[0]=='A') add(x,y);
45                                                 if (ch[0]=='S') add(x,-y);
46                                                 if (ch[0]=='Q') printf("%d\n",sum(y)-sum(x-1));
47                                 }
48                 }
49         return(0);
50 }

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

posted on 2014-07-16 17:30  xiao_xin  阅读(81)  评论(0编辑  收藏  举报

导航