zoukankan      html  css  js  c++  java
  • hdu 1166 敌兵布阵 (线段树)

    http://acm.hdu.edu.cn/showproblem.php?pid=1166

    最基础的线段树,单点更新。完全跟着HH的代码风格写的。

    code:

    #include<cstdio>
    #define lson l, m, rt<<1
    #define rson m+1, r, rt<<1|1
    const int maxn = 50005 ;
    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=1, T, n, i, j, a, b ;
        char str[10] ;
        scanf("%d", &T) ;
        while(T--){
            scanf("%d", &n) ;
            build(1, n, 1) ;
            printf("Case %d:\n", t++) ;
            while(1){
                scanf("%s", str) ;
                if(str[0]=='E'break ;
                scanf("%d%d", &a, &b) ;
                if(str[0]=='A') update(a, b, 1, n, 1) ;
                else if(str[0]=='S') update(a, -b, 1, n, 1) ;
                else printf("%d\n", query(a, b, 1, n, 1)) ;
            }
        }
        return 0 ;} 
  • 相关阅读:
    用JAVA发送一个XML格式的HTTP请求
    LR 测试http协议xml格式数据接口
    软件测试术语
    linux学习笔记
    接口测试文章整理
    InputStream只能读取一次的解决办法 C# byte[] 和Stream转换
    zTree更新自定义标签>>>
    C# 各类常见Exception 异常信息
    C# 调用存储过程 Sql Server存储过程 存储过程报错,程序中的try
    SQL Server 2014 清除用户名和密码
  • 原文地址:https://www.cnblogs.com/xiaolongchase/p/2491157.html
Copyright © 2011-2022 走看看