题解报告:hdu 1698 Just a Hook(线段树区间修改+lazy懒标记的运用)
Problem Description
Let us number the consecutive metallic sticks of the hook from 1 to N. For each operation, Pudge can change the consecutive metallic sticks, numbered from X to Y, into cupreous sticks, silver sticks or golden sticks.
The total value of the hook is calculated as the sum of values of N metallic sticks. More precisely, the value for each kind of stick is calculated as follows:
For each cupreous stick, the value is 1.
For each silver stick, the value is 2.
For each golden stick, the value is 3.
Pudge wants to know the total value of the hook after performing the operations.
You may consider the original hook is made up of cupreous sticks.
Input
For each case, the first line contains an integer N, 1<=N<=100,000, which is the number of the sticks of Pudge’s meat hook and the second line contains an integer Q, 0<=Q<=100,000, which is the number of the operations.
Next Q lines, each line contains three integers X, Y, 1<=X<=Y<=N, Z, 1<=Z<=3, which defines an operation: change the sticks numbered from X to Y into the metal kind Z, where Z=1 represents the cupreous kind, Z=2 represents the silver kind and Z=3 represents the golden kind.
Output
Sample Input
Sample Output
#include<iostream>
#include<string.h>
#include<cstdio>
using namespace std;
const int maxn=;
int T,n,q,a,b,val,lazy[maxn<<],sum[maxn<<];//懒标记、该区间的和
void build(int l,int r,int x){
int mid=(l+r)>>;
if(l==r){sum[x]=;return;}//懒标记将节点全部标记为0
build(l,mid,x<<);//建立左子树
build(mid+,r,x<<|);//建立右子树
sum[x]=sum[x<<]+sum[x<<|];//统计父节点的值
}
void push_down(int x,int len){//下放懒标记
if(lazy[x]){
lazy[x<<]=lazy[x<<|]=lazy[x];//直接将父节点的懒标记给左右儿子
sum[x<<]=(len-(len>>))*lazy[x];//左右儿子的总值统计为对应区间长度乘以父节点的懒标记值
sum[x<<|]=(len>>)*lazy[x];//左子区间长度为len-(len>>1),右子区间长度为len>>1
lazy[x]=;//同时置父节点懒标记为0
}
}
void modify(int l,int r,int x,int val){
if(a<=l&&r<=b){//如果区间[a,b]包含当前子区间[l,r],则直接将此区间值懒标记修改为val值,并且重新计算该区间的和
lazy[x]=val;
sum[x]=val*(r-l+);//将区间节点[l,r]的总和变为区间长度乘以改变值val,表示该区间每个节点的值全部修改为val
return;
}
push_down(x,r-l+);//如果不包含当前子区间,并且此区间有懒标记,应下放懒标记,同时取消当前区间的懒标记
int mid=(l+r)>>;
if(b<=mid)modify(l,mid,x<<,val);
else if(a>mid)modify(mid+,r,x<<|,val);
else{
modify(l,mid,x<<,val);
modify(mid+,r,x<<|,val);
}
sum[x]=sum[x<<]+sum[x<<|];//左右(区间)节点的值可能发生改变,因此要向上更新父节点的值
}
int main(){
scanf("%d",&T);
for(int j=;j<=T;++j){
scanf("%d%d",&n,&q);
memset(lazy,,sizeof(lazy));//每个节点的懒标记都为0
build(,n,);//建树
while(q--){
scanf("%d%d%d",&a,&b,&val);
modify(,n,,val);
}
printf("Case %d: The total value of the hook is %d.\n",j,sum[]);//1~N所有值的总和为sum[1]
}
return ;
}
题解报告:hdu 1698 Just a Hook(线段树区间修改+lazy懒标记的运用)的更多相关文章
- 题解报告:poj 3468 A Simple Problem with Integers(线段树区间修改+lazy懒标记or树状数组)
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
- HDU 1698 Just a Hook(线段树 区间替换)
Just a Hook [题目链接]Just a Hook [题目类型]线段树 区间替换 &题解: 线段树 区间替换 和区间求和 模板题 只不过不需要查询 题里只问了全部区间的和,所以seg[ ...
- 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 ...
- (简单) HDU 1698 Just a Hook , 线段树+区间更新。
Description: In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of ...
- HDU 1698 Just a Hook(线段树区间替换)
题目地址:pid=1698">HDU 1698 区间替换裸题.相同利用lazy延迟标记数组,这里仅仅是当lazy下放的时候把以下的lazy也所有改成lazy就好了. 代码例如以下: # ...
- [HDU] 1698 Just a Hook [线段树区间替换]
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 1698 Just a Hook 线段树区间更新、
来谈谈自己对延迟标记(lazy标记)的理解吧. lazy标记的主要作用是尽可能的降低时间复杂度. 这样说吧. 如果你不用lazy标记,那么你对于一个区间更新的话是要对其所有的子区间都更新一次,但如果用 ...
- HDU.1689 Just a Hook (线段树 区间替换 区间总和)
HDU.1689 Just a Hook (线段树 区间替换 区间总和) 题意分析 一开始叶子节点均为1,操作为将[L,R]区间全部替换成C,求总区间[1,N]和 线段树维护区间和 . 建树的时候初始 ...
- HDU 1698 just a hook 线段树,区间定值,求和
Just a Hook Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1 ...
随机推荐
- 姓名与ID(codevs 1027 未结题)
题目描述 Description 有N个人,各自有一个姓名和ID(别名).每个人的姓名和ID都没有重复.这些人依次进入一间房间,然后可能会离开.过程中可以得到一些信息,告知在房间里的某个人的ID.你的 ...
- Jam's balance set 暴力
Jim has a balance and N weights. (1≤N≤20)(1≤N≤20) The balance can only tell whether things on differ ...
- [HDU5306]Gorgeous Sequence(标记回收线段树)
题意:维护一个序列,支持区间与一个数取min,询问区间最大,询问区间和(序列长度<=1e6) 分析: http://www.shuizilong.com/house/archives/hdu-5 ...
- 输入一个URL之后。。。
1.输入URL2.浏览器去浏览器缓存.系统缓存.路由器缓存查找缓存记录,有则直接访问URL对应的IP,无则下一步3.DNS解析URL,获得对应的IP4.浏览器通过TCP/IP三次握手连接服务器5.客户 ...
- Linux系统备份还原工具1(DD)(应用实例)
DD使用教程:http://www.cnblogs.com/EasonJim/p/7442223.html 以下实例没经过大量测试,可能在一些机器上不会有效. 一般围绕以下几点进行设置: 1.dd完后 ...
- rsh 无秘钥登陆配置
/etc/hosts.equiv里的主机不须要提供password就能够訪问本机./etc/host.equiv 要和~/.rhosts文件连用. [root@web-htl2-01 ~]# cat ...
- C++之桟的应用---括号匹配
刚開始学习数据结构.用桟写了一个经典的应用,括号匹配. 算法思路: 输入字符串时.将 '(' , '[' 压入桟.遇到 ')' ']' 时,再栈顶出桟.进行括号匹配.假设成功匹配.则继续进行.否 ...
- [RK3288][Android6.0] 音频调试方法小结【转】
本文转载自:http://blog.csdn.net/kris_fei/article/details/70053135 Platform: ROCKCHIPOS: Android 6.0Kernel ...
- 在canvas上面绘制图片--drawImage实例
在canvas上面绘制图片--drawImage实例 关键点: 1.图片居中 2.其它 <!DOCTYPE html> <html lang="zh-cn"> ...
- 【推荐】微服务分布式企业框架Springmvc+mybatis+shiro+Dubbo+ZooKeeper+Redis
摘要: 主要定位于互联网企业架构,已内置企业信息化系统的基础功能和高效的代码生成工具,包括:系统权限组件.数据权限组件.数据字典组件.核心工具 组件.视图操作组件.工作流组件.代码 ...