hdu 1698 线段数的区间更新 以及延迟更新
先说说区间更新和单点更新的区别 主要的区别是搜索的过程 前者需要确定一个区间 后者就是一个点就好了
贴上两者代码
void updata(int i)//单点更新
{
int l=stu[i].l;
int r=stu[i].r;
int mid=(l+r)/2;//二分咯
if(l==r&&r==x)//x为目标id 当左右节点相同的时候 就是找到这个数的时候
{
stu[i].maxx=y;
return;
}
if(l<=x&&x<=mid) updata(i*2);//向左找
else updata(i*2+1);//向右找
stu[i].maxx=max(stu[i*2].maxx,stu[i*2+1].maxx);//回溯的时候 更新max的数据
}
void update(int l,int r,int count)//x,y代表要更新的区间 //区间更新
{
if(l>=x&&r<=y)
{
mapp[count].sum=z*mapp[count].len;
mapp[count].flag=z;
return;
}
pushdown(count);
int mid=(l+r)/2;
if(x<=mid) update(l,mid,count*2);
if(y>=mid+1) update(mid+1,r,count*2+1);
mapp[count].sum=mapp[count*2].sum+mapp[count*2+1].sum;
}
在区间更新的时候 有的时候 我们不需要更新到底部 那么 这里引进一个延迟更新的概念 在搜索包含在我们需要的区间里面时 就可以返回了 那么在下次询问的时候 怎么保证底层的数据更新呢? 这里需要一个flag标记 如果当前的flag不为0的时候 向下更新 并更改当前的flag 比如在1-10标号的区间里面 我们要更新1-5的值
上代码
#include<iostream>
#define maxn 111111
using namespace std;
int n,q,x,y,z;
struct stu
{
int l,r,sum,flag,len;
};
stu mapp[maxn*4];
void build(int l,int r,int count)
{
mapp[count].l=l;
mapp[count].r=r;
mapp[count].flag=0;
mapp[count].len=r-l+1;
if(l==r)
{
mapp[count].sum=1;
mapp[count].flag=1;
return;
}
int mid=(l+r)/2;
build(l,mid,count*2);
build(mid+1,r,count*2+1);
mapp[count].sum=mapp[count*2].sum+mapp[count*2+1].sum;
}
void pushdown(int count)
{
if(mapp[count].flag)//如果有标记 向下更新
{
mapp[count*2].flag=mapp[count].flag;
mapp[count*2+1].flag=mapp[count].flag;
mapp[count*2].sum=mapp[count].flag*mapp[count*2].len;
mapp[count*2+1].sum=mapp[count].flag*mapp[count*2+1].len;
mapp[count].flag=0;//更新过一次 标记得消除
}
}//向下更新的过程
void update(int l,int r,int count)//x,y代表要更新的区间
{
if(l>=x&&r<=y)
if(l==r)
{
mapp[count].sum=z*mapp[count].len;
mapp[count].flag=z;
return;
}
pushdown(count);
int mid=(l+r)/2;
if(x<=mid) update(l,mid,count*2);
if(y>=mid+1) update(mid+1,r,count*2+1);
mapp[count].sum=mapp[count*2].sum+mapp[count*2+1].sum;
}
int main()
{
cin.sync_with_stdio(false);
int t;
cin>>t;
int casee=1;
while(t--)
{
cin>>n;
build(1,n,1);
cin>>q;
while(q--)
{
cin>>x>>y>>z;
update(1,n,1);
}
cout<<"Case "<<casee++<<": The total value of the hook is ";
cout<<mapp[1].sum<<"."<<endl;
}
return 0;
}
hdu 1698 线段数的区间更新 以及延迟更新的更多相关文章
- HDU 1698 <线段树,区间set>
题目连接 题意: 一条长为N的铜链子,每个结点的价值为1.有两种修改,l,r,z; z=2:表示把[l,r]区间内链子改为银质,价值为2. z=3:表示把[l,r]区间内链子改为金质,价值为3. 思路 ...
- HDU(1698),线段树区间更新
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698 区间更新重点在于懒惰标记. 当你更新的区间就是整个区间的时候,直接sum[rt] = c*(r- ...
- HDU 1698 线段树 区间更新求和
一开始这条链子全都是1 #include<stdio.h> #include<string.h> #include<algorithm> #include<m ...
- HDU 1698 (线段树 区间更新) Just a Hook
有m个操作,每个操作 X Y Z是将区间[X, Y]中的所有的数全部变为Z,最后询问整个区间所有数之和是多少. 区间更新有一个懒惰标记,set[o] = v,表示这个区间所有的数都是v,只有这个区间被 ...
- hdu 1698 线段树 区间更新 区间求和
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 2795 Billboard 线段树,区间最大值,单点更新
Billboard Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- HDU - 1698 线段树区间修改,区间查询
这就是很简单的基本的线段树的基本操作,区间修改,区间查询,对区间内部信息打上laze标记,然后维护即可. 我自己做的时候太傻逼了...把区间修改写错了,对给定区间进行修改的时候,mid取的是节点的左右 ...
- hdu 1698 线段树成段更新
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698 #include <cstdio> #include <cmath> # ...
- E - Just a Hook HDU - 1698 线段树区间修改区间和模版题
题意 给出一段初始化全为1的区间 后面可以一段一段更改成 1 或 2 或3 问最后整段区间的和是多少 思路:标准线段树区间和模版题 #include<cstdio> #include& ...
随机推荐
- Python日志库logging总结-可能是目前为止将logging库总结的最好的一篇文章
在部署项目时,不可能直接将所有的信息都输出到控制台中,我们可以将这些信息记录到日志文件中,这样不仅方便我们查看程序运行时的情况,也可以在项目出现故障时根据运行时产生的日志快速定位问题出现的位置. 1. ...
- c++ 套接字 --->2002 java NIO --->netty
c++ 套接字 --->2002 java NIO --->netty
- 对请求数据的格式化 方案 Spring Cloud Gateway features:
对请求数据的格式化 例如 {body:{}}--->{data:{}} 执行阶段概念 · OpenResty最佳实践 · 看云 https://www.kancloud.cn/kancloud/ ...
- es6对象复制合并 Object.assign
对象的复制 var obj= { a: 1 }; var copy = Object.assign({}, obj); console.log(copy); //{ a: 1 } 对象的合并和封装 v ...
- osg fbx模型点击节点,对应节点染色
class CPickHandler :public osgGA::GUIEventHandler { public: CPickHandler(osgViewer::Viewer *viewer) ...
- 10--STL无序容器(Unordered Containers)
一:无序容器简介 Unordered Containers也是一种关联式容器.其中元素是分散,没有定性的排列(不是图中那样松散).其中元素可能在某一次操作后改变原来的位置. 哈希表的链地址法,更能表现 ...
- Elasticsearch 7.x文档基本操作(CRUD)
官方文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/docs.html 1.添加文档 1.1.指定文档ID PUT ...
- thymeleaf自定义属性
例如我想给添加一个属性data-page,可以根据(SpringEL/Ognl)表达式计算获得. 需求: 期望效果 app个数大于0,有数据时链接 <xxx data-page="/a ...
- LeetCode_21. Merge Two Sorted Lists
21. Merge Two Sorted Lists Easy Merge two sorted linked lists and return it as a new list. The new l ...
- easyui-datagrid配置宽度高度自适应
在style属性中,去除之前添加的width和height属性(如果有的话),然后添加"fit:false"即可.