题解:

和hdu1166敌兵布阵不同的是 这道题需要区间更新(成段更新)。

单点更新不用说了比较简单,区间更新的话,如果每次都更新到底的话,有点费时间。

这里就体现了线段树的另一个重要思想:延迟标记。

在定义树节点结构体的时候加一个标记:flag。

typedef struct node
{
node():l(0),r(0),data(0),flag(0){} //构造函数 初始化数据成员
int l,r;
int data; //每个节点的数据
int flag; //延迟标记
}TNode;

更新的时候 如果当前区间 被 要更新的区间包括,则标志一下操作 flag,更新一下当前数据 ,直接返回,不必更新到叶子节点。

然后再次 更新或查询的时候 如果该节点有标记,则把当前节点的数据更新,把标志传到孩子节点,再把标志清0。

这就是lazy(延迟、懒惰)思想。

它的好处是更新的时候可以节省大量时间。

知道了lazy思想,那么怎么实现呢?看代码:

#include <iostream>
#include <cstdio>
#include <cstring> using namespace std; const int MAX=100001; typedef struct node
{
node():l(0),r(0),data(0),flag(0){} //构造函数 初始化数据成员
int l,r;
int data; //每个节点的数据
int flag; //延迟标记
}TNode; TNode tNode[MAX<<4]; void pushup(int p) //向上更新
{
tNode[p].data=tNode[p<<1].data+tNode[p<<1|1].data;
} void pushdown(int p) //向下更新
{
tNode[p<<1].flag=tNode[p<<1|1].flag=tNode[p].flag; //把当前节点的标志传到孩子节点
tNode[p<<1].data=(tNode[p<<1].r-tNode[p<<1].l+1)*tNode[p].flag; //更新孩子节点的data数据
tNode[p<<1|1].data=(tNode[p<<1|1].r-tNode[p<<1|1].l+1)*tNode[p].flag;
tNode[p].flag=0; //标志清0
} void buildTree(int p,int l,int r)
{
tNode[p].l=l;
tNode[p].r=r;
tNode[p].flag=0; //建树的时候flag要为0,表示没有操作
if(l==r)
{
tNode[p].data=1;
return;
}
int mid=(l+r)>>1;
buildTree(p<<1,l,mid);
buildTree(p<<1|1,mid+1,r);
pushup(p);
return;
} void update(int p,int l,int r,int x)
{
if(tNode[p].l>=l&&tNode[p].r<=r) //找到 被要更新的区间[l,r]包括的 节点
{
tNode[p].flag=x; //标志一下操作,意思就是把当前以及孩子节点的data值赋值为x。
tNode[p].data=(tNode[p].r-tNode[p].l+1)*x; //更新当前节点的data值。当前区间[l,r]。则它的和为(r-l+1)*x。没毛病!!
return;
}
if(tNode[p].l>r||tNode[p].r<l) //如果当前区间和要更新的区间没有交集
{
return;
}
if(tNode[p].flag!=0) pushdown(p); //如果当前区间有操作标记,则向下更新并把标记传给左右孩子节点
update(p<<1,l,r,x); //更新左孩子
update(p<<1|1,l,r,x); //更新右孩子
pushup(p); //回溯的时候向上更新 } //这个方法这道题没用到
int query(int p,int l,int r)
{
if(tNode[p].l>=l&&tNode[p].r<=r)
{
return tNode[p].data;
}
if(tNode[p].l>r||tNode[p].r<l)
{
return 0;
}
int sum=0;
sum+=query(p<<1,l,r);
sum+=query(p<<1|1,l,r);
return sum;
} int main()
{
int Case;
scanf("%d",&Case);
for(int t=1;t<=Case;t++)
{
int n,op;
scanf("%d%d",&n,&op);
buildTree(1,1,n);
while(op--)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
update(1,a,b,c);
}
printf("Case %d: The total value of the hook is %d.\n",t,tNode[1].data);
}
return 0;
}

  

hdu1698 Just a hook 线段树区间更新的更多相关文章

  1. 【原创】hdu1698 Just a Hook(线段树→区间更新,区间查询)

    学习线段树第二天,这道题属于第二简单的线段树,第一简单是单点更新,这个属于区间更新. 区间更新就是lazy思想,我来按照自己浅薄的理解谈谈lazy思想: 就是在数据结构中,树形结构可以线性存储(线性表 ...

  2. hdu1698 Just a Hook (线段树区间更新 懒惰标记)

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  3. hdu-------(1698)Just a Hook(线段树区间更新)

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  4. (简单) HDU 1698 Just a Hook , 线段树+区间更新。

    Description: In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of ...

  5. HDU 1698 Just a Hook(线段树区间更新查询)

    描述 In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes ...

  6. Just a Hook 线段树 区间更新

    Just a Hook In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of t ...

  7. HDU1698:Just a Hook(线段树区间更新)

    Problem Description In the game of DotA, Pudge’s meat hook is actually the most horrible thing for m ...

  8. hdu - 1689 Just a Hook (线段树区间更新)

    http://acm.hdu.edu.cn/showproblem.php?pid=1698 n个数初始每个数的价值为1,接下来有m个更新,每次x,y,z 把x,y区间的数的价值更新为z(1<= ...

  9. HDU 1698 Just a Hook 线段树区间更新、

    来谈谈自己对延迟标记(lazy标记)的理解吧. lazy标记的主要作用是尽可能的降低时间复杂度. 这样说吧. 如果你不用lazy标记,那么你对于一个区间更新的话是要对其所有的子区间都更新一次,但如果用 ...

随机推荐

  1. [POI2005]AUT-The Bus 树状数组维护最大前缀和

    #include<cstdio> #include<algorithm> using namespace std; const int N=100000+3; int x[N] ...

  2. Java Spring 两大特色

    0 引言 本文主要描述的是Spring常用的两大特色功能:AOP和IoC容器 1 IoC Spring的IoC:就是常说的“控制反转”,也又叫依赖注入的(DI). 优点:IoC最大的好处就是把对象生成 ...

  3. Centos7安装keepalived(自定义路径安装)-高级篇

    0.Keepalived介绍 Keepalived是一个基于VRRP协议来实现的服务高可用方案,可以利用其来避免IP单点故障,类似的工具还有heartbeat.corosync.pacemaker.但 ...

  4. Project Euler 30 Digit fifth powers

    题意:判断一个数 N 的每一位的5次方的和是否为其本身 ,求出所有满足条件的数的和 思路:首先设这个数 N 为 n 位,可以简单的判断一下这个问题的上界 10 ^ n <= 9 ^ 5 × n, ...

  5. luogu 4240 毒瘤之神的考验 (莫比乌斯反演)

    题目大意:略 题面传送门 果然是一道神duliu题= = 出题人的题解传送门 出题人的题解还是讲得很明白的 1.关于$\sum\limits_{i=1}^{n}\sum\limits_{j=1}^{m ...

  6. 简述JVM、JRE、JDK的关系及作用

    1.JVM:java虚拟机 . 作用:保证java语言跨平台. 2.JRE:java运行环境 jre=java虚拟机+核心类库. 作用:java程序的运行环境. 3.JDK :java开发工具集.JD ...

  7. python简单post信息

    最近学了点关于python的网络爬虫的知识,简单记录一下,这里主要用到了requests库和BeautifulSoup库 Requests is an elegant and simple HTTP ...

  8. 【hdu 6396】Swordsman

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 开k个优先队列.每个队列都满足从小到大那种.. 首先将所有的怪物加入到第一个队列中. 然后对于v[i]>=pq[i].top( ...

  9. @value 注解获取属性文件中的值

    一.属性文件 db.properties name=jack 二.配置文件 applicationContext.xml <!-- 加载配置文件,该节点只能存在一个,所以用 * ,加载所有属性文 ...

  10. ASP.NET-AD开发技巧

    分享一篇很好的介绍AD属性的文章 AD图片插件 如何给AD添加图片 http://www.doc88.com/p-9542932844870.html AD过滤条件 重命名ou使用user.Renam ...