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 ...
随机推荐
- [原创]pg_shard使用场景及功能测试
pg_shard是一个PostgreSQL的sharding extension.可以用于Shards.Replicates tables和高可用.它可以在不修改Applications的情况下无缝分 ...
- hdu 5272 Dylans loves numbers
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5272 Dylans loves numbers Description Who is Dylans?Y ...
- .NET Async/Await 最佳实践
.NET 异步编程Guildlines 名称 描述 例外 Avoid async void Prefer async Task methods over async void methods Even ...
- golang没有条件表达式?:
详见The Go Programming Language Specification中Expressions一章中未提及此表达式, 故其不支持. 再强调一次, GO不支持条件表达式 conditio ...
- centos rsync安装配置
安装 1 yum -y install rsync ---------------------服务器安装------------------------------- 创建基础配置文件 1 2 3 4 ...
- [译]rabbitmq 2.1 Consumers and producers (not an economics lesson)
我对rabbitmq学习还不深入,这些翻译仅仅做资料保存,希望不要误导大家. For now, all you need to know is that producers create messag ...
- 通过firefox+ProxySelector+dtunnel_lite实现代理上网
通过firefox+ProxySelector+dtunnel_lite实现代理上网 dtunnel_lite:http://dog-tunnel.tk/下载lite版本就可以 远端:./dtunne ...
- js从富文本中找出固定电话、手机号码、邮箱并添加上链接。
function richtextfindtel(value) { /*固定电话.手机号码.邮箱*/ var tempValue = value.replace(/<[^>]+>/g ...
- angularjs resources
http://tutorials.jenkov.com/angularjs/watch-digest-apply.html http://angular-tips.com/blog/2013/08/w ...
- hibernate tool连接oracle生成pojo和xml文件无法查询表解决办法
需要在hibernate的配置文件中增加 <property name="hibernate.default_schema">[username]</proper ...