poj 3667 Hotel(线段树 区间合并 区间更新 )
2018-04-02 22:13
501 查看
Hotel
DescriptionThe 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
4000
single integer r, the first room in the contiguous sequence of rooms to be occupied. If the request cannot be satisfied, output 0.
Sample Input10 6
1 3
1 3
1 3
1 3
2 5 5
1 6
Sample Output1
4
7
0
5题意: 一家酒店有 n 个房间 现在有m个情况需要你去处理 如果是 cin op 如果op==1 表示有 x 个人要办理入住, 但是有一个要求就是 需要 房间号是连续的 如果存在连续的房间就 输出第一个房间的 编号 否则 输出0
如果op==2 表示 x,y 从x房间开始的 y 个人 要退房拜拜.
思路: 当然容易想到 是线段树 但是比较麻烦的就是退房后的处理 . 需要对结点的左右情况进行考虑. 然后更新线段树 .
其实比较难的地方也就是 push_up 和push_down 函数.
结构体中lsum 表示 这个结点 从他的左端点开始最长的连续的空房间的数量. rsum 表示 从右结点 开始向左连续的空房间的数量. sum 表示这个结点中的最大连续空房间的数量.
代码: #include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define N 50005
using namespace std;
struct node
{
int l,r;
int lazy;
int sum;
int lsum,rsum;
}tree[N<<2];
int n,m;
void push_up(int i)
{
tree[i].lsum=tree[i<<1].lsum; tree[i].rsum=tree[i<<1|1].rsum;
int len=tree[i<<1].r-tree[i<<1].l+1;
if(len==tree[i<<1].lsum) tree[i].lsum+=tree[i<<1|1].lsum;
len=tree[i<<1|1].r-tree[i<<1|1].l+1;
if(len==tree[i<<1|1].rsum) tree[i].rsum+=tree[i<<1].rsum;
tree[i].sum=max(max(tree[i<<1].sum,tree[i<<1|1].sum),tree[i<<1].rsum+tree[i<<1|1].lsum);
return ;
}
void push_down(int i)
{
int f=tree[i].lazy;
if(f==1) // 置 满
{
tree[i<<1].sum=tree[i<<1].lsum=tree[i<<1].rsum=0;
tree[i<<1].lazy=1;
tree[i<<1|1].sum=tree[i<<1|1].rsum=tree[i<<1|1].lsum=0;
tree[i<<1|1].lazy=1;
}
else // 清 0
{
int len=tree[i<<1].r-tree[i<<1].l+1;
tree[i<<1].sum=tree[i<<1].lsum=tree[i<<1].rsum=len;
tree[i<<1].lazy=0;
len=tree[i<<1|1].r-tree[i<<1|1].l+1;
tree[i<<1|1].sum=tree[i<<1|1].rsum=tree[i<<1|1].lsum=len;
tree[i<<1|1].lazy=0;
}
tree[i].lazy=-1;
//push_up(i);
return ;
}
void build(int i,int l,int r)
{
//printf("%d %d %d\n",i,l,r);
int len=r-l+1;
//printf("len : %d\n",len);
tree[i].lsum=tree[i].rsum=tree[i].sum=len;
tree[i].l=l; tree[i].r=r;
tree[i].lazy=-1;
if(l==r)
{
return ;
}
int mid=(l+r)>>1;
build(i<<1,l,mid);
build(i<<1|1,mid+1,r);
push_up(i);
return ;
}
int query(int i,int cnt)
{
if(tree[i].l==tree[i].r)
{
return tree[i].l;
}
if(tree[i].lazy!=-1) push_down(i);
if(tree[i<<1].sum>=cnt)
{
query(i<<1,cnt);
}
else if(tree[i<<1].rsum+tree[i<<1|1].lsum>=cnt)
{
int len=tree[i<<1].rsum;
return tree[i<<1].r-len+1;
}
else
{
return query(i<<1|1,cnt);
}
}
void update(int i,int l,int r,int type)
{
if(tree[i].l==l&&tree[i].r==r)
{
if(type==1) tree[i].sum=tree[i].lsum=tree[i].rsum=0;
else
{
int len=tree[i].r-tree[i].l+1;
tree[i].sum=tree[i].lsum=tree[i].rsum=len;
}
tree[i].lazy=type;
return ;
}
if(tree[i].lazy!=-1) push_down(i);
int mid=(tree[i].l+tree[i].r)>>1;
if(r<=mid)
{
update(i<<1,l,r,type);
}
else if(l>mid)
{
update(i<<1|1,l,r,type);
}
else
{
update(i<<1,l,mid,type);
update(i<<1|1,mid+1,r,type);
}
push_up(i);
return ;
}
int main()
{
cin>>n>>m;
build(1,1,n);
int op;
int l,c,r;
while(m--)
{
scanf("%d",&op);
if(op==1)
{
scanf("%d",&c);
// printf("sum : %d \n",tree[1].sum);
if(tree[1].sum<c){
printf("0\n");
continue;
}
l=query(1,c);
printf("%d\n",l);
r=l+c-1;
update(1,l,r,1);
}
else
{
scanf("%d %d",&l,&c);
r=l+c-1;
update(1,l,r,0);
}
}
return 0;
}
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 18953 | Accepted: 8254 |
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
4000
single integer r, the first room in the contiguous sequence of rooms to be occupied. If the request cannot be satisfied, output 0.
Sample Input10 6
1 3
1 3
1 3
1 3
2 5 5
1 6
Sample Output1
4
7
0
5题意: 一家酒店有 n 个房间 现在有m个情况需要你去处理 如果是 cin op 如果op==1 表示有 x 个人要办理入住, 但是有一个要求就是 需要 房间号是连续的 如果存在连续的房间就 输出第一个房间的 编号 否则 输出0
如果op==2 表示 x,y 从x房间开始的 y 个人 要退房拜拜.
思路: 当然容易想到 是线段树 但是比较麻烦的就是退房后的处理 . 需要对结点的左右情况进行考虑. 然后更新线段树 .
其实比较难的地方也就是 push_up 和push_down 函数.
结构体中lsum 表示 这个结点 从他的左端点开始最长的连续的空房间的数量. rsum 表示 从右结点 开始向左连续的空房间的数量. sum 表示这个结点中的最大连续空房间的数量.
代码: #include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define N 50005
using namespace std;
struct node
{
int l,r;
int lazy;
int sum;
int lsum,rsum;
}tree[N<<2];
int n,m;
void push_up(int i)
{
tree[i].lsum=tree[i<<1].lsum; tree[i].rsum=tree[i<<1|1].rsum;
int len=tree[i<<1].r-tree[i<<1].l+1;
if(len==tree[i<<1].lsum) tree[i].lsum+=tree[i<<1|1].lsum;
len=tree[i<<1|1].r-tree[i<<1|1].l+1;
if(len==tree[i<<1|1].rsum) tree[i].rsum+=tree[i<<1].rsum;
tree[i].sum=max(max(tree[i<<1].sum,tree[i<<1|1].sum),tree[i<<1].rsum+tree[i<<1|1].lsum);
return ;
}
void push_down(int i)
{
int f=tree[i].lazy;
if(f==1) // 置 满
{
tree[i<<1].sum=tree[i<<1].lsum=tree[i<<1].rsum=0;
tree[i<<1].lazy=1;
tree[i<<1|1].sum=tree[i<<1|1].rsum=tree[i<<1|1].lsum=0;
tree[i<<1|1].lazy=1;
}
else // 清 0
{
int len=tree[i<<1].r-tree[i<<1].l+1;
tree[i<<1].sum=tree[i<<1].lsum=tree[i<<1].rsum=len;
tree[i<<1].lazy=0;
len=tree[i<<1|1].r-tree[i<<1|1].l+1;
tree[i<<1|1].sum=tree[i<<1|1].rsum=tree[i<<1|1].lsum=len;
tree[i<<1|1].lazy=0;
}
tree[i].lazy=-1;
//push_up(i);
return ;
}
void build(int i,int l,int r)
{
//printf("%d %d %d\n",i,l,r);
int len=r-l+1;
//printf("len : %d\n",len);
tree[i].lsum=tree[i].rsum=tree[i].sum=len;
tree[i].l=l; tree[i].r=r;
tree[i].lazy=-1;
if(l==r)
{
return ;
}
int mid=(l+r)>>1;
build(i<<1,l,mid);
build(i<<1|1,mid+1,r);
push_up(i);
return ;
}
int query(int i,int cnt)
{
if(tree[i].l==tree[i].r)
{
return tree[i].l;
}
if(tree[i].lazy!=-1) push_down(i);
if(tree[i<<1].sum>=cnt)
{
query(i<<1,cnt);
}
else if(tree[i<<1].rsum+tree[i<<1|1].lsum>=cnt)
{
int len=tree[i<<1].rsum;
return tree[i<<1].r-len+1;
}
else
{
return query(i<<1|1,cnt);
}
}
void update(int i,int l,int r,int type)
{
if(tree[i].l==l&&tree[i].r==r)
{
if(type==1) tree[i].sum=tree[i].lsum=tree[i].rsum=0;
else
{
int len=tree[i].r-tree[i].l+1;
tree[i].sum=tree[i].lsum=tree[i].rsum=len;
}
tree[i].lazy=type;
return ;
}
if(tree[i].lazy!=-1) push_down(i);
int mid=(tree[i].l+tree[i].r)>>1;
if(r<=mid)
{
update(i<<1,l,r,type);
}
else if(l>mid)
{
update(i<<1|1,l,r,type);
}
else
{
update(i<<1,l,mid,type);
update(i<<1|1,mid+1,r,type);
}
push_up(i);
return ;
}
int main()
{
cin>>n>>m;
build(1,1,n);
int op;
int l,c,r;
while(m--)
{
scanf("%d",&op);
if(op==1)
{
scanf("%d",&c);
// printf("sum : %d \n",tree[1].sum);
if(tree[1].sum<c){
printf("0\n");
continue;
}
l=query(1,c);
printf("%d\n",l);
r=l+c-1;
update(1,l,r,1);
}
else
{
scanf("%d %d",&l,&c);
r=l+c-1;
update(1,l,r,0);
}
}
return 0;
}
相关文章推荐
- poj 2892 Tunnel Warfare(线段树 单点更新 区间合并)
- POJ 3667 Hotel 线段树 区间更新 区间合并
- poj2892,线段树单点更新,区间合并
- poj 3667 Hotel(线段树区间更新合并)
- poj 2892---Tunnel Warfare(线段树单点更新、区间合并)
- Poj 3667 - Hotel 线段树-- 区间更新 区间合并 区间查询
- POJ 3928 线段树 单点更新+区间查询
- POJ 3468 A Simple Problem with Integers 线段树 区间更新
- poj 1823 Hotel(数据结构:线段树--区间更新)
- POJ - 3667 Hotel (线段树 + 区间合并)
- HDU 3308 LCIS(线段树区间合并 单点更新)
- 线段树区间更新,区间统计+离散化 POJ 2528 Mayor's posters
- HDU 1540 && POJ 2892 Tunnel Warfare (线段树,区间合并).
- POJ 2558 Mayor's posters(线段树+离散化+区间更新)
- HDU 1540 POJ 2892 线段树区间合并
- POJ 3468 A Simple Problem with Integers(线段树/区间更新)
- POJ 3468 区间更新的线段树、树状数组
- POJ 3667 hotel 和 NYOJ 537 hotel 【线段树之区间合并】
- POJ 3468 A Simple Problem with Integers(线段树 区间更新)
- POJ 2777 Count Color (线段树区间更新)