HDU 1698 Just a Hook (线段树区间更新)
题意 : 一个有n段长的金属棍,开始都涂上铜,分段涂成别的,金的值是3,银的值是2,铜的值是1,然后问你最后这n段总共的值是多少。
思路 : 线段树的区间更新。可以理解为线段树成段更新的模板题。
//HDU 1698
#include <cstdio>
#include <cstring>
#include <iostream> using namespace std; int lz[],p[] ; //lz延迟标记,每次更新不需要更新到底,使得更新延迟到下次更新或者查询的时候
void pushdown(int rt,int m)
{
if(lz[rt])
{
lz[rt << ] = lz[rt] ;
lz[rt << | ] = lz[rt] ;
p[rt << ] = (m -( m >> )) * lz[rt] ;
p[rt << | ] = (m>>) * lz[rt] ;
lz[rt] = ;
}
}
void pushup(int rt) // 将左右孩子的值更新到自己
{
p[rt] = p[rt << ] + p[rt << | ] ;
} void update(int L,int R,int l,int r,int sc,int rt)
{
if(l >= L && r <= R)
{ lz[rt] = sc ;
p[rt] = (r-l+)*sc ;
return ;
}
pushdown(rt,r-l+) ;
int m = (r + l ) >> ;
if(m >= L)
update(L,R,l,m,sc,rt << ) ;
if(R > m)
update(L,R,m+,r,sc,rt << | ) ;
pushup(rt) ;
}
void build(int l,int r,int rt)
{
lz[rt] = ;
if(l == r)
{
p[rt] = ;
return ;
}
int mid = (l + r) >> ;
build(l,mid,rt << ) ;
build(mid + ,r,rt << | ) ;
pushup(rt) ;
}
int query(int L,int R,int l,int r,int rt)
{
int sum = ;
if(L <= l && r <= R)
{ return p[rt] ;
}
pushdown(rt , r-l+) ;
int mid = (l + r) >> ;
if(mid >= L)
sum += query(L,R,l,mid,rt << ) ;
if(mid < R)
sum += query(L,R,mid+,r,rt << | ) ;
return sum ; }
int main()
{
int T,N,Q ;
cin >> T ;
int casee = ;
while(T--)
{
int x,y,z ;
cin >> N >> Q ;
build(,N,) ;
while(Q--)
{
cin >> x >> y >> z ;
update(x,y,,N,z,) ;
}
int sum = query(,N,,N,) ;
cout<<"Case "<< casee ++ <<": The total value of the hook is "<<sum <<"."<<endl ;
}
return ;
}
HDU 1698 Just a Hook (线段树区间更新)的更多相关文章
- (简单) HDU 1698 Just a Hook , 线段树+区间更新。
Description: In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of ...
- 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 ...
- HDU 1698 Just a Hook 线段树区间更新、
来谈谈自己对延迟标记(lazy标记)的理解吧. lazy标记的主要作用是尽可能的降低时间复杂度. 这样说吧. 如果你不用lazy标记,那么你对于一个区间更新的话是要对其所有的子区间都更新一次,但如果用 ...
- HDU 1698 Just a Hook(线段树 区间替换)
Just a Hook [题目链接]Just a Hook [题目类型]线段树 区间替换 &题解: 线段树 区间替换 和区间求和 模板题 只不过不需要查询 题里只问了全部区间的和,所以seg[ ...
- [HDU] 1698 Just a Hook [线段树区间替换]
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 1698 Just a Hook(线段树区间替换)
题目地址:pid=1698">HDU 1698 区间替换裸题.相同利用lazy延迟标记数组,这里仅仅是当lazy下放的时候把以下的lazy也所有改成lazy就好了. 代码例如以下: # ...
- hdu - 1689 Just a Hook (线段树区间更新)
http://acm.hdu.edu.cn/showproblem.php?pid=1698 n个数初始每个数的价值为1,接下来有m个更新,每次x,y,z 把x,y区间的数的价值更新为z(1<= ...
- HDU.1689 Just a Hook (线段树 区间替换 区间总和)
HDU.1689 Just a Hook (线段树 区间替换 区间总和) 题意分析 一开始叶子节点均为1,操作为将[L,R]区间全部替换成C,求总区间[1,N]和 线段树维护区间和 . 建树的时候初始 ...
- HDU.1556 Color the ball (线段树 区间更新 单点查询)
HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...
- Just a Hook 线段树 区间更新
Just a Hook In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of t ...
随机推荐
- C++中int *p[4]和 int (*q)[4]的区别
这俩兄弟长得实在太像,以至于经常让人混淆.然而细心领会和甄别就会发现它们大有不同. 前者是指针数组,后者是指向数组的指针.更详细地说. 前: 指针数组;是一个元素全为指针的数组.后: 数组指针;可以直 ...
- android开发遇到SDK无法访问谷歌而安装不了的情况
遇到SDK无法访问谷歌而安装不了的情况 1.修改C:\Windows\System32\drivers\etc的HOSTS文件,添加 #google_android更新203.208.46.146 d ...
- Go安装
http://www.linuxidc.com/Linux/2015-02/113159.htm https://github.com/astaxie/beego http://www.sizeofv ...
- PHP中使用CURL实现GET和POST请求
转自:http://www.smsyun.com/home-index-page-id-284.html 一.什么是CURL? cURL 是一个利用URL语法规定来传输文件和数据的工具,支持很多协议, ...
- dev 激活没有权限问题
用管理员身份打开 Microsoft Visual Studio 2010-->Visual Studio Tools-->Visual Studio 命令提示(2010) 然后输入一下命 ...
- Android Jni变量对照表
字符 Java类型 C类型 V void void Z jboolean boolean I jint in ...
- C++string的使用
在这里总结一下string的用法 String是可变长字符串,使用的时候要包含string头文件. 要想使用标准C++中string类,必须要包含 #include <string>// ...
- NewRelicAgent(CustomAnalyticEvent.cxx.o), building for iOS simulator, but linking in object file built for OSX, for architecture x8(botched)
昨天遇到一个问题,在项目swift1.2适配swift2.0的过程中,修改完毕之后,运行报错如下: /Pods/NewRelicAgent/NewRelic_iOS_Agent_5.1.0/NewRe ...
- js原型继承与多态 How to apply virtual function in javascript
function BaseClass() { this.hello = function() { this.talk(); } this.talk = function() { document.wr ...
- JPA学习---第十节:JPA中的一对一双向关联
1.创建实体类,代码如下: 代码清单1: package learn.jpa.entity; import javax.persistence.CascadeType; import javax.pe ...