POJ 3667 Hotel 【线段树 区间合并】

Hotel
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 14506 Accepted: 6270

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 ≤ XiN-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


恩,题目大意就是说,房间,只在同一侧,然后是 1 a 表示查询是否存在 a 个连续的空房间(左边优先),若存在,输出最左边的房间号,然后 2 a b 表示从 a 到 b 的房间住的人离开了,也就是这些房间空了可用。因为线段树数组大小扰了我好几天(粗心)。。。。。。


#include <iostream>
#include<cstdio>
#include<cstring>
#define maxn 50000
using namespace std;
int n,m,c,x,d;
struct lnode
{
    int l,r,len;//左右端点及长度
    int ls,rs,ms;//ls从左边开始数最长的连续房间个数 rs从右边开始数 ms区间内最长的 
    int mark;//用于延迟标记
}node[maxn<<2];
int maxi(int a,int b)
{
    return a>b?a:b;
}
void pushup(int o)
{
    node[o].ls=node[o<<1].ls;//从左边开始数首先等于左子树从左边数的长度
    if(node[o<<1].ls==node[o<<1].len)//若其长度等于左子树总长则可以加上右子树从左边开始数的
        node[o].ls+=node[o<<1|1].ls;
    node[o].rs=node[o<<1|1].rs;//同上
    if(node[o<<1|1].rs==node[o<<1|1].len)
        node[o].rs+=node[o<<1].rs;
    node[o].ms=maxi(maxi(node[o<<1].ms,node[o<<1|1].ms),(node[o<<1].rs+node[o<<1|1].ls));
}
void pushdown(int o)
{
    if(node[o].mark!=-1)
    {
        node[o<<1].mark=node[o<<1|1].mark=node[o].mark;
        node[o].mark=-1;
        node[o<<1].rs=node[o<<1].ls=node[o<<1].ms=node[o<<1].mark?0:node[o<<1].len;//若为1则表示此时房间非空,为0表示为空,空房间长度即为区间长度
        node[o<<1|1].ls=node[o<<1|1].rs=node[o<<1|1].ms=node[o<<1|1].mark?0:node[o<<1|1].len;//同上
    }
}
void build(int o,int l,int r)
{
    node[o].l=l;
    node[o].r=r;
    node[o].len=r-l+1;
    node[o].mark=-1;
    node[o].ls=node[o].rs=node[o].ms=r-l+1;
    if(l==r)
        return ;
    int mid=(l+r)>>1;
    build(o<<1,l,mid);
    build(o<<1|1,mid+1,r);
    pushup(o);
}
void update(int o,int l,int r,int c)
{
    if(node[o].l==l&&node[o].r==r)
    {
        node[o].ms=node[o].ls=node[o].rs=c?0:(r-l+1);//同上
        node[o].mark=c;
        return ;
    }
    pushdown(o);
    int mid=(node[o].l+node[o].r)>>1;
    if(r<=mid)
        update(o<<1,l,r,c);
    else if(l>mid)
        update(o<<1|1,l,r,c);
    else
    {
        update(o<<1,l,mid,c);
        update(o<<1|1,mid+1,r,c);
    }
    pushup(o);
}
int query(int o,int w)
{
    if(node[o].l==node[o].r)
        return node[o].l;
    pushdown(o);
    int mid=(node[o].l+node[o].r)>>1;
    if(node[o<<1].ms>=w)//在左子树
        return query(o<<1,w);
    else if(node[o<<1].rs+node[o<<1|1].ls>=w)//因为左边优先,所以要先检测中间的在检测右子树
        return mid-node[o<<1].rs+1;
    return query(o<<1|1,w);
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        build(1,1,n);
        while(m--)
        {
            scanf("%d",&c);
            if(c==1)
            {
                scanf("%d",&x);
                if(node[1].ms<x)
                    printf("0\n");
                else
                {
                    int p=query(1,x);
                    printf("%d\n",p);
                    update(1,p,p+x-1,1);
                }
            }
            else
            {
                scanf("%d%d",&x,&d);
                update(1,x,x+d-1,0);
            }
        }
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值