Problem Description

In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several consecutive metallic sticks which are of the same length.
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

The input consists of several test cases. The first line of the input is the number of the cases. There are no more than 10 cases.
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

For each case, print a number in a line representing the total value of the hook after the operations. Use the format in the example.

Sample Input

1
10
2
1 5 2
5 9 3

Sample Output

Case 1: The total value of the hook is 24.
解题思路:线段树区间修改,由于修改的次数比较多,因此每次修改不可能将树中对应区间所有节点的值都修改了,如果是这样就会超时,于是延迟标记即懒标记就派上用场了。题目的意思就是编号1~N的棍棒初始值都为1(铜棒),然后有q次修改,每次修改将对应区间的所有编号的值修改为Z对应的值。怎么使用懒标记呢?假如我们修改的区间为[a,b],如果递归到a<=l&&r<=b即修改区间[a,b]包含了子区间[l,r],则不再递归下去,直接在这个区间上进行修改,即此时区间的和修改为修改值val*此区间长度(r-l+1):sum[x]=val*(r-l+1)(下标x为在树中的节点编号),同时将此区间的懒标记标记为修改值val即lazy[x]=val,因为下次更新时,如果遇到有懒标记的区间,要先下放懒标记,然后子区间的懒标记修改为父区间懒标记的值val即lazy[x<<1]=lazy[x<<1|1]=lazy[x],记得取消父区间的懒标记即lazy[x]=0,子区间的和也要修改即左区间和sum[x<<1]=(len-(len>>1))*lazy[x](len为父区间的长度:r-l+1),右区间和sum[x<<1|1]=(len>>1)*lazy[x],这样就减少了修改的次数,根据需要来修改较大的区间,并且向上更新父节点的和即可,最后sum[1]的值即为编号1~N所有值的总和。对于以上操作,在纸上模拟一下就清楚了。
AC代码:
 #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懒标记的运用)的更多相关文章

  1. 题解报告: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 ...

  2. HDU 1698 Just a Hook(线段树 区间替换)

    Just a Hook [题目链接]Just a Hook [题目类型]线段树 区间替换 &题解: 线段树 区间替换 和区间求和 模板题 只不过不需要查询 题里只问了全部区间的和,所以seg[ ...

  3. 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 ...

  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(线段树区间替换)

    题目地址:pid=1698">HDU 1698 区间替换裸题.相同利用lazy延迟标记数组,这里仅仅是当lazy下放的时候把以下的lazy也所有改成lazy就好了. 代码例如以下: # ...

  6. [HDU] 1698 Just a Hook [线段树区间替换]

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

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

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

  8. HDU.1689 Just a Hook (线段树 区间替换 区间总和)

    HDU.1689 Just a Hook (线段树 区间替换 区间总和) 题意分析 一开始叶子节点均为1,操作为将[L,R]区间全部替换成C,求总区间[1,N]和 线段树维护区间和 . 建树的时候初始 ...

  9. 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 ...

随机推荐

  1. Android TextView内容过长加省略号

    在Android TextView中有个内容过长加省略号的属性,即ellipsize,用法如下: 在xml中: android:ellipsize = "end" //省略号在结尾 ...

  2. 重启系统media服务

    1.adb shell 执行 stop media & start media 或者stop media ; start media 2.代码里执行 import android.os.Sys ...

  3. 用jQuery向div中添加Html文本内容

    前台代码: <link href="http://www.cnblogs.com/Content/themes/base/jquery-ui.css" rel="s ...

  4. 洛谷 P1018 乘积最大

    P1018 乘积最大 题目描述 今年是国际数学联盟确定的“ 20002000 ――世界数学年”,又恰逢我国著名数学家华罗庚先生诞辰 9090 周年.在华罗庚先生的家乡江苏金坛,组织了一场别开生面的数学 ...

  5. zip4j加密压缩、解压缩文件、文件夹

    原文:http://blog.csdn.net/k21325/article/details/54376188 1.首先,引用到zip4j的第三方类库,感谢作者的无私奉献,官网打不开,这里就不贴了,下 ...

  6. pipenv 的使用

    pipenv 的使用 学习了:https://blog.csdn.net/chroming/article/details/77104873?locationNum=4&fps=1 https ...

  7. scala 入门Eclipse环境搭建及第一个入门经典程序HelloWorld

    scala 入门Eclipse环境搭建及第一个入门经典程序HelloWorld 学习了: http://blog.csdn.net/wangmuming/article/details/3407911 ...

  8. day2-搭建hdfs分布式集群

    1.搭建hdfs分布式集群 4.1 hdfs集群组成结构: 4.2 安装hdfs集群的具体步骤: 一.首先需要准备N台linux服务器 学习阶段,用虚拟机即可! 先准备4台虚拟机:1个namenode ...

  9. 【Linux多线程】三个经典同步问题

    在了解了<同步与互斥的区别>之后,我们来看看几个经典的线程同步的例子.相信通过具体场景可以让我们学会分析和解决这类线程同步的问题,以便以后应用在实际的项目中. 一.生产者-消费者问题 问题 ...

  10. vue组件之间的通信,父子之间的数据通信

    父子组件之间的通信问题既可以传递数据也可以传递变量,父组件传递数据给子组件可以使用props,子组件传递数据给父组件则可以自定义函数来监听子组件的事件发射器. 首先说说组件注册,组件的注册分为全局注册 ...