HDU 1166 敌兵布阵

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166

View Code
 1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 using namespace std;
5 #define L(x)(x<<1)
6 #define R(x)(x<<1|1)
7 #define MID(x,y) ((x+y)>>1)
8 const int MAX=50010;
9 struct Tnode{
10 int sum,left,right;
11 }node[MAX*4];
12 int num[MAX];
13 void init()
14 {
15 memset(node,0,sizeof(node));
16 }
17 void Build(int t,int l,int r)
18 {
19 node[t].left=l;
20 node[t].right=r;
21 if(l+1==r)
22 {
23 node[t].sum=num[l];
24 return ;
25 }
26 int mid=MID(l,r);
27 Build(L(t),l,mid);
28 Build(R(t),mid,r);
29 node[t].sum=node[L(t)].sum+node[R(t)].sum;
30 }
31 void update(int t,int l,int r,int sum)
32 {
33 if(node[t].left==l&&node[t].right==r)
34 {
35 node[t].sum+=sum;
36 return ;
37 }
38 int mid=MID(node[t].left,node[t].right);
39 if(l>=mid) update(R(t),l,r,sum);
40 else if(r<=mid) update(L(t),l,r,sum);
41 else {
42 update(L(t),l,mid,sum);
43 update(R(t),mid,r,sum);
44 }
45 node[t].sum+=sum;
46 }
47
48 int query(int t,int l,int r)
49 {
50 if( node[t].left == l && node[t].right == r )
51 return node[t].sum;
52 int mid = MID(node[t].left,node[t].right);
53 if( l >= mid )
54 return query(R(t),l,r);
55 else
56 if( r <= mid )
57 return query(L(t),l,r);
58 else
59 return query(L(t),l,mid) + query(R(t),mid,r);
60 }
61 int main()
62 {
63 int ind = 1,ncases,n,x,y;
64 char s[10];
65 scanf("%d",&ncases);
66
67 while( ncases-- )
68 {
69 scanf("%d",&n);
70 for(int i=0; i<n; i++)
71 scanf("%d",&num[i]);
72 init();
73 Build(1,0,n+1);
74 printf("Case %d:\n",ind++);
75 while( scanf("%s",s) && strcmp(s,"End") )
76 {
77 scanf("%d%d",&x,&y);
78 if( s[0] == 'A' )
79 update(1,x-1,x,y);
80 else
81 if( s[0] == 'S' )
82 update(1,x-1,x,-y);
83 else
84 printf("%d\n",query(1,x-1,y));
85 }
86 }
87 return 0;
88 }

 

posted @ 2012-02-27 16:59  我们一直在努力  阅读(144)  评论(0编辑  收藏  举报