HDU1166 敌兵布阵

   

题目意思是中文的,相信大家都看得懂不解释。

普通的单点更新线段树,无坑无陷阱。

由于,做的线段树题不多,所以没有自己的代码风格。正在建立自己的风格中。

尽管。曾经早就写了N遍这道题了,可是这次要把线段树的大部分题都过一遍。所以这题也写了一下,而且更改了以往的代码风格。

#include <iostream>
#include <cstdio>
#include <cstring>

#define lson l,m,rt << 1
#define rson m+1,r,rt<<1|1

using namespace std;

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){
     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 kase = 1;kase <= T;++kase){
        scanf("%d",&n);
        build(1,n,1);
        char op[10];
        printf("Case %d:
",kase);
        while(scanf("%s",&op),op[0] != 'E'){
            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/tlnshuju/p/6940450.html