`
king_tt
  • 浏览: 2084172 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

HDU 1698 Just a Hook(线段树,成段更新)

 
阅读更多

链接:

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


分析与总结:

我的第一道线段树成段更新题... 看刘汝佳大白书+傻崽的代码学习的


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define mid ((left+right)>>1)
#define lson rt<<1,left,mid
#define rson rt<<1|1,mid+1,right
#define int64 long long

using namespace std;

const int MAXN = 100005;
int n,m;
int sum[MAXN<<2],col[MAXN<<2];

inline void push_up(int rt){
    sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}
inline void push_down(int rt,int m){
    if(col[rt]){
        col[rt<<1] = col[rt<<1|1] = col[rt];
        sum[rt<<1] = (m-(m>>1)) * col[rt];
        sum[rt<<1|1] = (m>>1) * col[rt];
        col[rt] = 0;
    }
}
void build(int rt,int left,int right){
    col[rt] = 0;
    sum[rt] = 1;
    if(left==right)return;
    build(lson);
    build(rson);
    push_up(rt);
}
void update(int rt,int left,int right,int l,int r,int data){
    if(l<=left && right<=r) {
        col[rt] = data;
        sum[rt] = data*(right-left+1);
        return;
    }
    push_down(rt, right-left+1);
    int m = mid;
    if(l <= m) update(lson,l,r,data);
    if(r > m) update(rson,l,r,data);
    push_up(rt);
}
int main(){
    int T,x,y,z,cas=1;
    scanf("%d", &T);
    while(T--){
        scanf("%d%d",&n,&m);
        build(1,1,n);
        for(int i=0; i<m; ++i){
            scanf("%d%d%d",&x,&y,&z);
            update(1,1,n,x,y,z);
        }
        printf("Case %d: The total value of the hook is %d.\n",cas++,sum[1]);
    }
    return 0;
}


—— 生命的意义,在于赋予它意义士。

原创http://blog.csdn.net/shuangde800By D_Double (转载请标明)




分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics