hdu 1698 Just a Hook 【线段树+lazy】
写了一天的线段树,这道题主要说明一下sum是赋值的,不是累加的,并且在push_down的时候lazy也是赋值的。因可能对懒标记的理解还不是很透彻吧。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int Max = 1e5+10;
typedef long long ll;
struct Tree{
int L,R;
ll sum;
int lazy;
} tree[Max<<2];
void PushUp(int x)
{
tree[x].sum = tree[x<<1].sum+tree[x<<1|1].sum;
}
void pushdown(int x,int num)
{
if(tree[x].lazy){
tree[x<<1].sum=tree[x].lazy*(num-(num/2));
tree[x<<1|1].sum=tree[x].lazy*(num/2);
tree[x<<1].lazy = tree[x].lazy;
tree[x<<1|1].lazy=tree[x].lazy;
tree[x].lazy=0;
}
}
void build(int x,int l,int r)
{
tree[x].L=l;
tree[x].R=r;
tree[x].sum=0;
tree[x].lazy=0;
if(l==r){
tree[x].sum=1;return;
}
int mid = (l+r)>>1;
build(x<<1,l,mid);
build(x<<1|1,mid+1,r);
PushUp(x);
}
void update(int x,int l,int r,int val)
{
int L = tree[x].L,R=tree[x].R;
if(l<=L&&R<=r)
{
tree[x].lazy=val;
tree[x].sum=(ll)(R-L+1)*val;
return ;
}
pushdown(x,R-L+1);
int mid = (L+R)>>1;
if(l<=mid)
update(x<<1,l,r,val);
if(mid<r)
update(x<<1|1,l,r,val);
PushUp(x);
}
ll query(int x,int l,int r)
{
ll res = 0;
int L=tree[x].L,R=tree[x].R;
if(l<=L&&r>=R){
return tree[x].sum;
}
pushdown(x,R-L+1);
int mid = (L+R)>>1;
if(l<=mid)
res += query(x<<1,l,r);
if(mid<r)
res += query(x<<1|1,l,r);
return res;
}
int main()
{
int T,n,m,left,right,sum;
scanf("%d",&T);
int cnt = 0;
while(T--)
{
cnt++;
scanf("%d%d",&n,&m);
build(1,1,n);
while(m--)
{
scanf("%d%d%d",&left,&right,&sum);
update(1,left,right,sum);
}
printf("Case %d: The total value of the hook is %lld.\n",cnt,query(1,1,n));
}
return 0;
}
hdu 1698 Just a Hook 【线段树+lazy】的更多相关文章
- HDU 1698 Just a Hook (线段树 成段更新 lazy-tag思想)
题目链接 题意: n个挂钩,q次询问,每个挂钩可能的值为1 2 3, 初始值为1,每次询问 把从x到Y区间内的值改变为z.求最后的总的值. 分析:用val记录这一个区间的值,val == -1表示这 ...
- HDU 1698 just a hook 线段树,区间定值,求和
Just a Hook Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1 ...
- [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 线段树+lazy-target 区间刷新
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- 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(线段树区间替换)
题目地址:pid=1698">HDU 1698 区间替换裸题.相同利用lazy延迟标记数组,这里仅仅是当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) Tota ...
- (简单) 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 线段树区间更新、
来谈谈自己对延迟标记(lazy标记)的理解吧. lazy标记的主要作用是尽可能的降低时间复杂度. 这样说吧. 如果你不用lazy标记,那么你对于一个区间更新的话是要对其所有的子区间都更新一次,但如果用 ...
随机推荐
- PAT 1017 A除以B(20)(代码)
1017 A除以B(20 分) 本题要求计算 A/B,其中 A 是不超过 1000 位的正整数,B 是 1 位正整数.你需要输出商数 Q 和余数 R,使得 A=B×Q+R 成立. 输入格式: 输入在一 ...
- De novo RNA-Seq Assembly Using De Bruijn Graphs
De novo RNA-Seq Assembly Using De Bruijn Graphs 2017-06-12 09:42:47 59 0 0 在说基因组的拼接之前,可 ...
- [Robot Framework] 调用ExcelLibrary
安装ExcelLibrary for Robot Framework 参考 : http://navinet.github.io/robotframework-excellibrary/ 打开wind ...
- vue ui之 iview 事件拦截
用过easyui的,应该清楚easyui组件有很多before事件拦截,有时候会特别重要. 最近在研究vue的ui组件iview虽然功能也不错,感觉还是没有像easyui那样强大,就比如before事 ...
- 【转载】 了解实时媒体的播放(RTP/RTCP 和 RTSP)
http://blog.csdn.net/span76/article/details/12913307 离线媒体只是用 Http协议去读取服务器端文件而已,而对于实时直播如何实现, 这里就要用到 R ...
- 【Linux】ODBC安装
ODBC介绍 ODBC是Open Database Connect 即开发数据库互连的简称,它是一个用于访问数据库的统一界面标准.ODBC引入一个公共接口以解决不同数据库潜在的不一致性,从而很好的保证 ...
- 20155312 张竞予 2006-2007-2 《Java程序设计》第四周学习总结
20155312 2006-2007-2 <Java程序设计>第四周学习总结 课堂笔记 Ctrl+shift+T调出三个窗口,分别是"vi编写代码","jav ...
- word中括号中公式对齐
在写论文中使用到括号,但没有对齐,如: 但是我需要对齐,如: 此时只需要在每行需要对齐的地方输入‘&’即可(此符号在专业型括号是不可见的,在线性中可见,上图为专业型),同样也可以有多个对齐点, ...
- 2018.11.24 poj3415Common Substrings(后缀数组+单调栈)
传送门 常数实在压不下来(蒟蒻开O(3)都过不了). 但有正确性233. 首先肯定得把两个字符串接在一起. 相当于heightheightheight数组被height<kheight<k ...
- layer.alert没有垂直居中
经查找是因为 <!DOCTYPE html> 这句没有写在整个页面的最顶部,将其放在整个页面的第一行就可以了. ps:原理不是很清楚