POJ 3667 Hotel 线段树区间合并

题意:

模拟订房系统。给出N,M。代表房间数和操作次数。每组操作中 

1 表示入住 k 间连续房间,要求安排的房间连续且在最左边,如果能入住输出最左边的房号,不能则 -1.

2 表示退从第K 间房起连续的P间房。

思路:

先查找最左端的房子,当左区间的房间的最大连续小于要求时(即查找到最左端)返回,返回后通过区间合并判断是否满足题意。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define ls rt<<1
#define rs (rt<<1)|1
#define root rt,l,r
#define lson rt<<1,l,mid
#define rson (rt<<1)|1,mid+1,r
#define mi (l+r)>>1

const int MAXN=50000+100;
int st,en,v;
int tree[MAXN*4];
int sum[MAXN*4];
int rsum[MAXN*4];
int lsum[MAXN*4];
void push_down(int rt,int l,int r){
    if(tree[rt]){
        tree[rt]=0;
        if(sum[rt]){
            int len=r-l+1;
            int rlen=len/2;
            int llen=len-rlen;
            tree[ls]=1;
            lsum[ls]=rsum[ls]=sum[ls]=llen;
            tree[rs]=1;
            lsum[rs]=rsum[rs]=sum[rs]=rlen;
        }else{
            tree[ls]=1;
            lsum[ls]=rsum[ls]=sum[ls]=0;
            tree[rs]=1;
            lsum[rs]=rsum[rs]=sum[rs]=0;
        }
    }
    return ;
}
void push_up(int rt,int l,int r){
    lsum[rt]=lsum[ls];
    rsum[rt]=rsum[rs];
    int len=r-l+1;int rlen=len/2;int llen=len-rlen;
    if(lsum[rt]==llen) lsum[rt]+=lsum[rs];
    if(rsum[rt]==rlen) rsum[rt]+=rsum[ls];
    sum[rt]=max(max(lsum[rt],rsum[rt]),rsum[ls]+lsum[rs]);
    return ;
}
void update(int rt,int l,int r){
    if(st<=l&&r<=en){
        if(v){
            tree[rt]=1;
            lsum[rt]=rsum[rt]=sum[rt]=r-l+1;
        }else{
            tree[rt]=1;
            lsum[rt]=rsum[rt]=sum[rt]=0;
        }
        return ;
    }
    int mid=mi;
    push_down(root);
    if(st<=mid)
        update(lson);
    if(mid<en)
        update(rson);
    push_up(root);
    return ;
}
int query(int rt,int l,int r){
    if(sum[rt]<v) return 0;
    if(l==r)    return l;
    push_down(root);
    int mid=mi;
    int ans=0;
    if(rsum[ls]+lsum[rs]>=v){
        ans=mid-rsum[ls]+1;
    }else{
        ans=query(lson);
        if(!ans)
            ans=query(rson);
    }
    return ans;
}

int main()
{
    int n,m,ch;
    while(cin>>n>>m){
        memset(tree,0,sizeof(tree));
        memset(sum,0,sizeof(sum));
        memset(lsum,0,sizeof(lsum));
        memset(rsum,0,sizeof(rsum));
        st=1;en=n;v=1;
        update(1,1,n);
        for(int i=0;i<m;i++){
            scanf("%d%d",&ch,&v);
            if(ch==1){
                st=query(1,1,n);
                if(st){
                    en=st+v-1;
                    v=0;
                    update(1,1,n);
                }
                cout<<st<<endl;
            }else{
                scanf("%d",&en);
                st=v;
                en=en+st-1;
                v=1;
                update(1,1,n);
            }
        }
    }
}




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 Dicontiguous 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 D(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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值