HDU 1698 just a hook - 带有lazy标记的线段树(用结构体实现)
2017-08-30 18:54:40
writer:pprp
可以跟上一篇博客做个对比,
这种实现不是很好理解,上一篇比较好理解,但是感觉有的地方不够严密
代码如下:
/*
@theme:segmentation/interval T
@writer:pprp
@begin:15:26
@end:16:13
@declare: 使用lazy标记的线段树,HDU 1698
这次写的是带结构体的那种
@date:2017/8/30
*/ #include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <cstdlib> using namespace std;
const int maxn = ; struct tree
{
int r, l;
int v; //lazy标记
int mid;//中间的值
} T[maxn<<]; //test:ok
void pushDown(int rt)
{
T[rt<<].v = T[rt<<|].v = T[rt].v;
T[rt].v = -; //-1代表着
} //void build(int rt, int l, int r)
//{
// T[rt].l = l, T[rt].r = r;
// T[rt].v = 1; //因为这个题中认为每个节点的值都是1
// T[rt].mid = (l+r)>>1;
// if(l == r)return ;
// //递归求解对两边进行处理
// build(rt<<1,l,T[rt].mid);
// build(rt<<1|1,T[rt].mid+1,r);
//} void build(int rt,int L,int R)
{
T[rt].l=L,T[rt].r=R;T[rt].mid = (L+R)>>;
T[rt].v=;
if(L==R){return;}
build(rt<<,L,T[rt].mid);
build(rt<<|,T[rt].mid+,R);
} void update(int rt, int L, int R, int z)
{
if(L <= T[rt].l && R >= T[rt].r) //error find
{
T[rt].v = z;
return;
}
else
{
if(T[rt].v != -) //如果不是-1也就是说之前没有被pushdown过,那就pushdown
pushDown(rt);
if(L > T[rt].mid)
update(rt<<|,L,R,z);
else if(R <= T[rt].mid)
update(rt<<,L,R,z);
else //左右都有
{
update(rt<<,L,T[rt].mid,z);
update(rt<<|,T[rt].mid+,R,z);
}
}
} //找到根节点的sum值
int Find(int rt)
{
//判断根节点是否有lazy标记
if(T[rt].v != -)
return T[rt].v * (T[rt].r-T[rt].l+);
else
return Find(rt<<) + Find(rt<<|);
} int main()
{
//freopen("in.txt","r",stdin);
int N,n,m,i,j,k,v,c=;
scanf("%d",&N);
while(N--)
{
scanf("%d %d",&n,&m);
build(,,n);
for(i=;i<=m;++i){
scanf("%d%d%d",&j,&k,&v);
update(,j,k,v);
}
printf("Case %d: The total value of the hook is %d.\n",++c,Find());
}
return ;
}
HDU 1698 just a hook - 带有lazy标记的线段树(用结构体实现)的更多相关文章
- HDU1698 just a Hook - 带有lazy标记的线段树
2017-08-30 16:44:33 writer:pprp 上午刚刚复习了一下不带有lazy标记的线段树, 下午开始学带有lazy标记的线段树 这个是我看大佬代码敲的,但是出了很多问题, 这提醒我 ...
- 带有lazy标记的线段树
#include<bits/stdc++.h> using namespace std; ]; struct st{ int l,r,val,add; }tr[]; void build( ...
- HDU 1698 Just a Hook(线段树 区间替换)
Just a Hook [题目链接]Just a Hook [题目类型]线段树 区间替换 &题解: 线段树 区间替换 和区间求和 模板题 只不过不需要查询 题里只问了全部区间的和,所以seg[ ...
- HDU 1698 Just a Hook 区间更新 lazy标记
lazy标记 #include <iostream> #include <cstdio> #include <cstring> #include <sstre ...
- 题解报告:hdu 1698 Just a Hook(线段树区间修改+lazy懒标记的运用)
Problem Description In the game of DotA, Pudge’s meat hook is actually the most horrible thing for m ...
- HDU 1698 Just a Hook (线段树 成段更新 lazy-tag思想)
题目链接 题意: n个挂钩,q次询问,每个挂钩可能的值为1 2 3, 初始值为1,每次询问 把从x到Y区间内的值改变为z.求最后的总的值. 分析:用val记录这一个区间的值,val == -1表示这 ...
- hdu 1698 Just a Hook 【线段树+lazy】
题目 写了一天的线段树,这道题主要说明一下sum是赋值的,不是累加的,并且在push_down的时候lazy也是赋值的.因可能对懒标记的理解还不是很透彻吧. #include <iostream ...
- HDU 1698——Just a Hook——————【线段树区间替换、区间求和】
Just a Hook Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit ...
- HDU 1698 Just a Hook(线段树区间替换)
题目地址:pid=1698">HDU 1698 区间替换裸题.相同利用lazy延迟标记数组,这里仅仅是当lazy下放的时候把以下的lazy也所有改成lazy就好了. 代码例如以下: # ...
随机推荐
- jquery的强大选择器
$("#myELement") 选择id值等于myElement的元素,id值不能重复在文档中只能有一个id值是myElement所以得到的是唯一的元素 $("di ...
- Storm-源码分析- Component ,Executor ,Task之间关系
Component包含Executor(threads)的个数 在StormBase中的num-executors, 这对应于你写topology代码时, 为每个component指定的并发数(通过s ...
- virt-manager 操作 kvm虚拟机中鼠标不同步的问题
在/etc/libvirt/qemu下找到对应的xml配置文件 在<devices>标签下添加 <input type='tablet' bus='usb'/> 然后 vi ...
- 检查Linux服务器性能命令详解
如果你的Linux服务器突然负载暴增,如何在最短时间内找出Linux性能问题所在? 通过执行以下命令,可以在1分钟内对系统资源使用情况有个大致的了解. uptime dmesg | tail vmst ...
- 八、网页版消息推送SDK-WebSockets
介绍 由于项目组需求.最近在研究消息推送服务平台.结合业务和使用场景分析最终选择的是 Mosquitto 消息服务器. Mosquitto 服务器的安装.配置.集群搭建 我就不在这多说了.有兴趣的可以 ...
- (转)MFC中Doc,View,MainFrmae,App各指针的互相获取
App是应用域,所有的域中的东西都可以通过全局函数访问到它. MainFrame是主框架,也基本可以用全局函数访问到. MainFrame下是若干个ChildFrame,ChildFrame中若干个V ...
- PKU 1204 Word Puzzles(AC自动机)
题目大意:原题链接 给定一个字符串矩阵和待查找的单词,可以朝8个不同的方向查找,输出待查找单词第一个字母在矩阵中出现的位置和该单词被查到的方向. A~H代表8个不同的方向,A代表正北方向,其他依次以4 ...
- hdu3518 Boring counting
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=3518 题目: Boring counting Time Limit: 2000/1000 MS ...
- 分布式系统 SOA与中间件
在分布式系统中,有一个基础的理论 CAP,Consistency一致性 Availability可用性 Partition Tolerance分区容忍性,任何一个系统都不可能同时满足这三个条件(高富帅 ...
- Oracle中的substr()函数详解案例
1)substr函数格式 (俗称:字符截取函数) 格式1: substr(string string, int a, int b); 格式2:substr(string string, int a ...