您的位置:首页 > 其它

poj 3667 Hotel(线段树 区间合并 区间更新 )

2018-04-02 22:13 501 查看
Hotel
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 18953 Accepted: 8254
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: