POJ 3667 Hotel 区间合并+区间更新

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 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-n,这些人要求住连号且号头编号最小。
给出旅馆房间数n和操作数m(入住和清空操作),要你找出每次住进旅馆的头编号。

分析:
数据较大,用线段树的区间合并+区间更新解决问题;

区间更新需注意的问题:
1.在更新和查找之前进行pushdown;
2.在pushdown时检查是否能够向下传递,防止越界;
区间合并需注意问题:
1.合并之后的tag一定是未标记状态
2.区间合并时一定要检查两个要合并区间的端点是否满足连续的状态


#include<cstring>
#include<string>
#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<map>
#include<cstdlib>
#include<cmath>
#include<vector>

using namespace std;

#define INF 0x3f3f3f3f

struct node
{
    int l,r;
    int tag;
    int lt,rt,len;
} T[600005];

int m,n;
void init(int l,int r,int k)
{
    T[k].l=l;
    T[k].r=r;
    T[k].tag=-1;
    T[k].lt=r-l+1;
    T[k].rt=T[k].lt;
    T[k].len=T[k].rt;
    if(l==r) return;
    int mid=(l+r)>>1;
    init(l,mid,2*k);
    init(mid+1,r,2*k+1);
}

node Merge(node A,node B)
{
    node C;
    C.l=A.l;
    C.r=B.r;
    C.lt=A.lt;
    C.rt=B.rt;
    if(A.len==A.lt&&A.len==A.r-A.l+1) C.lt+=B.lt;
    if(B.len==B.rt&&B.len==B.r-B.l+1) C.rt+=A.rt;
    C.len=max(A.len,B.len);
    C.len=max(C.len,max(C.lt,C.rt));
    C.len=max(C.len,A.rt+B.lt);
    C.tag=-1;
    return C;
}

void pushdown(int k)
{
    if(T[k].tag==-1||T[k].l==T[k].r) return ;
    if(T[k].tag==0)
    {
        T[2*k].tag=T[2*k+1].tag=T[k].tag;
        T[2*k].lt=T[2*k].rt=T[2*k].len=T[2*k].r-T[2*k].l+1;
        T[2*k+1].lt=T[2*k+1].rt=T[2*k+1].len=T[2*k+1].r-T[2*k+1].l+1;
        T[k].tag=-1;
        return ;
    }
    if(T[k].tag==1)
    {
        T[2*k].tag=T[2*k+1].tag=T[k].tag;
        T[2*k].lt=T[2*k].rt=T[2*k].len=0;
        T[2*k+1].lt=T[2*k+1].rt=T[2*k+1].len=0;
        T[k].tag=-1;
        return ;
    }
}

void Insert(int d,int l,int r,int k)
{
    pushdown(k);
    if(T[k].l==l&&T[k].r==r)
    {
        T[k].tag=d;
        if(!d) T[k].lt=T[k].rt=T[k].len=r-l+1;
        else T[k].lt=T[k].rt=T[k].len=0;
        return ;
    }
    int mid=(T[k].l+T[k].r)>>1;
    if(mid>=r) Insert(d,l,r,2*k);
    else if(mid<l) Insert(d,l,r,2*k+1);
    else
    {
        Insert(d,l,mid,2*k);
        Insert(d,mid+1,r,2*k+1);
    }
    T[k]=Merge(T[2*k],T[2*k+1]);
}

int query(int d,int l,int r,int k)
{
    pushdown(k);
    if(l==r) return l;
    int mid=(T[k].l+T[k].r)>>1;
    if(T[2*k].len>=d)  return query(d,l,mid,2*k);
    else if(T[2*k].rt+T[2*k+1].lt>=d) return mid-T[2*k].rt+1;
    return query(d,mid+1,r,2*k+1);
}


int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        init(1,n,1);
        while(m--)
        {
            int a,b;
            scanf("%d",&a);
            if(a==1)
            {
                scanf("%d",&a);
                if(T[1].len<a)
                {
                    puts("0");
                    continue;
                }
                int flag=0;
                flag=query(a,1,n,1);
                printf("%d\n",flag);
                Insert(1,flag,flag+a-1,1);
            }
            else
            {
                scanf("%d%d",&a,&b);
                Insert(0,a,a+b-1,1);
            }
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值