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. [JOISC2016]サンドイッチ

    题目大意: 一个$n\times m(n,m\leq400)$的网格图中,每个格子上放了两个三明治,摆放的方式分为'N'和'Z'两种.一个三明治可以被拿走当且仅当与该三明治的两条直角边相邻的三明治均被 ...

  2. 各种计算机编码与base64

    什么是base64,base64与Hex编码,ASCII编码,UTF-8编码都是什么关系 1 计算机开始之初,二进制 计算机所用的语言是什么呢?这个语言非常简单,只有0和1两种表示.0代表否,1代表是 ...

  3. 用gulp+webpack构建多页应用——记一次Node多页应用的构建过程

    通过参考网上的一些构建方法,当然也在开发过程中进行了一番实践,最终搭建了一套适用于当前多页应用的构建方案,当然该方案还处于draft版本,会在后续的演进过程中不断的优化. 个人觉得该方案的演进过程相对 ...

  4. Java 继承问题 -- 子类是否继承父类的私有属性

    理解一: 子类会继承父类的所有属性和方法,至于能不能直接访问,那就是访问权限的问题了. 例如:父类有个private String name; 属性.子类会继承下来,但子类访问不了,因为是privat ...

  5. 为Chrome多账户添加单独的快捷方式

    Chrome的多账户功能非常好用,每个账户都有自己的独立的收藏夹.个人设置等.但是,当你要使用的账户不是默认账户时,必须经过一个切换的操作.本文将简单的介绍一个如何各账户添加快捷方式,从而实现直接登陆 ...

  6. 【MySQL】undo,redo,2PC,恢复思维导图

    http://blog.itpub.net/22664653/viewspace-2131353/

  7. iOS教程:如何使用NSFetchedResultsController

    不知不觉我们已经来到了Core Data系列教程的最后一部分了,在这里我们要讨论如何使用NSFetchedResultsController来优化我们的应用,提高应用的运行速度,减少其内存占用. 你是 ...

  8. 【java】Stream的使用

    首先,给大家推荐一个好的地方:http://ifeve.com/stream/ 可以好好学一下 接下来,今天要删除数组中的某些元素,想到了之前用过的这个JDK8的Stream 1.Array转化为St ...

  9. Visual Studio Debug和Release的区别及obj的作用

    一.Debug和Release的区别      1.Debug:调试版本,包含调试信息,所以容量比Release大很多,并且不进行任何优化(优化会使调试复杂化,因为源代码和生成的指令间关系会更复杂), ...

  10. [置顶] kubernetes创建资源yaml文件例子--rc

    apiVersion: v1 #指定api版本,此值必须在kubectl apiversion中 kind: ReplicationController #指定创建资源的角色/类型 metadata: ...