• 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;
    }
    

  • 相关阅读:
    python 时间差计算
    NET Framework 4.5新特性 (一) 数据库的连接加密保护。
    某表含有N个字段超精简模糊查询方法
    清空javascript数组数据
    IIS无法连接LocalDb,怎么办?
    jquery 模糊查询对象属性
    解释杨中科随机数为什么会骗人?
    前端Js传递数组至服务器端
    javascript获取客户端默认打印机
    水晶报表注意的问题
  • 原文地址:https://www.cnblogs.com/tlnshuju/p/6940450.html
Copyright © 2020-2023  润新知