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

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

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
ines 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

题意:N个宾馆 M次操作
1 d 代表有d个人要入住(注:必须是连续的房间)
注意 住的房间序列号越低越好 不能入住输出0
2 x d 代表 从位置x开始 有连续d个人退房 开始的时候房间全为空
简言之:求一块满足条件的最左边的空白空间
思路:
我们可以设空房间为1 满房间为0
tree[k].left 代表从左端点向右 连续都为1的区间长度
tree[k].right 代表从右端点向左 连续区间长度
而 tree[cur].all 代表整个区间内最长连续区间的长度

#include<stdio.h> 
#include<algorithm> 
using namespace std; 
const int N=50050; 
struct TREE
{
 int l,r,left,right,all,lazy;
 }tree[N<<2]; 
 
 void pushup(int k)//合并的过程 
 {
      tree[k].left=tree[k<<1].left;
      tree[k].right=tree[k<<1|1].right;
  if(tree[k<<1].all==tree[k<<1].r-tree[k<<1].l+1)
      tree[k].left+=tree[k<<1|1].left;
    if(tree[k<<1|1].all==tree[k<<1|1].r-tree[k<<1|1].l+1)
     tree[k].right+=tree[k<<1].right; 
 tree[k].all=max(tree[k<<1|1].left+tree[k<<1].right,max(tree[k<<1].all,tree[k<<1|1].all));
  
 }
 
void build(int k,int l,int r)
{
 tree[k].l=l;
 tree[k].r=r;
 tree[k].lazy=-1;
 tree[k].all=tree[k].left=tree[k].right=r-l+1; 
 if(l==r)
 {
  return;
 } 
 int mid=(l+r)>>1; 
 build(k<<1,l,mid);
 build(k<<1|1,mid+1,r);
 } 
 
 void down(int k)
 {
  if(tree[k].lazy!=-1)
  {
   int val=(tree[k<<1].r-tree[k<<1].l+1)*tree[k].lazy;
   tree[k<<1].left=tree[k<<1].right=tree[k<<1].all=val;
   tree[k<<1].lazy=tree[k].lazy;
   
    val=(tree[k<<1|1].r-tree[k<<1|1].l+1)*tree[k].lazy;
   tree[k<<1|1].left=tree[k<<1|1].right=tree[k<<1|1].all=val;
   tree[k<<1|1].lazy=tree[k].lazy;
   
   tree[k].lazy=-1;
  }
 }
 
 void change(int k,int l,int r,int w)
 {
      if(tree[k].l>=l&&tree[k].r<=r)
      {
       tree[k].lazy=w;
       tree[k].left=tree[k].right=tree[k].all=(tree[k].r-tree[k].l+1)*w;
       return;
   }
   down(k);
   int mid=(tree[k].l+tree[k].r)>>1;
   if(l<=mid) change(k<<1,l,r,w);
   if(r>mid ) change(k<<1|1,l,r,w);
   pushup(k);
 }

int query(int k,int len))//查询最早的长度为len的连续的1序列出现的位置 
{

 if(tree[k].l==tree[k].r)
return tree[k].l;
  //下面要查询的是子区间
 down(k);
  //先看当前区间的左孩子区间能否放下 
 //再看左右孩子共有的区间能否放下
 //不能则再看右孩子
 
 if(tree[k<<1].all>=len) return query(k<<1,len);
 else if(tree[k<<1].right+tree[k<<1|1].left>=len) 
 return  tree[k<<1].r-tree[k<<1].right+1;
 else
  return query(k<<1|1,len);
}

int main()
{
 int n,m;
 while(scanf("%d%d",&n,&m)!=EOF) 
 {
  build(1,1,n);
  int a,x,d;
  while(m--)
  {
   scanf("%d",&a);
   if(a==1)
   {
    scanf("%d",&d); 
    if(tree[1].all<d) printf("0\n");
    else
    {
     int ret=query(1,d);
     printf("%d\n",ret);
     change(1,ret,ret+d-1,0);    
    } 
   }
   else
   {
    scanf("%d%d",&x,&d);//从位置x起有d个人要走
    change(1,x,x+d-1,1); 
   } 
  }
 } 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值