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 [题目类型]线段树 ...
 
随机推荐
- POJ2955 Brackets(区间DP)
			
给一个括号序列,求有几个括号是匹配的. dp[i][j]表示序列[i,j]的匹配数 dp[i][j]=dp[i+1][j-1]+2(括号i和括号j匹配) dp[i][j]=max(dp[i][k]+d ...
 - 【bzoj3173】【Tjoi2013】【最长上升子序列】treap+dp二分优化
			
[pixiv] https://www.pixiv.net/member_illust.php?mode=medium&illust_id=61560361 向大(hei)佬(e)实力学(di ...
 - Oracle PL/SQL入门之慨述
			
Oracle PL/SQL入门之慨述 一.PL/SQL出现的目的 结构化查询语言(Structured Query Language,简称SQL)是用来访问关系型数据库一种通用语言,它属于第四代语言( ...
 - 程设刷题 | 编译C++文件出现to_string is not a member of std 或者 to_string was not declared in this scope的解决方法
			
写在前面 原文链接:Enabling string conversion functions in MinGW C++在将整型.浮点型.长整型等数据类型转换为字符串时,可使用<string> ...
 - 集合框架(02)List
			
List的类型和特点: ArrayList:底层的数据结构使用的是数组结构.特点:查询的速度很快,但是增删稍慢 线程不同步 LinKedList:底层使用的链表数据结构.特点:增删的速度很快,查询稍慢 ...
 - linux PHP 安装及 GD库安装
			
linux GD库安装 GD 安裝 第一部需要做的是先要安裝 GD 到系統內,而安裝 GD 前需要安裝 jpeg-6b, libpng, zlib, freetype.以下是下载网址:GD 2.0.3 ...
 - 理解内存----优化SQL Server内存配置
			
http://blog.csdn.net/burgess_liu/article/details/17757655
 - 深入理解MySQL中的Redo、Undo、MVCC
			
http://edu.csdn.net/course/detail/3495 http://edu.csdn.net/courses/o317_a3/云计算大数据
 - SQL Reverse函数
			
原文:SQL Reverse函数 Sql sever里面有个自带的reverse函数,这个函数的主要功能是把一个字符产反转.比如对于: select REVERSE('hello,world') 将得 ...
 - 【java】java反射机制,动态获取对象的属性和对应的参数值,并属性按照字典序排序,Field.setAccessible()方法的说明【可用于微信支付 签名生成】
			
方法1:通过get()方法获取属性值 package com.sxd.test.controller; public class FirstCa{ private Integer num; priva ...