poj-3667 Hotel (线段树区间合并)

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 rto 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
  【题意】 N间空房子,共有M次操作,接下来M行,每行输入一个整数a,a=1时再输入一个数b,查询N间房子里面是否有b间连续的空房子,如果有返回最左边的空房间编号,如果a=2,接着输入两个数b,c,表示将[b,c]区间所有房间清空。典型的区间合并题,代码相对较长,还是有点小吃力的。


【AC】代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<map>
using namespace std;
const int N=50000;
int m,n;

struct tree
{
    int l,r,lsum,rsum,ssum;
    int ck;
}node[N<<2];

int max(int x,int y)
{
    return x>y?x:y;
}

void pushup(int ans)
{
    int ll = node[ans<<1].r - node[ans<<1].l+1;
    int rr = node[ans<<1|1].r - node[ans<<1|1].l+1;
    node[ans].lsum = node[ans<<1].lsum;
    if(ll == node[ans<<1].lsum) node[ans].lsum+=node[ans<<1|1].lsum;
    node[ans].rsum = node[ans<<1|1].rsum ;
    if(rr == node[ans].rsum) node[ans].rsum+=node[ans<<1].rsum;

    node[ans].ssum = max(max(node[ans<<1].ssum, node[ans<<1|1].ssum),node[ans<<1].rsum+node[ans<<1|1].lsum);
}

void pushdown(int ans)
{
    if(node[ans].ck!=-1)
    {
        int ll = node[ans<<1].r- node[ans<<1].l+1;
        int rr = node[ans<<1|1].r - node[ans<<1|1].l+1;
        node[ans<<1].ck = node[ans<<1|1].ck = node[ans].ck;
        node[ans].ck = -1;
        node[ans<<1].rsum = node[ans<<1].lsum =node[ans<<1].ssum = node[ans<<1].ck?0:ll;
        node[ans<<1|1].rsum = node[ans<<1|1].lsum = node[ans<<1|1].ssum = node[ans<<1|1].ck?0:rr;
    }
}

void build_tree(int ans,int ll,int rr)
{
    node[ans].l =ll, node[ans].r =rr, node[ans].ck =-1;
    node[ans].lsum = node [ans].rsum =node[ans].ssum = rr - ll +1 ;
    if(ll == rr )
        return ;
    int mid = (ll + rr )>>1 ;
    build_tree(ans<<1,ll,mid);
    build_tree(ans<<1|1,mid+1,rr);
    pushup(ans);
}

void update(int ans,int ll,int rr,int value)
{
    if(node[ans].l == ll&& node[ans].r == rr)
    {
        node[ans].ssum = node[ans].lsum = node[ans].rsum = value?0:(rr-ll+1);
        node[ans].ck = value;
        return ;
    }
    pushdown(ans);
    int m = (node[ans].l + node[ans].r)>>1;
    if(rr<=m)
    {
        update(ans<<1,ll,rr,value);
    }
    else if(ll>m)
    {
        update(ans<<1|1,ll,rr,value);
    }
    else
    {
        update(ans<<1,ll,m,value);
        update(ans<<1|1,m+1,rr,value);
    }
    pushup(ans);
}

int query(int ans,int vis)
{
    if(node[ans].l == node[ans].r) return node[ans].l;
    pushdown(ans);
    int mid= (node[ans].l+ node[ans].r)>>1;
    if(node[ans<<1].ssum>=vis) return query(ans<<1,vis);
    else if(node[ans<<1].rsum+node[ans<<1|1].lsum>=vis) return mid-node[ans<<1].rsum+1;
    return query(ans<<1|1,vis);
}

int main()
{
    scanf("%d%d",&m,&n);
    build_tree(1,1,m);
    while(n--)
    {
        int x;
        scanf("%d",&x);
        if(x==1)
        {
            int w;
            scanf("%d",&w);
            if(node[1].ssum<w) puts("0");//如果根节点的最大连续空余房间数小于w,则直接输出0
            else
            {
                int p=query(1,w);
                printf("%d\n",p);
                update(1,p,p+w-1,1);//区间更新  
            }
        }
        else
        {
            int a,b;
            scanf("%d %d",&a,&b);
            update(1,a,a+b-1,0); //区间更新
        }
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值