Language:
Default
 
                                               

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 18020   Accepted: 7823

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 ≤ XiN-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
* Lines 2..M+1: Line i+1 contains request expressed as one of two possible formats: (a) Two space separated integers representing a check-in request: 1 and Di (b) Three space-separated integers representing a check-out: 2, Xi, and Di

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
1 3
1 3
1 3
1 3
2 5 5
1 6

Sample Output

1
4
7
0
5

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的更多相关文章

  1. Hotel - poj 3667(求连续子区间)

    题意:有两种操作 1,从左往右找一个区间是 D 的连续序列,然后覆盖,返回区间最前面的数,如果没有输出0 2, 释放从L开始连续D的区间 分析:就是从左往右查找一个D的连续区间,可以使用三个值操作ls ...

  2. 线段树(区间合并) POJ 3667 Hotel

    题目传送门 /* 题意:输入 1 a:询问是不是有连续长度为a的空房间,有的话住进最左边 输入 2 a b:将[a,a+b-1]的房间清空 线段树(区间合并):lsum[]统计从左端点起最长连续空房间 ...

  3. POJ 3667 Hotel(线段树)

    POJ 3667 Hotel 题目链接 题意:有n个房间,如今有两个操作 1.找到连续长度a的空房间.入住,要尽量靠左边,假设有输出最左边的房间标号,假设没有输出0 2.清空[a, a + b - 1 ...

  4. POJ 3667 & HDU 3308 & HDU 3397 线段树的区间合并

    看到讲课安排上 线段树有一节课"区间合并" 我是迷茫的 因为并没有见过 然后了解了一下题目 发现以前写过 还是很麻烦的树链剖分 大概是 解决带修改的区间查询"连续问题&q ...

  5. poj 3667 Hotel (线段树)

    http://poj.org/problem?id=3667 Hotel Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 94 ...

  6. POJ 3667 Hotel (线段树区间合并)

    题目链接:http://poj.org/problem?id=3667 最初给你n间空房,m个操作: 操作1 a 表示检查是否有连续的a间空房,输出最左边的空房编号,并入住a间房间. 操作2 a b ...

  7. POJ 3667 Hotel(线段树+区间合并)

    http://poj.org/problem?id=3667 题意: 有N个房间,M次操作.有两种操作(1)"1a",表示找到连续的长度为a的空房间,如果有多解,优先左边的,即表示 ...

  8. POJ 3667 Hotel (线段树区间合并)

    题目链接:http://poj.org/problem?id=3667 题目大意:一共有n个房间,初始时都是空的,现在有m个操作,操作有以下两种: 1.1 d :询问是否有连续d个空的房间,若有则输出 ...

  9. POJ 3667 Hotel(线段树 区间合并)

    Hotel 转载自:http://www.cnblogs.com/scau20110726/archive/2013/05/07/3065418.html [题目链接]Hotel [题目类型]线段树 ...

随机推荐

  1. sharepoint 2013 和 office web apps server 2013集成

    环境: 三台服务器  系统:window 2008 R2server01: 192.168.10.162(office web app)server02: 192.168.10.163(AD)serv ...

  2. HDU 4034 Graph Floyd最短路

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4034 题意: 给你一个最短路的表,让你还原整个图,并使得边最少 题解: 这样想..这个表示通过floy ...

  3. final-第十章

    1,nas概念 NAS就是一种直接连接到用户网络中,并具有信息存储功能的硬件设备. NAS是由处理器,文件服务管理模块,和存储部分等组成的. 2,san概念 SAN是一种利用光纤集线器,光纤路由器,光 ...

  4. UIPanGestureRecognizer判断滑动的方向

    .h文件 CGFloat const gestureMinimumTranslation = 20.0 ; typedef enum : NSInteger { kCameraMoveDirectio ...

  5. How to Use Auto Layout in XCode 6 for iOS 7 and 8 Development

    The Auto Layout is available on the Storyboard for iOS or OS X development since XCode 5. But, I did ...

  6. 跟着Sedgewick学算法(week 1 ElementarySort)

     链接https://www.evernote.com/shard/s408/sh/dbe0167f-20e0-41c4-a49b-75717ad98695/461148482ffb6add092be ...

  7. CURL简单使用

    学习地址:https://yq.aliyun.com/articles/33262 curl的简单使用步骤 要使用cURL来发送url请求,具体步骤大体分为以下四步: 1.初始化2.设置请求选项3.执 ...

  8. selenium firefox设置代理

    from selenium import webdriver profile = webdriver.FirefoxProfile() profile.set_preference('network. ...

  9. SWIG 多语言接口变换 【转】

    一.             SWIG 是Simple Wrapper and Interface Generator的缩写,是一个帮助使用C或者C++编写的软件创建其他编语言的API的工具.例如,我 ...

  10. 2017.10.13 unable to open debugger port(127.0.0.1:10308)

    参考来自:http://blog.csdn.net/qq_34360219/article/details/76169653 1.场景 突然间IDEA就跑不起项目了,报了如下的错误:unable to ...