Hotel

E - Hotel
Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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

区间合并,现在感觉是线段树中最难的了,看到题的时候完全没什么头绪,然后看了别人写的代码一步步理解,最后仿照着写还是写错了,半天没找到错误的地方。最后同学帮我看才发现错误了。


#include <iostream>
#include<cstdio>
#include<algorithm>
#define lson id<<1
#define rson id<<1|1
using namespace std;
int max(int a,int b)
{
return a>b?a:b;
}
struct node
{
    int le,ri,n;    //n为标记,标记房间是否为空
    int ls,rs,ms;   //ls表示该节点左端开始连续的空房间,rs表示右端开始连续的空房间,ms表示该节点下最大的连续空房间
}t[50050<<2];
void build(int id,int l,int r)
{
    t[id].le=l;
    t[id].ri=r;
    t[id].n=-1;
    t[id].ls=t[id].rs=t[id].ms=r-l+1;
    if(l!=r){
        int mid=(l+r)>>1;
        build(lson,l,mid);
        build(rson,mid+1,r);
    }
}
void pushup(int id)
{
    t[id].ls=t[lson].ls;        //父节点的左端最大空房间数默认等于左儿子的左端最大
    t[id].rs=t[rson].rs;         //右端最大等于右儿子最大
    int m=t[id].ri-t[id].le+1;
    if(t[lson].ls==m-(m>>1))        
        t[id].ls+=t[rson].ls;                //当左儿子的左端最大等于左儿子的区间长度时,即左儿子全为空,父节点的左端最大应加上右儿子的左端最大
    if(t[rson].rs==m>>1)
        t[id].rs+=t[lson].rs;                 //当右儿子全为空时,父节点的右端最大应加上左儿子的右端最大
    t[id].ms=max(max(t[lson].ms,t[rson].ms),t[lson].rs+t[rson].ls);
}
void pushdown(int id)
{
    if(t[id].n!=-1)
    {
        int m=t[id].ri-t[id].le+1;
        t[lson].n=t[rson].n=t[id].n;
        t[lson].ls=t[lson].rs=t[lson].ms=t[id].n?0:(m-m/2);           //如果父节点的标记为1,则两个儿子的最大都更新为0(表示无空房间);若标记为0,则更新为区间长度(表示都是空房间)
        t[rson].ls=t[rson].rs=t[rson].ms=t[id].n?0:(m/2);
        t[id].n=-1;              //pushdown后需要清楚父节点的标记
    }
}
int query(int id,int y)
{
    if(t[id].le==t[id].ri) return t[id].le;            //如果查询到叶子节点则返回区间的左边。
    pushdown(id);
    int m=(t[id].ri+t[id].le)>>1;
    if(t[lson].ms>=y) return query(lson,y);    //如果左儿子的最大连续大于y,则继续查找左儿子
    else if(t[lson].rs+t[rson].ls>=y) return m-t[lson].rs+1;    //如果左儿子的右端加上右儿子的左端大于y,则直接返回最左边的坐标,不用继续查询
    else return query(rson,y);                      //上面都不符合时查找右儿子
}
void update(int id,int l,int r,int c)
{
    if(l<=t[id].le&&t[id].ri<=r)           //该节点的区间在查询区间之内则只需要标记就行,不用往下进行,需要时再往下移动标记
    {
        t[id].ls=t[id].rs=t[id].ms=c?0:t[id].ri-t[id].le+1;
        t[id].n=c;
        return ;
    }
    pushdown(id);
    int m=(t[id].le+t[id].ri)>>1;
    if(l<=m) update(lson,l,r,c);
    if(r>m) update(rson,l,r,c);
    pushup(id);                       //往下计算完后需要往上维护父节点
}
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        build(1,1,n);
        for(int i=0;i<m;i++)
        {
            int x,y,z;
            scanf("%d",&x);
            if(x==1)
            {
                scanf("%d",&y);
                if(t[1].ms<y)
                {
                    printf("0\n");
                    continue;
                }
                int p=query(1,y);
                printf("%d\n",p);
                update(1,p,p+y-1,1);
            }
            else {
                scanf("%d%d",&y,&z);
                update(1,y,z+y-1,0);
            }
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值