整理下线段树吧 poj hotel
除了上次的新学的有 区间更新 延迟更新 区间合并
先说下区间更新以及延迟更新吧
既然是对区间的维护 在求解一些问题的时候 有的时候没有必要对所有的自区间都进行遍历 这个时候 延迟标记就派上用场了 (只有在需要的时候才对子区间更新)
struct node()
{
int l,r,len;
int flag=0;
}stu[maxn];
void pushdown(int i)
{
if(stu[i].flag)
{
stu[i*2].flag=stu[i*2+1].flag=stu[i].flag;
/// 还需要根据题目的意思 做一些处理
}
}
void updata(int l,int r,int i)//l , r 为需要查找区间的范围 x y 为目的区间
{
if(x<=l&&r<=y)//找到对应的区间
{
return;
}
int mid=(l+r)/2;
pushdown(i);
if(x<=mid) updata(l,mid,i*2);
if(y>mid) updata(mid+1,r,i*2+1);
}
poj hotel
#include<iostream>
#define maxn 50010
using namespace std;
int n,m,a,b;
int cmd;
struct stu
{
int r,l;
int flag;
int tlen,llen,rlen;
int up()
{
tlen=llen=rlen=(flag? 0:r-l+1);
}
};
stu mapp[maxn*4];
void build(int l,int r,int count)
{
mapp[count].l=l;
mapp[count].r=r;
mapp[count].tlen=mapp[count].llen=mapp[count].rlen=r-1+1;
mapp[count].flag=0;
if(l==r) return;
int mid=(l+r)/2;
build(l,mid,count*2);
build(mid+1,r,count*2+1);
}
void push(int count) //延迟更新
{
if(mapp[count].flag!=-1)
{
mapp[count*2].flag=mapp[count*2+1].flag=mapp[count].flag;
mapp[count].flag=-1;
mapp[count*2].up();
mapp[count*2+1].up();
} }
int que(int l,int r,int count)
{
if(mapp[count].l==mapp[count].r&&mapp[count].tlen) return mapp[count].l;
push(count);
if(mapp[count*2].tlen>=a) return que(l,r,count*2);
else if(mapp[count*2].rlen+mapp[count*2+1].llen>=a)
{
return mapp[count*2].r-mapp[count*2].rlen+1;
}
else if(mapp[count*2+1].tlen>=a) return que(l,r,count*2+1);
else return 0;
}
void updata(int l,int r,int v,int count)
{
if(mapp[count].l==l&&mapp[count].r==r)
{
mapp[count].flag=v;
mapp[count].up();
return;
}
push(count);
int mid=(mapp[count].l+mapp[count].r)/2;
if(r<=mid) updata(l,r,v,count*2);
else if(l>=mid+1) updata(l,r,v,count*2+1);
else
{
updata(l,mid,v,count*2);
updata(mid+1,r,v,count*2+1);
}
int tmp=max(mapp[count*2].tlen,mapp[count*2+1].tlen); // 后面为pushup函数
mapp[count].tlen=max(tmp,mapp[count*2].rlen+mapp[count*2+1].llen);
mapp[count].llen=mapp[count*2].llen;
mapp[count].rlen=mapp[count*2+1].rlen;
if(mapp[count*2].tlen==(mapp[count*2].r-mapp[count*2].l+1))
{
mapp[count].llen+=mapp[count*2+1].llen;
}
if(mapp[count*2+1].tlen==(mapp[count*2+1].r-mapp[count*2+1].l+1))
{
mapp[count].rlen+=mapp[count*2].rlen;
}
}
int main()
{
cin.sync_with_stdio(false);
while(cin>>n>>m)
{
build(1,n,1);
for(int i=0;i<m;i++)
{
cin>>cmd;
if(cmd==1)
{
cin>>a;
int ans=que(1,n,1);
cout<<ans<<endl;
if(ans) updata(ans,ans+a-1,1,1);
}
else
{
cin>>a>>b;
updata(a,a+b-1,0,1);
}
}
}
return 0;
}
整理下线段树吧 poj hotel的更多相关文章
- 线段树+离散化 POJ 2528 Mayor's posters
题目传送门 题意:在一面墙上贴海报,有先后顺序,问最后有多少张不同的海报(指的是没被覆盖或者只是部分覆盖的海报) 分析:这题数据范围很大,直接搞超时+超内存,需要离散化:离散化简单的来说就是只取我们需 ...
- poj 3468 整理一下线段树的写法
// 对于延迟更新,我们在updata 和query的时候 pushdown和pushup两个东西都要存在 #include <iostream> #include <cstdio& ...
- poj 2528 线段树+离散化
题意:在墙上贴一堆海报(只看横坐标,可以抽象成一线段),新海报可以覆盖旧海报.求最后能看到多少张海报 sol:线段树成段更新.铺第i张海报的时候更新sg[i].x~sg[i].y这一段为i. 然而坐标 ...
- poj 2828(线段树单点更新)
Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 18561 Accepted: 9209 Desc ...
- 洛谷 P3521 ROT-Tree Rotations [POI2011] 线段树
正解:线段树合并 解题报告: 传送门! 今天学了下线段树合并,,,感觉线段树相关的应用什么的还是挺有趣的,今天晚上可能会整理一下QAQ? 然后直接看这道题 现在考虑对一个节点nw,现在已经分别处理出它 ...
- UVA 12299 线段树 ( 单点跟新 , 区间查询)
题目链接:题意:在传统的RMQ的基础上加上一个操作:shift(i1,i2,i3...ik),表示将这些元素,依次向左移动一位(训练指南247页) #include <iostream> ...
- hdu 5091(线段树+扫描线)
上海邀请赛的一道题目,看比赛时很多队伍水过去了,当时还想了好久却没有发现这题有什么水题的性质,原来是道成题. 最近学习了下线段树扫描线才发现确实是挺水的一道题. hdu5091 #include &l ...
- HDU1394(线段树||树状数组)
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- 洛谷P4556 雨天的尾巴 线段树
正解:线段树合并 解题报告: 传送门! 考虑对树上的每个节点开一棵权值线段树,动态开点,记录一个max(num,id)(这儿的id,define了一下,,,指的是从小到大排QAQ 然后修改操作可以考虑 ...
随机推荐
- vscode远程代码同步
参考资料: https://blog.csdn.net/u012560340/article/details/83030680 https://github.com/liximomo/vscode-s ...
- 虚拟机VMware安装及Linux系统基础配置(CentOS 7)
PS: 我是 VMware 14 安装 CentOS 7 来配置Linux虚拟机,想要安装 Ubuntu 18.04 的可以自行其他搜或者参考博客:https://blog.csdn.net/gith ...
- 初识kaggle,以及记录 kaggle的使用
1.简介:Kaggle是一个数据建模和数据分析竞赛的平台.企业和研究者可在其上发布数据,统计学者和数据挖掘专家可在其上进行竞赛,通过“众包”的形式以产生最好的模型.Kaggle可以分为Competit ...
- git notes的使用
1. 获取notes git fetch origin refs/notes/*:refs/notes/* 2. 设置notes 2.1 git config --add core.notesRef ...
- Java并发包线程池之Executors、ExecutorCompletionService工具类
前言 前面介绍了Java并发包提供的三种线程池,它们用处各不相同,接下来介绍一些工具类,对这三种线程池的使用. Executors Executors是JDK1.5就开始存在是一个线程池工具类,它定义 ...
- @Component默认是单例还是多例?
@Component默认是单例还是多例? 答: @Component注解默认实例化的对象是单例,如果想声明成多例对象可以使用@Scope("prototype") @Repos ...
- Q_PROPERTY
1.在自定义控件中看到这个用法 2.Q_PROPERTY(double Min READ getMin WRITE setMin) 代表当前类有一个double属性叫Min,有一个读函数getMin, ...
- Qt编写自定义控件42-开关按钮
一.前言 从2010年进入互联网+智能手机时代以来,各种各样的APP大行其道,手机上面的APP有很多流行的元素,开关按钮个人非常喜欢,手机QQ.360卫士.金山毒霸等,都有很多开关控制一些操作,在Qt ...
- Struts 2 --ONGL介绍
先了解一下OGNL的概念 OGNL的全名称Object Graph Navigation Language.全称为对象图导航语言,是一种表达式语言.使用这种表达式语言,你可以通过某种表达式语法,存取J ...
- todolist形式的搜索框,分开组件写的,点击上下键时,框内显示当前选中的内容
### 首先 安装react 脚手架 cnpm install create-react-app -g //只需要在电脑上安装一次就好了,以后不用再下载了 ### 创建项目 crea ...