HDU 1166 敌兵布阵 (线段树单点更新区间求值)

题意

中文题面不需要解释了吧

题解

裸的线段树线段树单点更新区间,注意输出格式就ok,算是给新手入门的题

 1 #define bug(x,y) cout<<"i="< 2 #define IO std::ios::sync_with_stdio(0);
 3 #include 
 4 #define itor ::iterator
 5 using namespace  std;
 6 typedef long long ll;
 7 typedef pairP;
 8 #define pb push_back
 9 #define se second
10 #define fi first
11 #define rs o*2+1
12 #define ls o*2
13 const int N=5e4+5;
14 int sumv[N*4];
15 int T,n;
16 void build(int o,int l,int r){
17     if(l==r){
18         scanf("%d",&sumv[o]);
19         return;
20     }
21     int m=(l+r)/2;
22     build(ls,l,m);
23     build(rs,m+1,r);
24     sumv[o]=sumv[ls]+sumv[rs];
25 }
26 void up(int o,int l,int r,int p,int v){
27     if(l==r&&l==p){
28         sumv[o]+=v;
29         return;
30     }
31     int m=(l+r)/2;
32     if(p<=m)up(ls,l,m,p,v);
33     else up(rs,m+1,r,p,v);
34     sumv[o]=sumv[ls]+sumv[rs];
35 }
36 int query(int o,int l,int r,int ql,int qr){
37     if(l>=ql&&r<=qr){
38         return sumv[o];
39     }
40     int m=(l+r)/2;
41     int res=0;
42     if(ql<=m)res+=query(ls,l,m,ql,qr);
43     if(qr>m)res+=query(rs,m+1,r,ql,qr);
44     return res;
45 }
46 int main(){
47     scanf("%d",&T);
48     int kase=0;
49     while(T--){
50         scanf("%d",&n);
51         build(1,1,n);
52         char s[20];
53         int x,y;
54         printf("Case %d:\n",++kase);
55         while(~scanf("%s",s)){
56             if(s[0]=='E')break;
57             if(s[0]=='A'){
58                 scanf("%d%d",&x,&y);
59                 up(1,1,n,x,y);
60             }
61             else if(s[0]=='S'){
62                 scanf("%d%d",&x,&y);
63                 up(1,1,n,x,-y);
64             }
65             else{
66                 scanf("%d%d",&x,&y);
67                 printf("%d\n",query(1,1,n,x,y));
68             }
69         }
70     }
71 }

 

你可能感兴趣的:(HDU 1166 敌兵布阵 (线段树单点更新区间求值))