Hotel poj 3667
|
Language:
Default
Description The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and enjoy a vacation on the sunny shores of Lake Superior. Bessie, ever the competent travel agent, has named the Bullmoose Hotel on famed Cumberland Street as their vacation residence. This immense hotel has N (1 ≤ N ≤ 50,000) rooms all located on the same side of an extremely long hallway (all the better to see the lake, of course). The cows and other visitors arrive in groups of size Di (1 ≤ Di ≤ N) and approach the front desk to check in. Each group i requests a set of Di contiguous rooms from Canmuu, the moose staffing the counter. He assigns them some set of consecutive room numbers r..r+Di-1 if they are available or, if no contiguous set of rooms is available, politely suggests alternate lodging. Canmuu always chooses the value of r to be the smallest possible. Visitors also depart the hotel from groups of contiguous rooms. Checkout i has the parameters Xi and Di which specify the vacating of rooms Xi ..Xi +Di-1 (1 ≤ Xi ≤ N-Di+1). Some (or all) of those rooms might be empty before the checkout. Your job is to assist Canmuu by processing M (1 ≤ M < 50,000) checkin/checkout requests. The hotel is initially unoccupied. Input * Line 1: Two space-separated integers: N and M Output * Lines 1.....: For each check-in request, output a single line with a single integer r, the first room in the contiguous sequence of rooms to be occupied. If the request cannot be satisfied, output 0. Sample Input 10 6 Sample Output 1 Source |
[Submit] [Go Back] [Status] [Discuss]
题意 :
两种操作
1) 选一段 长度为 D 的可用区间 ,并占用它
2) 从 X 开始 长度为 D 的区间 标记为可用
开始时均为可用
区间范围 1--n
询问次数 m
1 ≤ n ,m < 50,000
思路:
考虑区间合并线段树,带lazy标记
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <functional> #define lson l,mid,pos<<1
#define rson mid+1,r,pos<<1|1 using namespace std; const int maxn = +;
int lazy[maxn<<];
int pre[maxn<<];
int suf[maxn<<];
int now[maxn<<];
int a[maxn];
void push_down(int l,int r,int pos)
{
if (l==r){
a[l]=lazy[pos]-;
pre[pos]=suf[pos]=now[pos]=(a[l]==)*;
lazy[pos]=;
return ;
}
int mid=(l+r)>>;
int lpos=pos<<,rpos=pos<<|;
a[l]=a[r]=a[mid]=a[mid+]=lazy[pos]-;
pre[pos]=suf[pos]=now[pos]=(a[l]==)*(r-l+);
lazy[lpos]=lazy[rpos]=lazy[pos];
lazy[pos]=;
}
void push_up(int l,int r,int pos)
{
int mid=(l+r)>>;
int lpos=pos<<,rpos=pos<<|;
if (lazy[lpos])push_down(lson);
if (lazy[rpos])push_down(rson);
if (pre[lpos]+l==mid+)pre[pos]=pre[lpos]+pre[rpos];
else pre[pos]=pre[lpos];
if (r-suf[rpos]==mid)suf[pos]=suf[lpos]+suf[rpos];
else suf[pos]=suf[rpos];
now[pos]=max(now[lpos],now[rpos]);
if (a[mid]==&&a[mid+]==)now[pos]=max(now[pos],suf[lpos]+pre[rpos]);
}
int getit(int l,int r,int pos)
{
if (lazy[pos])push_down(l,r,pos);
return now[pos];
}
void build(int l,int r,int pos)
{
lazy[pos]=;
if (l==r){
a[l]=;
suf[pos]=pre[pos]=now[pos]=;
return ;
}
int mid=(l+r)>>;
build(lson);
build(rson);
push_up(l,r,pos);
}
void upd(int l,int r,int pos,int l1,int r1,int v)
{
if (lazy[pos])push_down(l,r,pos);
if (l1<=l&&r1>=r){
lazy[pos]=v;
a[l]=a[r]=lazy[pos]-;
pre[pos]=suf[pos]=now[pos]=(a[l]==)*(r-l+);
return ;
}
int mid=(l+r)>>;
if (mid>=l1)upd(lson,l1,r1,v);
if (mid+<=r1)upd(rson,l1,r1,v);
push_up(l,r,pos);
}
int query(int l,int r,int pos,int len)
{
if (lazy[pos])push_down(l,r,pos);
if (now[pos]<len)return ;
if (l==r)return l;
int mid=(l+r)>>;
int lpos=pos<<,rpos=pos<<|;
if (getit(lson)>=len)return query(lson,len);
int v2=getit(rson);
int v3=suf[lpos];
if (a[mid]==&&a[mid+]==)v3=suf[lpos]+pre[rpos];
if (v3>=len)return mid-suf[lpos]+;
return query(rson,len);
}
int main()
{
int n,m;
int tr,x,y;
while (~scanf("%d%d",&n,&m)){
memset(a,,sizeof(a));
memset(lazy,,sizeof(lazy));
build(,n,);
for (int i=;i<m;i++){
scanf("%d%d",&tr,&x);
if (tr==){
int ans=query(,n,,x);
printf("%d\n",ans);
if (ans)
upd(,n,,ans,ans+x-,);
}else {
scanf("%d",&y);
upd(,n,,x,x+y-,);
}
}
}
return ;
}
* 在 vj 上看到了一个极快的vector暴力: 代码
* 分块这题应该也是可做的。(留坑,有空补上代码)
* 应该有瞎搞做法?(想到了补上)
Hotel poj 3667的更多相关文章
- Hotel - poj 3667(求连续子区间)
题意:有两种操作 1,从左往右找一个区间是 D 的连续序列,然后覆盖,返回区间最前面的数,如果没有输出0 2, 释放从L开始连续D的区间 分析:就是从左往右查找一个D的连续区间,可以使用三个值操作ls ...
- 线段树(区间合并) POJ 3667 Hotel
题目传送门 /* 题意:输入 1 a:询问是不是有连续长度为a的空房间,有的话住进最左边 输入 2 a b:将[a,a+b-1]的房间清空 线段树(区间合并):lsum[]统计从左端点起最长连续空房间 ...
- POJ 3667 Hotel(线段树)
POJ 3667 Hotel 题目链接 题意:有n个房间,如今有两个操作 1.找到连续长度a的空房间.入住,要尽量靠左边,假设有输出最左边的房间标号,假设没有输出0 2.清空[a, a + b - 1 ...
- POJ 3667 & HDU 3308 & HDU 3397 线段树的区间合并
看到讲课安排上 线段树有一节课"区间合并" 我是迷茫的 因为并没有见过 然后了解了一下题目 发现以前写过 还是很麻烦的树链剖分 大概是 解决带修改的区间查询"连续问题&q ...
- poj 3667 Hotel (线段树)
http://poj.org/problem?id=3667 Hotel Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 94 ...
- POJ 3667 Hotel (线段树区间合并)
题目链接:http://poj.org/problem?id=3667 最初给你n间空房,m个操作: 操作1 a 表示检查是否有连续的a间空房,输出最左边的空房编号,并入住a间房间. 操作2 a b ...
- POJ 3667 Hotel(线段树+区间合并)
http://poj.org/problem?id=3667 题意: 有N个房间,M次操作.有两种操作(1)"1a",表示找到连续的长度为a的空房间,如果有多解,优先左边的,即表示 ...
- POJ 3667 Hotel (线段树区间合并)
题目链接:http://poj.org/problem?id=3667 题目大意:一共有n个房间,初始时都是空的,现在有m个操作,操作有以下两种: 1.1 d :询问是否有连续d个空的房间,若有则输出 ...
- POJ 3667 Hotel(线段树 区间合并)
Hotel 转载自:http://www.cnblogs.com/scau20110726/archive/2013/05/07/3065418.html [题目链接]Hotel [题目类型]线段树 ...
随机推荐
- 网络防嗅探工具SniffJoke
网络防嗅探工具SniffJoke 在渗透测试中,通过网络嗅探,可以获取网络通信主机的各种信息.为了防止嗅探,Kali Linux提供了专用工具SniffJoke.该工具能够自动对用户的网络数据进行 ...
- luogu P1145 约瑟夫
题目描述 n个人站成一圈,从某个人开始数数,每次数到m的人就被杀掉,然后下一个人重新开始数,直到最后只剩一个人.现在有一圈人,k个好人站在一起,k个坏人站在一起.从第一个好人开始数数.你要确定一个最小 ...
- pr_debug、dev_dbg等动态调试二
内核版本:Linux-3.14 作者:彭东林 邮箱:pengdonglin137@163.com 下面我们简要分析 1: echo -n "file demo.c +p" > ...
- The beta-reports-active Entitlement
Q: How do I resolve the "beta-reports-active" code signing error? A: There are a number o ...
- 遨游maxthon打开页面一片黑色,百度地图等黑屏解决办法
遨游maxthon使用webkit极速核心,打开百度地图等页面一片黑色,黑屏了. 找了好久,不知道什么问题. 版本一样,都是4.4.xxx版本.另外一台机器又正常. 后来上傲游社区,好多人也有这个问题 ...
- docker开发之pyudev模块用法
一.实现功能:获取docker_id #docker数据源: [root@docker scripts]# docker ps -a CONTAINER ID IMAGE COMMAND CREATE ...
- Linux下查找、删除、替换命令
查看某目录下所有文件的个数: [root@localhost1 opt]# ls -l |grep "^-"|wc -l 查看某目录下所有文件的个数,包括子目录里面的: [root ...
- python良好的编程习惯
良好的编程习惯 2.1 在程序中是用丰富的注释,注释有助于其他程序员理解程序,有助于程序调试(发现和排除程序中的错误),并列出有用的信息.以后修改或更新代码时,注释还有助于理解当初自己编写的程序 2. ...
- Hive错误记录
创建表报错 Error: Error while processing statement: FAILED: Execution Error, return code 1 from org.apach ...
- CentOS6.4 安装 codeblocks-12.11
FROM: http://blog.csdn.net/theegao/article/details/8750239 一.下载 1. codeblocks-12.11-1.el6.x86_64.t ...