hdu1698 Just a Hook 线段树延迟标记区间更新 求加权区间总和

权值线段树 详解+操作模板(c++) 权值线段树详解+模板代码+例题 阅读详情


Just a Hook

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 25444    Accepted Submission(s): 12707


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.


#include <cstdio>
#include <iostream>
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1

using namespace std;

const int maxn = 111111;
int sum[maxn << 2], col[maxn << 2];

void PushUp(int rt) {
	sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];
}

void PushDown(int rt, int m) {
	if (col[rt]) {
		col[rt << 1] = col[rt << 1 | 1] = col[rt];
		sum[rt << 1] = col[rt] * (m - (m >> 1));
		sum[rt << 1 | 1] = col[rt] * (m >> 1);
		col[rt] = 0; //注意取消延迟标记
	}
}

void build(int l, int r, int rt) {
	sum[rt] = 1;
	col[rt] = 0;
	if (l == r) return;
	int m = (l + r) >> 1;
	build(lson);
	build(rson);
	PushUp(rt);
}

void Update(int L, int R, int c, int l, int r, int rt) {
	if (L <= l && r <= R) {
		col[rt] = c;
		sum[rt] = c * (r - l + 1);
		return;
	}
	PushDown(rt, r - l + 1);
	int m = (l + r) >> 1;
	if (L <= m) Update(L, R, c, lson);
	if (R > m) Update(L, R, c, rson);
	PushUp(rt);
}

int main()
{
	int T, N, Q;
	scanf("%d", &T);
	for (int k = 1; k <= T; k++) {
		scanf("%d%d", &N, &Q);
		build(1, N, 1);
		for (int i = 1; i <= Q; i++) {
			int a, b, c;
			scanf("%d%d%d", &a, &b, &c);
			Update(a, b, c, 1, N, 1);
		}
		//sum[1]存的就是整个hook的每个区间的加权和
		printf("Case %d: The total value of the hook is %d.\n", k, sum[1]);
	}
	return 0;
}




线段树区间更新 线段树成段更新延迟标记理解区间更新是指每次更新的时候更新的是一个区间里面的所有值,例如将区间[l,r]内的所有点都加或者减去一个数,或者替换成一个数字等等.因为区间更新每次更新的不止一个叶子节点,而叶子节点的值的更新肯定会影响到他的一系列的节点,所以假如每次都按照单点更新的思路将每一个叶子节点以及他的父节点都更新的话,那么工作量太大了并且时间肯定不会是log(n)会超时的。例如线段树总区间为[1, 阅读详情

相关推荐

加权随机森林算法研究

随机森林可以产生高准确度的分类器,被广泛用于解决模式识别问题。然而,随机森林赋予每个决策树相同的权重,这在一定程度上降低了整个分类器的性能。为了解决这个问题,本文提出一种加权随机森林算法。该算法引入二次训练过程,提高分类正确率高的决策树投票权重,降低分类错误率高的决策树投票权重,从而提高整个分类器的分类能力。通过在不同数据集上的分类测试实验,证明了本文算法相比于传统的随机森林算法具有更强的分类性能。

数据结构 线段树--权值线段树 详解

一、权值线段树 简介 1.线段树 线段树是一种用于维护区间信息的高效数据结构,可以在 O(log⁡N)O(\log N)O(logN) 的时间复杂度内实现单点修改、区间修改、区间查询(区间求和,求区间最大值,求区间最小值)等操作。 线段树维护的信息必须满足可加性,即能以可以接受的速度合并信息和修改信息,包括在使用Lazy标记时,标记也要满足可加性 具体有关线段树的的讲解请参考博客:线段树 详解与模板

HeartFireY的博客 1万+

Rocky Linux 9.0国内yum源一键替换指南(上海交大镜像站实测)

本文详细介绍了如何将Rocky Linux 9.0的yum源替换为上海交大镜像站,以解决国内用户访问官方源速度慢的问题。通过实测验证,上海交大镜像站提供高速下载和稳定更新,并提供了完整的替换步骤、常见问题解决方案及高级优化配置,帮助用户提升软件包下载效率。

weixin_29304985的博客 371

线段树进阶:权值线段树

————————————————————————————等风来,不如追风去。 介绍 权值线段树是用来求第k大(或小的)。 权值线段树,顾名思义是一棵线段树。 但它和普通线段树不同: 线段树,每个节点用来维护一段区间的最大值或总和等。 权值线段树,相当于一个桶,每个节点用来表示一个区间的数***出现的次数***。 假设我们由一串数:1,5,2,7,4,6,3。全部都加进去的情...

banteng7670的博客 117

什么!!!寒假就要准备暑期实习

介绍暑期实习,从作用,时间线,投递渠道,技术准备,常见问题,经验教训几个方面介绍暑期实习

今天不coding的博客 1732

手把手教你用ADS优化肖特基二极管检波电路(附RC参数避坑指南)

本文详细介绍了如何使用ADS软件优化肖特基二极管检波电路,包括RC参数设置、常见失真问题分析与解决方案。通过实战案例和参数优化技巧,帮助射频工程师避免设计陷阱,提升电路性能。特别附有RC参数避坑指南,适用于调幅解调、功率检测等应用场景。

m2n3o4p5的博客 202

【线段树】 hdu1698 Just a Hook

Just a Hook http://acm.hdu.edu.cn/showproblem.php?pid=1698 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

Ted's 游乐园 2283

STM32F103新手必看:0.96寸OLED屏幕从接线到动态显示全攻略(附IIC避坑指南)

本文为STM32F103新手提供了0.96寸OLED屏幕从硬件接线到软件驱动的完整实战指南。重点解析了IIC通信的时序、设备地址(0x78/0x7A)的常见陷阱,并详细演示了如何实现字符显示与平滑滚动显示等动态效果,帮助开发者快速上手并避开常见调试难题。

stone的博客 558

hdu1698-Just a Hook (线段树区间更新)(延迟标记)

Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 32616    Accepted Submission(s): 15997 Problem Description In the game

wangheng 468

验证码破解2.0:Python+CV大模型搞定滑块/点选验证

核心创新:基于YOLOv8+SAM CV大模型的端到端验证码破解方案,无需人工设计特征,适配多种验证码类型;核心价值高成功率:滑块验证≥95%,点选验证≥90%,远超传统CV方案;低延迟:单验证码破解≤2秒,满足高并发采集需求;行为模拟:生成符合人类操作的轨迹,绕过行为检测;易扩展:统一架构支持新增验证码类型,仅需少量标注数据微调;技术要点采用YOLOv8实现快速目标检测,SAM实现像素级分割;生成物理规律的人类轨迹,模拟真实操作行为;

专注于Python爬虫开发,分享爬虫技巧、项目实战与反爬经验,使用Scrapy、BeautifulSoup等工具,解决数据抓取难题。 1264

HDU - 1698 - Just a Hook (线段树-成段更新)

题目传送:Just a Hook 思路:线段树,成段替换, 区间求和。成段更新时,注意延迟标记的作用,它就是用来暂停往下更新来达到节省时间的,然后每次更新每个节点的子节点之前都要判断是否需要往下更新。 AC代码: #include #include #include #include #include #include #include #include #i

Never say never 1649

11.1华为OD机试真题 新系统 - 整理学生编号 (Java/Py/C/C++/Js/Go)

11.1华为OD机试真题 新系统 - 整理学生编号 (Java/Py/C/C++/Js/Go)

算法大师! 612

Just a Hook(HDU-1698线段树代码讲解很详细)

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 ho

weixin_44313771的博客 2756

conda install出现CondaValueError: Malformed version string invalid character(s)解决方案

本文主要介绍了conda install出现Solving environment:failed、CondaValueError: Malformed version string invalid character(s),希望能对新手有所帮助。 文章目录 1. 问题描述 2. 解决方案 2.1 步骤一 2.2 步骤二 2.3 步骤三

weixin_43178406的博客 6万+

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

HDU 1698 Just a Hook (区间更新+延迟标记)

Just a Hook Time Limit: 4000/2000 MS (Java/Others)Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 31096Accepted Submission(s): 15313 Problem Description In the game of Do...

ailincheng0815的博客 137

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

Just a Hook(线段树,区间更新)

A - Just a Hook Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1698 Appoint description:  System Crawler  (2015-10-10) Descrip

梧桐下的四叶草 792

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

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

经过昨天六个单点更新线段树水题的洗礼,今天上手快了很多。这道题也是一个线段树的一个入门题 这个题是对一个区间段内的数进行整体的更新,如果对区间的更新还是最终落实到对点的更新,显然复杂度是很高的,因此在处理上需要换一种方式 这次,线段树节点存的内容是其左右子树的颜色信息,如果左右子树颜色相同,那么他的值就是左右子树的颜色值,如果不同,则标记为-1 本题的重心在于update时的处理,如果(L=

renew 870

HDU 1698 Just a Hook (线段树,区间更新)

Just a Hook Time Limit: 4000/2000 MS (Java/Others)Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 17214Accepted Submission(s): 8600 Problem Description In the game of Dot...

176

kuangbin专题七:E题 Just a Hook ( 延迟标记,区间更新(赋值)求和 )

HDU 1698 ( 延迟标记,区间更新(赋值)求和 ) Just a Hook题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1698////////////////////////////////////注意 : lazy思想的简单应用,所谓的lazy思想就是成段更新,用一个变量标记该段被更新过,便把这个消息传给子树。lazy思想...

aboc43983的博客 203

【HDU 1698】Just a Hook 线段树 区间覆盖更新+延迟

Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 24314    Accepted Submission(s): 12127 Problem Description In the game of D

ATOD 792

HDU 1698 - Just a Hook(线段树-区间更新)

题目链接:   http://acm.hdu.edu.cn/showproblem.php?pid=1698 题目描述:   有一条长度为N的链,该链每一个单位长度的价值都是1。现有Q个操作,每个操作包括三个数字X, Y, Z, 表示将链中X ~ Y这一段每个单位长度的价值全部改变为Z,注意不是添加Z而是改变为Z。最后问该链的总价值是多少。 解题思路:   区间的线段树更新问题...

ayg046002的博客 158

HDU1698--Just a Hook(成段更新)

题目大意:给出一个区间,更换某一个子区间中的所有值,所有操作结束后,求区间和 分析:线段树,成段更新。需要用到延迟标记(或者说懒惰标记),简单来说就是每次更新的时候不要更新到底,用延迟标记使得更新延迟到下次需要更新or 询问到的时候。 另一篇解题报告讲解较为详细http://blog.csdn.net/hhhhhhj123/article/details/47067873 这里的su

Apple.Daily的专栏 405

HDU 1698 Just a Hook // 线段树 区间更新

题目描述HDU 1698 Just a Hook解题思路题目大意: 每段区间有三类状态: 值为1, 2, 3 要求支持下列操作: 1) 修改区间的状态, 然后输出整个区间[1, n]的和 线段树成段更新的入门题, 具体看注释哈~参考代码#include <stdio.h> #define lson rt<<1 #define rson rt<<1|1 #define mid ((l+r) >

FZU_zyt的专栏_(:з」∠)_ 394

hdu 1698 Just a Hook

题意是:dota中屠夫的钩子,分为n段,每一段刚开始的时候价值为1,他可以对自己的钩子进行改造,改造过程中可以把钩子换成价值2,价值3或价值1的钩子。每次更新一个区间上的钩子,即x-y之间的钩子进行改造。问经过m次改造,钩子的总价值。 此题是明显的线段树题目,而且是更新段数据的

凌风 1039

hdu 1698 Just a Hook(线段树成段更新+延迟标记).

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698 题意:n个价值为1的棍子,按照输入把区间[x,y]的棍子变成z.问经过q操作后,总价值是多少。 线段树成段更新的裸题,学习了lazy的用法。区间更新时先更新到他的下一层左右节点,再往下就等到用到左右节点的时候再往下更新,用一个lazy数组配合pushDown实现。 注意:l

Algobird 607
上一篇: codeforces 626C - Block Towers
下一篇: hdu5652 India and China Origins BFS+二分 或 二维的并查集
Daemoonn
博客等级 码龄11年 42粉丝 198原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值