POJ3667——Hotel(线段树)

Hotel
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 8531 Accepted: 3626
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 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 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

USACO 2008 February Gold

解析:

         经典的线段树。。。

         s表示最长连续空房间的长度,z表示从左端点起的最长空房间的长度,y表示从右端点起的最长空房间的长度。。。

         lazy标记 1为满,0为空,-1为混合

         找起始位置,s[左儿子]>长度,就在左儿子找 else if(s[右儿子]>长度)  就在右儿子找  如果跨区间 即y[p<<1]+z[(p<<1)+1]>长度 直接返回起始位置

代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstdlib>
using namespace std;
const int N=200010;//4*n

int n,mm,z[N],s[N],y[N];    //z:从左端点起的最长空序列的长度  y:从右端点起的最长空序列  s:当前节点的最大一段空房的长度
int vis[N];  //lazy标记:0为空, 1为满, -1为混合

void doit(int p,int l,int r)
{
    z[p]=s[p]=y[p]=vis[p]?0:r-l+1;
}

void up(int p,int l,int r)
{
    int m=(l+r)>>1;
    if(m-l+1==s[p<<1])z[p]=s[p<<1]+z[(p<<1)+1];//左儿子全空
    else z[p]=z[p<<1];
    if(r-m==s[(p<<1)+1])y[p]=s[(p<<1)+1]+y[p<<1];//右儿子全空
    else y[p]=y[(p<<1)+1];
    s[p]=max(s[p<<1],s[(p<<1)+1]);
    s[p]=max(s[p],y[p<<1]+z[(p<<1)+1]);
}

void down(int p,int l,int r)
{
    if(r==l || vis[p]==-1)return ;
    vis[p<<1]=vis[(p<<1)+1]=vis[p];
    vis[p]=-1;
    int m=(l+r)>>1;
    doit(p<<1,l,m);
    doit((p<<1)+1,m+1,r);
}

void build(int p,int l,int r)
{
    z[p]=y[p]=s[p]=r-l+1;
    vis[p]=-1;
    if(l==r)return ;
    int m=(l+r)>>1;
    build(p<<1,l,m);
    build((p<<1)+1,m+1,r);
}

int get(int p,int l,int r,int x)  //查找客人从哪间开始入住
{
    down(p,l,r);
    if(r==l)return x=1;
    int m=(l+r)>>1;
    if(s[p<<1]>=x)return get(p<<1,l,m,x);
    if(y[p<<1]+z[(p<<1)+1]>=x)return m-y[p<<1]+1;
    if(s[(p<<1)+1]>=x) return get((p<<1)+1,m+1,r,x);
    return 0;
}

void update(int p,int ll,int rr,int l,int r,int x)//x=1:客人入住   x=0:清空客人
{
    if(l<=ll && r>=rr)
    {
        vis[p]=x;
        doit(p,ll,rr);
        return ;
    }
    down(p,ll,rr);
    int m=(ll+rr)>>1;
    if(r<=m)update(p<<1,ll,m,l,r,x);
    else if(m<l)update((p<<1)+1,m+1,rr,l,r,x);
    else update(p<<1,ll,m,l,m,x),update((p<<1)+1,m+1,rr,m+1,r,x);
    up(p,ll,rr);
}

void readdata()
{
    freopen("poj3667.in","r",stdin);
    freopen("poj3667.out","w",stdout);
    scanf("%d%d",&n,&mm);
}

void work()
{
    int x,l,len;
    build(1,1,n);
    for(int i=1;i<=mm;i++)
    {
        int op;
        scanf("%d",&op);
        if(op==1)
        {
            scanf("%d\n",&x);
            int xx=get(1,1,n,x);
            cout<<xx<<endl;
            if(xx)update(1,1,n,xx,xx+x-1,1);
        }
        if(op==2)
        {
            scanf("%d%d\n",&l,&len);
            update(1,1,n,l,l+len-1,0);
        }
    }
}

int main()
{
    readdata();
    work();
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值