HDOJ1698 Just a hook【线段树---成段更新---lazy标志】

简介:

 

复制代码
/*
1
10
2
1 5 2
5 9 3
*/
#include <stdio.h>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define maxn 111111
int sum[maxn<<2],lazy[maxn<<2];
void PullUp(int rt)//上拉
{
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void PushDown(int rt,int len)//下推
{
    lazy[rt<<1]=lazy[rt<<1|1]=lazy[rt];
    sum[rt<<1]=lazy[rt]*(len-(len>>1));
    sum[rt<<1|1]=lazy[rt]*(len>>1);
    lazy[rt]=0;
}
void build(int l,int r,int rt)
{
    int m=(l+r)>>1;
    lazy[rt]=0;
    if(l==r){
        sum[rt]=1;
        return;
    }
    build(lson);
    build(rson);
    PullUp(rt);
}
void update(int z,int L,int R,int l,int r,int rt)
{
    int m=(l+r)>>1;
    if(l>=L&&r<=R){//当前区间是更新区间的子集,则一定要更新
        lazy[rt]=z;//标记
        sum[rt]=(r-l+1)*z;//更新当前区间
        return;
    }
    if(lazy[rt])PushDown(rt,r-l+1);//延迟标记下传一层
    if(L<=m)    update(z,L,R,lson);//左子树上有一部分
    if(R>m)        update(z,L,R,rson);//右子树上有一部分
    PullUp(rt);//上推
}
int main()
{
    int t,n,q,x,y,z,i;
    scanf("%d",&t);
    for (i=1;i<=t;i++){
        scanf("%d",&n);
        build(1,n,1);
        scanf("%d",&q);
        while (q--){
            scanf("%d%d%d",&x,&y,&z);
            update(z,x,y,1,n,1);
        }
        printf("Case %d: The total value of the hook is %d.\n",i,sum[1]);
    }
    return 0;
}
复制代码

 

 


本文转自ZH奶酪博客园博客,原文链接:http://www.cnblogs.com/CheeseZH/archive/2012/05/02/2479803.html,如需转载请自行联系原作者

相关文章
|
7月前
|
BI 数据库
SAP ABAP 释放 TR 遇到错误消息 ended with return code 8 的含义和处理办法
SAP ABAP 释放 TR 遇到错误消息 ended with return code 8 的含义和处理办法
83 0
|
5天前
|
JavaScript
JS中call()、apply()、bind()改变this指向的原理
JS中call()、apply()、bind()改变this指向的原理
|
7月前
|
JavaScript
[Vue warn]: Duplicate keys detected: ‘1‘. This may cause an update error. for循环,key重复报错
[Vue warn]: Duplicate keys detected: ‘1‘. This may cause an update error. for循环,key重复报错
21 0
|
7月前
|
Kubernetes 容器
【K8s源码品读】002:Phase 1 - kubectl - create的调用逻辑
我们的目标是查看`kubectl create -f nginx_pod.yaml` 这个命令是怎么运行的。
35 0
|
10月前
|
SQL 并行计算 数据库连接
ArcSWAT报错:Error Number :-2147467259; 对 COM 组件的调用返回了错误 HRESULT E_FAIL
ArcSWAT报错:Error Number :-2147467259; 对 COM 组件的调用返回了错误 HRESULT E_FAIL
|
存储 JavaScript
tp5源码解析--hook(钩子函数)类详解
tp5源码解析--hook(钩子函数)类详解
256 0
tp5源码解析--hook(钩子函数)类详解
|
缓存 JavaScript
computed 与 watch和 methods的异同之处
computed 与 watch和 methods的异同之处
81 0
|
JavaScript 前端开发 Shell
在child_process域和错误的冒泡和捕获实践【Note.js】
在child_process域和错误的冒泡和捕获实践【Note.js】
|
Java 编译器 Go
Golang 笔记(一):值方法和指针方法(Value Methods vs Pointer Methods)
Golang 笔记(一):值方法和指针方法(Value Methods vs Pointer Methods)
131 0