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. [BZOJ4398]福慧双修/[BZOJ2407]探险

    题目大意: 给定一个$n(n\leq40000)$个点$m(m\leq100000)$条边的有向图,求从$1$出发回到$1$的不经过重复结点的最短路. 思路: 首先Dijkstra求出从1出发到每个结 ...

  2. php的function() use($args)用法

    使用use返回 aaa aaa.使用函数传参数aaa bbb. use的参数必须是已经存在的,如果没有定义返回Notice: Undefined variable: word ,使用函数参数方式不需要 ...

  3. SSO [ OAuth2.0 ]

    1) SSO英文全称Single Sign On,单点登录. SSO是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统. 它包括可以将这次主要的登录映射到其他应用中用于同一个用户的 ...

  4. 六. 异常处理10.Java的内置异常

    在标准包java.lang中,Java定义了若干个异常类.前面的例子曾用到其中一些.这些异常一般是标准类RuntimeException的子类.因为java.lang实际上被所有的Java程序引入,多 ...

  5. 从vue.js的源码分析,input和textarea上的v-model指令到底做了什么

    v-model是 vue.js 中用于在表单表单元素上创建双向数据绑定,它的本质只是一个语法糖,在单向数据绑定的基础上,增加了监听用户输入事件并更新数据的功能:对,它本质上只是一个语法糖,但到底是一个 ...

  6. CMake 按照文件目录组织VS工程的筛选器

    macro(group_src_by_dir src_files) foreach(file_path ${${src_files}}) relative_path ${file_path}) str ...

  7. textarea限制字符输入方法

    function check(obj){ var Maxchar=20; if(obj.value.length>Maxchar){ //如果超出 obj.value=obj.value.sub ...

  8. Yii2数据库分页操作方法介绍

    本章节将介绍怎样怎样创建一个从数据表 country 中获取国家数据并显示出来的页面. 为了实现这个目标,你将会配置一个数据库连接.创建一个活动记录类,而且创建一个操作及一个视图. 贯穿整个章节,你将 ...

  9. spring 源码下载

    github spring 源码 导入Spring源码方法 java世界中的三大构建工具:ant,maven,gradle gradle 简介

  10. oracle经常使用函数(1)

    1.返回与指定的字符相应的十进制数 select ascii('A') A,ascii('z') a,ascii('12') 一打,ascii(' ') kg from dual; 2.返回与指定十进 ...