题解报告: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 ...
随机推荐
- wget下载网络图片
wget http....... --no-check-certificate
- MongoDB小结12 - update【多文档更新】
当一次更新一个文档无法满足我们的脚步时,我们可以选择一次更新多个文档,及在update的第四个参数的位置添上true,及做多文档更新,建议就算不做多文档更新也显式的在第四个参数上置false,这样明确 ...
- list去掉重复元素
需求: 有list 里面含有重复元素,要求去掉重复元素: solution 1: >>> a [1, 2, 2, 1, 3, 4, 5, 6, 5] >>> set ...
- mysql数据类型和java数据类型匹配
Java数据类型和MySql数据类型对应一览 类型名称 显示长度 数据库类型 JAVA类型 JDBC类型索引(int) 描述 VARCHAR L+N VARCHAR java. ...
- 怎样载入指定路径的Logback.xml
今天想外置logback.xml,谢了例如以下代码 File logbackFile = new File("./conf/logback.xml"); if (logbackFi ...
- mysql 经常使用命令整理总结
#改动字段类型 alter table `table_name` modify column ip varchar(50); #添加字段 alter table `table_name` add ip ...
- 【Linux多线程】同步与互斥的区别
同步与互斥这两个概念经常被混淆,所以在这里说一下它们的区别. 一.同步与互斥的区别 1. 同步 同步,又称直接制约关系,是指多个线程(或进程)为了合作完成任务,必须严格按照规定的 某种先后次序来运行. ...
- Python学习笔记_Python对象
Python学习笔记_Python对象 Python对象 标准类型 其它内建类型 类型对象和type类型对象 Python的Null对象None 标准类型操作符 对象值的比較 对象身份比較 布尔类型 ...
- android几个高速打包命令
在MTK android4.4 上,有几条命令能够高速打包system uaserdata bootimage,可提高效率. snod:这个命令是能够高速打包system.是不会运行android各 ...
- UVA 11927 - Games Are Important(sg函数)
UVA 11927 - Games Are Important option=com_onlinejudge&Itemid=8&page=show_problem&catego ...