POJ 3667 - Hotel (线段树 区间更新)

Hotel
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 18479 Accepted: 8040

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 D(b) Three space-separated integers representing a check-out: 2, Xi, and D

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



题意:

n个房间,1操作 ,有x个人来住房间,问有没有相邻空的的x个房间,有则返回最左的房间。多个答案一定要最左边的。2操作,清空[x,x+len-1]这些房间。

POINT:

线段树每个区间记录,F:这个区间内最大连续房间数。 R:这个区间内从右开始数有几个空房间(遇到有人的房间即停止)L:一样。

这样每次询问就是找x个响铃空房间,左子树的F满足条件就进入左子树。之后查询跨越左右的房间(利用左子树的R和右子树的L),之后查询右子树的F。


#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <map>
using namespace std;
#define LL long long
#define lt x<<1
#define rt x<<1|1
const int maxn = 50100*4;
const int inf = 0x3f3f3f3f;
int f[maxn],L[maxn],R[maxn];
int G[maxn];
int n;
void build(int x,int l,int r)
{
    int mid=(l+r)>>1;
    f[x]=L[x]=R[x]=r-l+1;
    G[x]=-1;
    if(l==r) return;
    build(lt,l,mid);
    build(rt,mid+1,r);
}
void up(int x,int l,int r)
{
    int mid=(l+r)>>1;
    f[x]=max(f[lt],f[rt]);
    f[x]=max(f[x],L[rt]+R[lt]);
    if(L[lt]==mid-l+1)
        L[x]=L[lt]+L[rt];
    else
        L[x]=L[lt];
    if(R[rt]==r-mid)
        R[x]=R[rt]+R[lt];
    else
        R[x]=R[rt];
}
void pushdown(int x,int l,int r)
{
    if(l==r) return;
    int mid=(l+r)>>1;
    G[lt]=G[rt]=G[x];
    if(G[x]){
        f[lt]=L[lt]=R[lt]=0;
        f[rt]=L[rt]=R[rt]=0;
    }else{
        f[lt]=L[lt]=R[lt]=mid-l+1;
        f[rt]=L[rt]=R[rt]=r-mid;
    }
    G[x]=-1;
}
int query(int x,int l,int r,int len)
{
    if(f[x]<len) return 0;
    if(G[x]!=-1) pushdown(x, l, r);
    if(l==r) return l;
    int mid=(l+r)>>1;
    if(f[lt]>=len) return query(lt, l, mid, len);
    if(R[lt]+L[rt]>=len) return mid-R[lt]+1;
    return query(rt,mid+1,r,len);
    
}
void change(int x,int l,int r,int ll,int rr,int v)
{
    if(G[x]!=-1) pushdown(x,l,r);
    if(ll<=l&&rr>=r){
        G[x]=v;
        if(v==0){
            f[x]=L[x]=R[x]=r-l+1;
        }else{
            f[x]=L[x]=R[x]=0;
        }
    }
    else{
        int mid=(l+r)>>1;
        if(ll<=mid){
            change(lt, l, mid, ll, rr, v);
        }
        if(mid+1<=rr){
            change(rt,mid+1,r,ll,rr,v);
        }
        up(x,l,r);
    }
}
int main()
{
    int m;
    while(~scanf("%d %d",&n,&m)){
        build(1,1,n);
        for(int i=1;i<=m;i++){
            int k;scanf("%d",&k);
            if(k==1){
                int len;scanf("%d",&len);
                int ans=query(1,1,n,len);
                printf("%d\n",ans);
                if(ans!=0){
                    change(1,1,n,ans,ans+len-1,1);
                }
            }else{
                int l,len;
                scanf("%d %d",&l,&len);
                change(1,1,n,l,l+len-1,0);
            }
        }
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值