[poj 3367] Hotel 线段树应用
2016-04-05 08:59
218 查看
Hotel
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 15205 Accepted: 6583
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
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
USACO 2008 February Gold
题目链接:http://poj.org/problem?id=3667
题意:Hotel有N(1 ≤ N ≤ 50,000)间rooms,并且所有的rooms都是连续排列在同一边,groups需要check in 房间,要求房间的编号为连续的r..r+Di-1并且r是最小的;visitors同样可能check out,并且他们每次check out都是编号为Xi ..Xi +Di-1 (1 ≤ Xi ≤ N-Di+1)的房间,题目的输入有两种样式:
要求对于每次request,输出为groups分配数目为a的房间中编号最小的房间编号;
思路:类比最大连续子段和 tr.lx代表左端最长的房间数; tr.rx代表右端最长的房间数;
更新:
能左找左找–》query 函数
代码
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 15205 Accepted: 6583
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
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
USACO 2008 February Gold
题目链接:http://poj.org/problem?id=3667
题意:Hotel有N(1 ≤ N ≤ 50,000)间rooms,并且所有的rooms都是连续排列在同一边,groups需要check in 房间,要求房间的编号为连续的r..r+Di-1并且r是最小的;visitors同样可能check out,并且他们每次check out都是编号为Xi ..Xi +Di-1 (1 ≤ Xi ≤ N-Di+1)的房间,题目的输入有两种样式:
1 a : groups需要check in a间编号连续的房间 2 a b : visitors check out 房间,其中房间编号是 a…a+b-1
要求对于每次request,输出为groups分配数目为a的房间中编号最小的房间编号;
思路:类比最大连续子段和 tr.lx代表左端最长的房间数; tr.rx代表右端最长的房间数;
更新:
int mid=(l+r)>>1; if(tr[lson].lx==mid-l+1) tr[id].lx=tr[lson].rx+tr[rson].lx; else tr[id].lx=tr[lson].lx; if(tr[rson].rx==r-mid) tr[id].rx=tr[rson].lx+tr[lson].rx; else tr[id].rx=tr[rson].rx;
能左找左找–》query 函数
int query(int id,int l,int r,int k) { //cout<<id<<" "<<l<<" "<<r<<" "<<tr[lson].val<<endl; int mid=(l+r)>>1; if(l!=r) push_down(id,l,r); if(tr[id].val<k) return 0;//找不到 if(tr[id].lx>=k) return l; //左段开始的长度够 if(tr[lson].val>=k) return query(lson,l,mid,k);//左找 if(tr[rson].lx+tr[lson].rx>=k) return mid-tr[lson].rx+1;//跨区间 if(tr[rson].val>=k) return query(rson,mid+1,r,k); return 0; }
代码
#include<iostream>
#include<string.h>
#include<stdio.h>
#define lson (id*2)
#define rson (id*2+1)
using namespace std;
struct node{
int lx,rx,val,lazy;
}tr[50002*4];
int n,m;
void push_down(int id,int l,int r)
{
if(tr[id].lazy==-1) return;
int mid=(l+r)>>1;
tr[lson].lazy=tr[rson].lazy=tr[id].lazy;
if(tr[id].lazy==1)
{
tr[lson].lx=tr[rson].lx=tr[lson].rx=tr[rson].rx=tr[lson].val=tr[rson].val=0;
}
else
{
tr[lson].lx=tr[lson].rx=tr[lson].val=mid-l+1;
tr[rson].lx=tr[rson].rx=tr[rson].val=r-mid;
}
tr[id].lazy=-1;
}
void push_up(int id,int l,int r)
{int mid=(l+r)>>1; if(tr[lson].lx==mid-l+1) tr[id].lx=tr[lson].rx+tr[rson].lx; else tr[id].lx=tr[lson].lx; if(tr[rson].rx==r-mid) tr[id].rx=tr[rson].lx+tr[lson].rx; else tr[id].rx=tr[rson].rx;
tr[id].val=max(max(tr[lson].val,tr[rson].val),tr[lson].rx+tr[rson].lx);
}
int query(int id,int l,int r,int k)
{
//cout<<id<<" "<<l<<" "<<r<<" "<<tr[lson].val<<endl;
int mid=(l+r)>>1;
if(l!=r)
push_down(id,l,r);
if(tr[id].val<k) return 0;
if(tr[id].lx>=k) return l;
if(tr[lson].val>=k) return query(lson,l,mid,k);
if(tr[rson].lx+tr[lson].rx>=k) return mid-tr[lson].rx+1;
if(tr[rson].val>=k) return query(rson,mid+1,r,k);
return 0;
}
void add(int id,int L,int R,int l,int r,int v)
{
if(R<L||r<L||l>R) return ;
if(L>=l&&R<=r)
{
tr[id].lazy=v;
if(v==0)
{
tr[id].lx=tr[id].rx=tr[id].val=R-L+1;
}
else
{
tr[id].lx=tr[id].rx=tr[id].val=0;
}
//cout<<" "<<id<<" "<<L<<" "<<R<<" "<<tr[id].lx<<" "<<tr[id].rx<<" "<<tr[id].val<<endl;
return ;
}
int mid=(L+R)>>1;
push_down(id,L,R);
if(l<=mid)
add(lson,L,mid,l,r,v);
if(r>mid)
add(rson,mid+1,R,l,r,v);
push_up(id,L,R);
//cout<<" "<<id<<" "<<L<<" "<<R<<" "<<tr[id].lx<<" "<<tr[id].rx<<" "<<tr[id].val<<endl;
}
void build(int id,int l,int r)
{
tr[id].rx=tr[id].lx=tr[id].val=r-l+1;
tr[id].lazy=-1;
if(l==r) return;
int mid=(l+r)>>1;
build(lson,l,mid);
build(rson,mid+1,r);
}
int main()
{
int aa,bb,cc;
scanf("%d%d",&n,&m);
build(1,1,n);
for(int i=1;i<=m;i++)
{
scanf("%d%d",&aa,&bb);
if(aa==1)
{
int pos=query(1,1,n,bb);
printf("%d\n",pos);
if(pos)
add(1,1,n,pos,pos+bb-1,1);
}
else
{
scanf("%d",&cc);
add(1,1,n,bb,bb+cc-1,0);
}
}
}
相关文章推荐
- 个人技术栈
- DUNS邓白氏码申请之旅
- jquery js 重置表单 reset()具体实现代码
- storm单机伪分布详细
- 数据库范式——通俗易懂
- Java 设计模式 之 单例模式(Singleton)
- sscanf函数 和cin流的问题
- arcgis python 更新顺序号
- 2016年4月5日08:53:40
- 利用Target来管理项目不同环境的配置
- storm 网上中文资料搜集大全
- C# 操作INI配置文件
- JAXB完毕XML与Java对象的互转
- 基于.NET平台常用的框架整理
- C# Distinct使用,支持对象的相等比较
- 黑马笔记总结(二)
- iOS中传感器的基本使用
- JBPM学习(四):运行流程实例
- 常用数字签名算法RSA与DSA的Java程序内实现示例
- 浅谈Java的几种设计模式