Hotel
poj3667:http://poj.org/problem?id=3667
题目大意:Hotel有N(1 ≤ N ≤ 50,000)间rooms,并且所有的rooms都是连续排列在同一边,groups需要check in 房间,要求房间的编号为连续的r..r+Di-1并且r是最小的;visitors同样可能check out,并且他们每次check out都是编号为Xi ..Xi +Di-1 (1 ≤ Xi ≤ N-Di+1)的房间,题目的输入有两种样式:
1 a : groups需要check in a间编号连续的房间
2 a b : visitors check out 房间,其中房间编号是 a…a+b-1要求对于每次request,输出为groups分配数目为a的房间中编号最小的房间编号
题解:用 线段树维护区间的连续最大值,最大左连续,最大右连续。查询时,如果当前节点的sum值大等于val,则看该区间的左连续,若左连续大等于val,直接返回left;否则,看左二子,如果左儿子的sum值大于等于val,则进入左儿子进行查询,如果不满足,再看左儿子的右连续+右儿子的左连续是否大等于val,若大于则直接返回左儿子右连续的起点值,否则进入右儿子进行查询。否则,返回0.如果返回的不是0,就进行区间更新。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
int u,v,w;
int n,m;
struct Node{
int left;
int right;
int lsum,rsum,sum;
int lazy;
}node[*];
void build(int l,int r,int idx){
node[idx].left=l;
node[idx].right=r;
node[idx].lazy=;//lazy标记
if(l==r){
node[idx].sum=node[idx].lsum=node[idx].rsum=;
return;
}
int mid=(l+r)/;
build(l,mid,idx<<);
build(mid+,r,idx<<|);
node[idx].sum=node[idx].lsum=node[idx].rsum=r-l+;
}
void pushup(int idx){
if(node[idx<<].lsum==node[idx<<].right-node[idx<<].left+)
node[idx].lsum=node[idx<<].lsum+node[idx<<|].lsum;
else
node[idx].lsum=node[idx<<].lsum;
if(node[idx<<|].rsum==node[idx<<|].right-node[idx<<|].left+)
node[idx].rsum=node[idx<<].rsum+node[idx<<|].rsum;
else
node[idx].rsum=node[idx<<|].rsum;
node[idx].sum=node[idx<<].rsum+node[idx<<|].lsum;
node[idx].sum=max(node[idx<<].sum,max(node[idx].sum,node[idx<<|].sum));
}
void pushdown(int idx){
if(node[idx].lazy==){
node[idx<<].sum=node[idx<<|].sum=;
node[idx<<].lsum=node[idx<<].rsum=;
node[idx<<|].lsum=node[idx<<|].rsum=;
node[idx<<].lazy=node[idx<<|].lazy=;//很重要,开始忘记往下传递问题
node[idx].lazy=;
}
if(node[idx].lazy==){
node[idx<<].lsum=node[idx<<].rsum=node[idx<<].sum=node[idx<<].right-node[idx<<].left+;
node[idx<<|].lsum=node[idx<<|].rsum=node[idx<<|].sum=node[idx<<|].right-node[idx<<|].left+;
node[idx<<].lazy=node[idx<<|].lazy=;
node[idx].lazy=;
}
}
void update1(int l,int r,int val,int idx){
if(node[idx].left==l&&node[idx].right==r&&val==){
node[idx].sum=;
node[idx].lsum=node[idx].rsum=;
node[idx].lazy=;
return;
}
if(node[idx].left==l&&node[idx].right==r&&val==){
node[idx].sum=node[idx].right-node[idx].left+;
node[idx].lsum=node[idx].rsum=node[idx].right-node[idx].left+;
node[idx].lazy=;
return;
}
pushdown(idx);
int mid=(node[idx].right+node[idx].left)/;
if(mid>=r)update1(l,r,val,idx<<);
else if(mid<l)update1(l,r,val,idx<<|);
else{
update1(l,mid,val,idx<<);
update1(mid+,r,val,idx<<|);
}
pushup(idx);
}
int update2(int val,int idx){
if(node[idx].sum>=val){
pushdown(idx);
if(node[idx].lsum>=val){
return node[idx].left;
}
else if(node[idx<<].sum>=val)return update2(val,idx<<);
else if(node[idx<<].rsum+node[idx<<|].lsum>=val)
return node[idx<<].right-node[idx<<].rsum+;
else
return update2(val,idx<<|);
}
else {
return ;
}
}
int main(){
scanf("%d%d",&n,&m);
build(,n,);
for(int i=;i<=m;i++){
cin>>u;
if(u==){
cin>>v;
int ans=update2(v,);
printf("%d\n",ans);
if(ans>)//这里不要有分好,我几次就是死在了这里
update1(ans,ans+v-,,);
}
else{
cin>>v>>w;
update1(v,v+w-,,);
}
}
}
Hotel的更多相关文章
- POJ 3667 Hotel(线段树 区间合并)
Hotel 转载自:http://www.cnblogs.com/scau20110726/archive/2013/05/07/3065418.html [题目链接]Hotel [题目类型]线段树 ...
- ACM: Hotel 解题报告 - 线段树-区间合并
Hotel Time Limit:3000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Description The ...
- HDU - Hotel
Description The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and e ...
- 【POJ3667】Hotel
Description The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and e ...
- POJ-2726-Holiday Hotel
Holiday Hotel Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8302 Accepted: 3249 D ...
- Method threw 'org.hibernate.exception.SQLGrammarException' exception. Cannot evaluate com.hotel.Object_$$_jvst485_15.toString()
数据库字段和类Object属性不匹配,Method threw 'org.hibernate.exception.SQLGrammarException' exception. Cannot eval ...
- poj 3667 Hotel(线段树,区间合并)
Hotel Time Limit: 3000MSMemory Limit: 65536K Total Submissions: 10858Accepted: 4691 Description The ...
- [POJ3667]Hotel(线段树,区间合并)
题目链接:http://poj.org/problem?id=3667 题意:有一个hotel有n间房子,现在有2种操作: 1 a,check in,表示入住.需要a间连续的房子.返回尽量靠左的房间编 ...
- 【BZOJ】【3522】【POI2014】Hotel
暴力/树形DP 要求在树上找出等距三点,求方案数,那么用类似Free Tour2那样的合并方法,可以写出: f[i][j]表示以 i 为根的子树中,距离 i 为 j 的点有多少个: g[i][j]表示 ...
- Codeforces Round #336 (Div. 2) A. Saitama Destroys Hotel 模拟
A. Saitama Destroys Hotel Saitama accidentally destroyed a hotel again. To repay the hotel company ...
随机推荐
- OpenCV 2.4.9
2014.4.25 感谢那些參加开发.发送错误报告以及通过其它方式帮助我们的全部人和公司. 源代码如今已经可以从SourceForge和Github上下载了. 2.4.9版本号的帮助文章也更新到如今的 ...
- 一个Web Project引用多个Java Project在Eclipse下的配置--转载
项目结构: 项目由一个Web Project和多个Java Project构成,Web Project需要引用其它Java Project的类和Jar包.开发时用Eclipse3.5和Tomcat调试 ...
- abc - zx
诛仙青云志 第26集 第25集 第24集 第23集 第22集 第21集 第20集 第19集 第18集 第17集 第16集 第15集 第14集 ...
- 9.28noip模拟试题
1.栅栏迷宫 田野上搭建了一个黄金大神专用的栅栏围成的迷宫.幸运的是,在迷宫的边界上留出了两段栅栏作为迷宫的出口.更幸运的是,所建造的迷宫是一个“完美的”迷宫:即你能从迷宫中的任意一点找到一条走出迷宫 ...
- div宽度设置无效问题解决
问题描述: 要设置两个div在同一行显示,都加入了display:inline样式,但是其中一个div的宽度设置无效,在浏览器显示它的宽度始终是1003px. 解决办法: 方法1/给div加入样式:f ...
- discuz论坛几种安全策略(二)
大头看了我给的方案,http://www.cnblogs.com/itsmylife/p/4012000.html 今早给的回复如下: “ 补充几点,供参考: 第一条,配置一下允许上传文件的格式以及大 ...
- Android 安全性和权限
自定义权限 permission <permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT& ...
- session marked for kill处理oracle中杀不掉的锁
ora-00031:session marked for kill处理oracle中杀不掉的锁 一些ORACLE中的进程被杀掉后,状态被置为"killed",但是锁定的资源很长 ...
- WCF存储图片到指定文件夹下
string path = System.IO.Directory.GetCurrentDirectory() + @"\POIImages\"; Guid imgid = Gui ...
- 在window系统下配置login.sql
在window系统下配置login.sql 他的位置是登录用户的文件夹,我的win7系统位置是: C:\Users\Administrator 我的login.sql下载地址: http://file ...