题解报告:hdu 1698 Just a Hook(线段树区间修改+lazy懒标记的运用)
Problem Description
Now Pudge wants to do some operations on the hook.
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 ...
随机推荐
- Linux下汇编语言学习笔记51 ---
这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...
- HDU - 2059 龟兔赛跑(多阶段决策dp)
http://acm.hdu.edu.cn/showproblem.php?pid=2059 初始把起点和终点也算做充电站,设dp[i]是到第i个充电站的最短时间,那么dp[n+1]即是乌龟到达终点的 ...
- codeforces 691D(数据结构)
D. Swaps in Permutation time limit per test 5 seconds memory limit per test 256 megabytes input stan ...
- [bzoj1207][HNOI2004]打鼹鼠_动态规划
打鼹鼠 bzoj-1207 HNOI-2004 题目大意:题目链接. 注释:略. 想法: $dp_i$表示打到了第$i$个鼹鼠时最多打了多少个鼹鼠. $O(n)$转移即可. 总时间复杂度$O(n^2) ...
- COGS2479(四维偏序)
题意:给定一个有n个元素的序列,元素编号为1~n,每个元素有三个属性a,b,c,求序列中满足i<j且ai<aj且bi<bj且ci<cj的数对(i,j)的个数. 分析:cdq分治 ...
- Restful 级别划分以及HATEOAS是什么?
Restful简介 Rest是一种软件架构风格.设计风格,而不是标准,只是提供了一组设计原则和约束条件.它主要用于客户端和服务器交互类的软件.基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存 ...
- python dos2unix
有时你在windows上创建的文件拿到Linux/unix上运行会出错, 这是因为windows上有些字符如换行符在linux/unix不识别.这种情况下需要用dos2unix这个工具把文件转换成li ...
- 1.spring boot要求最低jdk1.8,平安默认1.6问题,-》安装JDK1.8 2.maven 3.3.3要求最低jdk1.7->安装jdk 1.8
1.spring boot要求最低jdk1.8,平安默认1.6问题,->安装JDK1.82.maven 3.3.3要求最低jdk1.7->安装jdk 1.8
- Android进程间通信之内部类作为事件监听器
在Android中,使用内部类能够在当前类里面发用改监听器类,由于监听器类是外部类的内部类.所以能够自由訪问外部类的全部界面组件. 下面是一个调用系统内部类实现短信发送的一个样例: SMS类: pac ...
- spring中编程式事务控制
step1:配置xml文件 <!-- 事务管理bean --> <bean id="transactionManager" class="org.spr ...