HDU 1689 Just a Hook (线段树, 区间修改)

2024-01-03 13:38
文章标签 修改 区间 hdu 线段 hook 1689

本文主要是介绍HDU 1689 Just a Hook (线段树, 区间修改),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!


这道题 卡了我 长达 一个月之久, 从 树状数组 开始 就一直 WA,  简单的一道 线段树 区间修改模板题, 愣是让我 做了这么久 ,  线段树 区间 修改 延迟标记,!!!!!!!!!!!!

Just a Hook

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


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 :延迟修改时    L 与 R  是 建立区间的 长度 而不是 修改区间的长度;

2 :数值修改时    L 与 R  是 建立区间的 长度 而不是 修改区间的长度;

3 : 要进行 向上更新 和向下更新,  并且 向下更新 一定要在  左右区间回溯之前,  向上更新 在 左右区间之后



我的代码:

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <stdlib.h>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <vector>
#define mem(a,b) memset(a,b,sizeof(a))
#define findx(x) lower_bound(b+1,b+1+bn,x)-b
#define FIN      freopen("input.txt","r",stdin)
#define FOUT     freopen("output.txt","w",stdout)
#define S1(n)    scanf("%d",&n)
#define S2(n,m)  scanf("%d%d",&n,&m)
#define Pr(n)     printf("%d\n",n)using namespace std;
typedef long long ll;
const double PI=acos(-1);
const int INF=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=1e6+5;
const int MOD=1e9+7;
const int mod=1e9+7;
int dir[5][2]={0,1,0,-1,1,0,-1,0};struct SegTree{int val;int addmark;
}Seg[maxn<<2];void PushUP(int root)
{Seg[root].val=Seg[root<<1].val+Seg[root<<1|1].val;
}void build(int root,int l,int r)
{Seg[root].addmark=0;if(l==r){Seg[root].val=1;return;}int mid=(l+r)>>1;build(root<<1,l,mid);build(root<<1|1,mid+1,r);PushUP(root);
}
void PushDown(int root,int l,int r)
{if(Seg[root].addmark){Seg[root<<1].addmark=Seg[root<<1|1].addmark=Seg[root].addmark;Seg[root<<1].val=Seg[root].addmark*l;Seg[root<<1|1].val=Seg[root].addmark*r;Seg[root].addmark=0;}
}
void update(int root,int l,int r,int left,int right,int m)
{int mid=(l+r)>>1;if(left<=l&&right>=r){Seg[root].val=m*(r-l+1);Seg[root].addmark=m;return ;}PushDown(root,mid-l+1,r-mid);if(left<=mid)update(root<<1,l,mid,left,right,m);if(right>mid)update(root<<1|1,mid+1,r,left,right,m);PushUP(root);
}int main()
{int T;int n,m;//FIN;cin>>T;int cont=0;int x,y,z;while(T--){S1(n);build(1,1,n);S1(m);while(m--){scanf("%d%d%d",&x,&y,&z);update(1,1,n,x,y,z);}printf("Case %d: The total value of the hook is %d.\n",++cont,Seg[1].val);}
}



这篇关于HDU 1689 Just a Hook (线段树, 区间修改)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/565875

相关文章

SQL Server修改数据库名及物理数据文件名操作步骤

《SQLServer修改数据库名及物理数据文件名操作步骤》在SQLServer中重命名数据库是一个常见的操作,但需要确保用户具有足够的权限来执行此操作,:本文主要介绍SQLServer修改数据... 目录一、背景介绍二、操作步骤2.1 设置为单用户模式(断开连接)2.2 修改数据库名称2.3 查找逻辑文件名

Oracle修改端口号之后无法启动的解决方案

《Oracle修改端口号之后无法启动的解决方案》Oracle数据库更改端口后出现监听器无法启动的问题确实较为常见,但并非必然发生,这一问题通常源于​​配置错误或环境冲突​​,而非端口修改本身,以下是系... 目录一、问题根源分析​​​二、保姆级解决方案​​​​步骤1:修正监听器配置文件 (listener.

Linux中修改Apache HTTP Server(httpd)默认端口的完整指南

《Linux中修改ApacheHTTPServer(httpd)默认端口的完整指南》ApacheHTTPServer(简称httpd)是Linux系统中最常用的Web服务器之一,本文将详细介绍如何... 目录一、修改 httpd 默认端口的步骤1. 查找 httpd 配置文件路径2. 编辑配置文件3. 保存

Nginx 413修改上传文件大小限制的方法详解

《Nginx413修改上传文件大小限制的方法详解》在使用Nginx作为Web服务器时,有时会遇到客户端尝试上传大文件时返回​​413RequestEntityTooLarge​​... 目录1. 理解 ​​413 Request Entity Too Large​​ 错误2. 修改 Nginx 配置2.1

Python对PDF书签进行添加,修改提取和删除操作

《Python对PDF书签进行添加,修改提取和删除操作》PDF书签是PDF文件中的导航工具,通常包含一个标题和一个跳转位置,本教程将详细介绍如何使用Python对PDF文件中的书签进行操作... 目录简介使用工具python 向 PDF 添加书签添加书签添加嵌套书签Python 修改 PDF 书签Pytho

Java并发编程之如何优雅关闭钩子Shutdown Hook

《Java并发编程之如何优雅关闭钩子ShutdownHook》这篇文章主要为大家详细介绍了Java如何实现优雅关闭钩子ShutdownHook,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起... 目录关闭钩子简介关闭钩子应用场景数据库连接实战演示使用关闭钩子的注意事项开源框架中的关闭钩子机制1.

Docker镜像修改hosts及dockerfile修改hosts文件的实现方式

《Docker镜像修改hosts及dockerfile修改hosts文件的实现方式》:本文主要介绍Docker镜像修改hosts及dockerfile修改hosts文件的实现方式,具有很好的参考价... 目录docker镜像修改hosts及dockerfile修改hosts文件准备 dockerfile 文

Python实现无痛修改第三方库源码的方法详解

《Python实现无痛修改第三方库源码的方法详解》很多时候,我们下载的第三方库是不会有需求不满足的情况,但也有极少的情况,第三方库没有兼顾到需求,本文将介绍几个修改源码的操作,大家可以根据需求进行选择... 目录需求不符合模拟示例 1. 修改源文件2. 继承修改3. 猴子补丁4. 追踪局部变量需求不符合很

Linux修改pip和conda缓存路径的几种方法

《Linux修改pip和conda缓存路径的几种方法》在Python生态中,pip和conda是两种常见的软件包管理工具,它们在安装、更新和卸载软件包时都会使用缓存来提高效率,适当地修改它们的缓存路径... 目录一、pip 和 conda 的缓存机制1. pip 的缓存机制默认缓存路径2. conda 的缓

Linux修改pip临时目录方法的详解

《Linux修改pip临时目录方法的详解》在Linux系统中,pip在安装Python包时会使用临时目录(TMPDIR),但默认的临时目录可能会受到存储空间不足或权限问题的影响,所以本文将详细介绍如何... 目录引言一、为什么要修改 pip 的临时目录?1. 解决存储空间不足的问题2. 解决权限问题3. 提