敌兵布阵 HDU

 敌兵布阵 HDU - 1166 点击打开链接 (用的是胡浩dalao的线段树模板)
中文题意。
      思路:线段树模板题

#include <cstdio>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int maxn = 55555;
int sum[maxn<<2];
void PushUP(int rt){//将儿子节点的信息更新到父亲节点
       sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void build(int l,int r,int rt){//[l,r]表示当前节点维护的区间,rt表示当前点的下标
       if(l==r){//说明已经是叶节点了
            scanf("%d",&sum[rt]);
            return ;
       }
       int m=(l+r)>>1;
       build(lson);
       build(rson);
       PushUP(rt);
}
void update(int p,int add,int l,int r,int rt) {
       if(l==r) {
            sum[rt]+=add;
            return ;
       }
       int m=(l+r)>>1;
       if (p<=m) update(p,add,lson);//判断要加的点在哪个区间
       else update(p,add,rson);
       PushUP(rt);
}
int query(int L,int R,int l,int r,int rt) {
       if (L<=l&&r<=R) {
              return sum[rt];
       }
       int m=(l+r)>>1;
       int ret=0;
       if (L<=m) ret+=query(L,R,lson);//缩右节点
       if (R>m) ret+=query(L,R,rson);//缩左节点
       return ret;
}
int main() {
       int T,n;
       scanf("%d",&T);
       for (int cas=1;cas<=T;cas++) {
              printf("Case %d:
",cas);
              scanf("%d",&n);
              build(1,n,1);
              char op[10];
              while (scanf("%s",op)) {
                     if (op[0]=='E') break;
                     int a,b;
                     scanf("%d%d",&a,&b);
                     if (op[0]=='Q') printf("%d
",query(a,b,1,n,1));
                     else if (op[0] == 'S') update(a,-b,1,n,1);
                     else update(a,b,1,n,1);
              }
       }
       return 0;
}

原文地址:https://www.cnblogs.com/Levi-0514/p/9092862.html