【HDU - 1698】 Just a Hook(线段树模板 区间覆盖更新(laz标记) + 区间和查询 )

题干:

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.

题目大意:

在DOTA里有一个英雄 屠夫,他有一个肉钩,巨长,然后默认都是黄铜做的,每一节的价值都为1。然后他要改变钩子连续链节,换成别的材质。然后题目开始输入样例的个数,然后是钩子的链节数,接下来是操作的次数。后面就是更改了,1是黄铜,2是白银,3是金,价值也是这个数。

解题报告:

     区间覆盖更新,区间和查询。这个题目用到了线段树对区间的更改,相对于单点改值,这个需要有一个pushdown函数。这个题目的话,我们现想一下更改区间的方法,最麻烦的方法就是从给出区间左端点一直遍历修改到右端点,但是这样的话相对来说时间复杂度有点高,难以接受,还有可能TLE。 
所以大佬们就想出来了,在节点当中加一个元素,那就是laz(lazy)元素,这个元素在建树的时候都赋值为零,表示还没有进行改值,然后改值更新的时候,直接在能够被完全包含的区间上进行修改,因为是区间改值,并且修改的值都相同,所以就是修改值乘以区间长度给父节点作出相应处理就可以了,而不是一直改下去改到每一个叶节点,取而代之的是在该节点上记录一下laz,代表这个区间内的值有所改变,但是还没有向分支传递。然后查询的时候,如果需要这个区间内子区间的值的话,那么就借助pushdown函数将laz下标传递给孩子节点。这样说起来可能有点抽象,画个图,或者结合代码思考一下就明白了。参考博客​​​​​​​

AC代码:

#include<bits/stdc++.h>using namespace std;
const int MAXN = 100000 + 5;	
int n;
int a[MAXN];
struct TREE {int l,r;int val;int laz;	
} tree[4*MAXN];
void pushup(int cur) {tree[cur].val = tree[2*cur].val + tree[2*cur + 1].val; 
}
void build(int l ,int r,int cur) {if(l == r) {tree[cur].l = tree[cur].r = l;//写成tree[r].r 了。。 tree[cur].val = a[l];tree[cur].laz = 0;return ;//这步return必须加!不然就无限递归了。这就是为什么写递归函数,要将出口写在最前面,就是,不给他再次进入递归函数的机会! }int m = (l+r)/2;tree[cur].l = l;tree[cur].r = r;
//	tree[cur].val = 0;//加不加均可。 build(l,m,2*cur);build(m+1,r,2*cur + 1);pushup(cur);
}
void pushdown(int cur,int l,int r) {int m = (l+r)/2;if(tree[cur].laz !=0) {tree[2*cur].val = (m-l+1) *tree[cur].laz;tree[2*cur].laz = tree[cur].laz;tree[2*cur + 1].val = (r-m) * tree[cur].laz;tree[2*cur + 1].laz = tree[cur].laz;tree[cur].laz = 0;}
}
//pl-pr为查询区间,l和r为树种 当前cur下标 
int query2(int pl,int pr,int l,int r,int cur) {if(pl<=l && pr>=r) return tree[cur].val; pushdown(cur,l,r);int m = (l+r)/2;int res = 0;if(pl <= m) res += query2(pl,pr,l,m,2*cur);//下面这里是if啊!!不是else!!! if(pr >= m+1) res += query2(pl,pr,m+1,r,2*cur + 1);return res;
}
void update2(int pl,int pr,int val,int l,int r,int cur) {if(pl<=l && pr >= r) {tree[cur].val = (r-l+1) * val;tree[cur].laz =val;return ;}pushdown(cur,l,r);int m = (l + r) / 2;if(pl <= m) update2(pl,pr,val,l,m,2*cur);if(pr >= m + 1) update2(pl,pr,val,m+1,r,2*cur+1);pushup(cur);
}
int main()
{int t,m;int iCase = 0;int tmp1,tmp2;int op;cin>>t;while(t--) {printf("Case %d: ",++iCase);scanf("%d",&n);for(int i = 1; i<=n; i++ ) {a[i] = 1;}memset(tree,0,sizeof(tree));build(1,n,1);
//		for(int i = 1; i<=100; i++) printf("%d ",tree[i].val);
//		printf("\n");scanf("%d",&m);while(m--) {scanf("%d%d%d",&tmp1,&tmp2,&op);update2(tmp1,tmp2,op,1,n,1);
//			for(int i = 1; i<=100; i++) printf("%d ",tree[i].val);
//			printf("\n");}printf("The total value of the hook is %d.\n",query2(1,n,1,n,1));
//		printf("%d %d ",tree[1].l,tree[1].r); 
//		for(int i = 1; i<=100; i++) printf("%d ",tree[i].val);
//		printf("\n");}	return 0 ;} 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441901.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

反序列化 php R类型,pikachu-PHP反序列化、XXE、SSFR

一、PHP反序列化1.1概述在理解这个漏洞前,你需要先搞清楚php中serialize()&#xff0c;unserialize()这两个函数。序列化serialize()序列化说通俗点就是把一个对象变成可以传输的字符串,比如下面是一个对象:class S{public $test"pikachu";}$snew S(); //创建一个对象…

【POJ - 3468 】 A Simple Problem with Integers (线段树模板 区间更新 + 区间和查询)(不能树状数组或差分数组)

题干&#xff1a; You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval. I…

php向下滑动,js如何判断鼠标滚轮是向下还是向上滚动

判断鼠标滚轮是向上或向下滚动&#xff0c;不同的浏览器的判别方式是不一样的&#xff0c;当前比较流行的浏览器有 IE&#xff0c;Opera&#xff0c;Safari&#xff0c;Firefox&#xff0c;Chrome&#xff0c;在这个问题上Firefox和其他浏览器的实现方式是不一样的。现在通过一…

学习php技巧,对初学者非常有用的PHP技巧

对初学者非常有用的PHP技巧131415161718function add_to_cart($item_id , $qty){if(!is_array($item_id)){$_SESSION[cart][$item_id] $qty;}else{foreach($item_id as $i_id > $qty){$_SESSION[cart][$i_id] $qty;}}}add_to_cart( IPHONE3 , 2 );add_to_cart( array(IPHO…

【51nod - 1065】 最小正子段和( 前缀和排序 )

题干&#xff1a; N个整数组成的序列a11,a22,a33,…,ann&#xff0c;从中选出一个子序列&#xff08;aii,ai1i1,…ajj&#xff09;&#xff0c;使这个子序列的和>0&#xff0c;并且这个和是所有和>0的子序列中最小的。 例如&#xff1a;4&#xff0c;-1&#xff0c;5&a…

oracle trace 文件名,限制oracle trace 文件大小

限制oracle trace 文件大小2007-10-10 17:34:55| 分类&#xff1a; oracle | 标签&#xff1a;|字号max_dump_file_size参数在oracle数据库中用来限制 oracle trace 文件大小max_dump_file_size 的单位是 操作系统块 默认值为 UNLIMITED在有些时候&#xff0c;oracle会产生…

【51nod - 前缀异或】 对前缀和的理解

题干&#xff1a; 前缀异或 基准时间限制&#xff1a;2 秒 空间限制&#xff1a;131072 KB 分值: 5 输入一个长度为n(1 < n < 100000)数组a[1], a[2], ..., a[n]。 输入一个询问数m(1 < m < 100000)和m组询问&#xff0c;每组询问形如(l, r) 对于每组询问(l, …

【C语言实现反转数组】(用栈实现)51nod - 训练营

题干&#xff1a; 输入一个长度为n(1 < n < 100000)数组&#xff0c;倒序输出他。 数组中的元素ai满足(1 < ai < 100000)。 Input 第一行一个整数n&#xff0c;表示数字长度 接下来n行&#xff0c;每行一个整数ai&#xff0c;表示数组的内容。 Output 输出第…

oracle 依赖包自动安装包,ORACLE 安装提示缺少依赖包

安装ORALCE 在做检验时提示缺少依赖包&#xff0c;其实是因为系统是64位系统而这些包都是32位的&#xff0c;This is a prerequisite condition to test whether the package "libaio-0.3.105" is available on the systemThis is a prerequisite condition to test…

oracle中创建实体,生成实体-SqlSugar 4.x-文档园

注意&#xff1a;使用DbFirst数据库账户要有系统表的权限,否则无法读取表的结构1.将库里面所有表都生成实体类文件db.DbFirst.CreateClassFile("c:\\Demo\\1",命名空间);2.指定名表生成 &#xff0c;可以传数组db.DbFirst.Where("Student").CreateClassFil…

永远年轻,永远热泪盈眶----致所有奋斗的ACMer

转载一个弱校ACMer的生涯回忆录&#xff1a;&#xff08;励志一下&#xff0c;顺便给自己一碗鸡汤~&#xff09; 有些事情&#xff0c;承诺了便要将它实现&#xff0c;就像我现在写下这些文字。或许之前我并不该定下“每个退役队员写一篇退役贴”这样一个规矩&#xff0c;这多少…

oracle pl/sql 面试,Oracle SQL 面试题(整理)

1、关于group by表内容&#xff1a;2005-05-09 胜2005-05-09 胜2005-05-09 负2005-05-09 负2005-05-10 胜2005-05-10 负2005-05-10 负如果要生成下列结果, 该如何写sql语句?胜 负2005-05-09 2 22005-05-10 1 2创建过程如下&#xff1a;create table tmp(rq varchar(10),shengf…

【HDU - 5094】 Maze (状态压缩+bfs)

题干&#xff1a; This story happened on the background of Star Trek. Spock, the deputy captain of Starship Enterprise, fell into Klingon’s trick and was held as prisoner on their mother planet Qo’noS. The captain of Enterprise, James T. Kirk, had to f…

oracle idl_ub1$,system表空间急剧增大原因分析

system表空间增大是正常的&#xff0c;但急剧增大是不合理的。1有可能是用户对象错误的放在系统表空间中2也可能是system表空间的UNDO过大3还有可能和高级复制的空间使用有关可通过如下语句查看一下是不是有应用的段放到了SYSTEM中&#xff1a;select OWNER,SEGMENT_NAME,SEGME…

【HDU - 1087】Super Jumping! Jumping! Jumping! (最大上升子序列类问题,dp)

题干&#xff1a; Nowadays, a kind of chess game called “Super Jumping! Jumping! Jumping!” is very popular in HDU. Maybe you are a good boy, and know little about this game, so I introduce it to you now. The game can be played by two or more than two pl…

oracle启动监听读取哪个文件,监听服务启动及数据文件恢复oracle数据库

最近遭遇了 oralce 监听服务启动了 又自行关闭的 悲惨经历我把我的过程和大家分享一下&#xff01;1)排查原因程序员是懒惰的&#xff0c;我始终都希望能够成功启动监听服务&#xff0c;但是就是事与愿违有一下方式可能不能成功启动监听1.端口占用&#xff0c;oralce 要用到152…

linux串口写入命令失败,linux – 从串口读取失败

我有以下C程序&#xff1a;#include #include #include int main(){int fd open("/dev/ttyS0",O_RDWR | O_NOCTTY | O_NONBLOCK);if(fd < 0){perror("Could not open device");}printf("Device opened\n");struct termios options;tcgetattr…

【HDU - 1172】猜数字 (枚举暴力)

题干&#xff1a; 猜数字游戏是gameboy最喜欢的游戏之一。游戏的规则是这样的&#xff1a;计算机随机产生一个四位数&#xff0c;然后玩家猜这个四位数是什么。每猜一个数&#xff0c;计算机都会告诉玩家猜对几个数字&#xff0c;其中有几个数字在正确的位置上。 比如计算机随…

linux内核支持的加密算法,Linux Kernel(Android) 加密算法总结(三)-应用程序调用内核加密算法接口...

本文将主要介绍&#xff0c;如何在应用程序空间中(user space) 调用内核空间(kernel space)加密模块提供的加密算法API。方法一&#xff1a;通过调用crypto: af_alg - User-space interface for Crypto API&#xff0c; Herbert Xu 2010年&#xff0c;给内核2.6.X 接口实现下面…

【HDU - 2571】 命运(记忆化搜索)

题干&#xff1a; 穿过幽谷意味着离大魔王lemon已经无限接近了&#xff01; 可谁能想到&#xff0c;yifenfei在斩杀了一些虾兵蟹将后&#xff0c;却再次面临命运大迷宫的考验&#xff0c;这是魔王lemon设下的又一个机关。要知道&#xff0c;不论何人&#xff0c;若在迷宫中被…