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. POJ 2411_Mondriaan's Dream

    题意: 用1*2和2*1的方块将给定长宽的矩形填满.问有多少种放法,对称的算两种. 分析: 状态压缩dp 首先用0表示前一行没有竖块占用这个位置,而1表示该位置和他上方的位置放了一个竖块,从而压缩状态 ...

  2. UVA 129_ Krypton Factor

    题意: 一个字符串含有两个相邻的重复的子串,则称这个串为容易的串,其他为困难的串,对于给定n,l,求出由前l个字符组成的字典序第n小的困难的串. 分析: 按字典序在字符串末尾增加新的字符,并从当前字符 ...

  3. [bzoj1430]小猴打架_prufer序列

    小猴打架 bzoj-1430 题目大意:题目链接. 注释:略. 想法: 我们发现打架的情况就是一棵树. 我们只需要把确定树的形态然后乘以$(n-1)!$表示生成这棵树时边的顺序. 一共$n$个节点我们 ...

  4. sqlit中使用到的查询语句

    近期使用sqlite查询比較多,包含连表查询等. 记录一下.以免忘记! 1.先依据时间排序后选择前十条: select * from MyBill order by  createTime desc ...

  5. System.AccessViolationException”类型的未经处理的异常在 System.Data.dll 中发生。其它信息:尝试读取或写入受保护的内存。这通常指示其它内存已损坏。

    错误背景: 操作系统:编程环境:VS2013.  语言:VB.net:  数据库:SQLserver2008 做数据库连接时.发生的错误: 错误提示为: 说明:用VB.net连接SQLServer数据 ...

  6. pageContext 获取Session 为null原因

    问题:在J2EE应用中.发如今自己定义标签中取不到session: HttpSession session = pageContext.getSession(); 你会发现session的值可能是空的 ...

  7. nlssort排序

    ALTER SESSION SET NLS_SORT=''; 排序影响整个会话 Oracle9i之前,中文是按照二进制编码进行排序的. 在oracle9i中新增了按照拼音.部首.笔画排序功能.设置NL ...

  8. 加入收藏BUG改善

    <script type="text/javascript"> function add_favorite(a, title, url) { url = url || ...

  9. android 特殊符号开头的联系人归并至“#”下

    在PeopleActivity界面.联系人的显示位置是由其display name的第一个字符决定的. 数字开头的联系人会显示在"#"这个header下. 中英文联系人会显示在&q ...

  10. LeetCode_Mysql_Second Highest Salary

    176. Second Highest Salary 1. 问题描写叙述: 写一个sql语句从 Employee 表里获取第二高位的工资. 2. 解决思路: 这道题非常easy,就当热身了.首先用ma ...