POJ 3667 Hotel(线段树+区间合并)

Hotel
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 16546 Accepted: 7178

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


题意很简单,维护区间内的最长全为0的长度,这里注意的是两个区间合并,特殊情况就是区间中间连接的时候要多想一点,然后就是对这棵二叉树的理解,从头到根的遍历,在查找的时候,要注意最优先考虑边界条件会比较方便,最后注意一点细节吧,ans=0的情况就不要更新了错了好几次.

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<vector>
using namespace std;
#define maxn 500000
struct  node
{
    int l;
    int r;
    int lsum;
    int rsum;
    int maxsum;
    int flag;
}tree[maxn*8];
int n,m,ans;
void pushup(int id)
{
    int tmp=tree[id*2].rsum+tree[id*2+1].lsum;
    if(tmp>tree[id*2].maxsum&&tmp>=tree[id*2+1].maxsum)
        tree[id].maxsum=tmp;
    else if(tree[id*2].maxsum>=tree[id*2+1].maxsum)
        tree[id].maxsum=tree[id*2].maxsum;
    else
        tree[id].maxsum=tree[id*2+1].maxsum;

    int ll=tree[id*2].r-tree[id*2].l+1;
    int rr=tree[id*2+1].r-tree[id*2+1].l+1;

    if(tree[id*2].lsum==ll)
        tree[id].lsum=tree[id*2].lsum+tree[id*2+1].lsum;
    else
        tree[id].lsum=tree[id*2].lsum;
    if(tree[id*2+1].rsum==rr)
        tree[id].rsum=tree[id*2+1].rsum+tree[id*2].rsum;
    else
        tree[id].rsum=tree[id*2+1].rsum;
}
void pushdown(int id)
{
    if(tree[id].flag==-1)
        return ;
    if(tree[id].flag==1)
    {
        tree[id*2].flag=1;
        tree[id*2].lsum=0;
        tree[id*2].rsum=0;
        tree[id*2].maxsum=0;

        tree[id*2+1].flag=1;
        tree[id*2+1].lsum=0;
        tree[id*2+1].rsum=0;
        tree[id*2+1].maxsum=0;
    }
    else
    {
        int ll=tree[id*2].r-tree[id*2].l+1;
        int rr=tree[id*2+1].r-tree[id*2+1].l+1;

        tree[id*2].flag=0;
        tree[id*2].lsum=ll;
        tree[id*2].rsum=ll;
        tree[id*2].maxsum=ll;

        tree[id*2+1].flag=0;
        tree[id*2+1].lsum=rr;
        tree[id*2+1].rsum=rr;
        tree[id*2+1].maxsum=rr;
    }
    tree[id].flag=-1;
}
void build(int id,int tl,int tr)
{
    tree[id].l=tl;
    tree[id].r=tr;
    tree[id].flag=-1;
    int mid=(tl+tr)/2;
    if(tl==tr)
    {
        tree[id].maxsum=1;
        tree[id].lsum=1;
        tree[id].rsum=1;
        return ;
    }
    build(id*2,tl,mid);
    build(id*2+1,mid+1,tr);
    pushup(id);
}
void add(int id,int x,int y,int z)
{
    int tl=tree[id].l;
    int tr=tree[id].r;
    if(tl>y||tr<x)
        return ;
    if(tl>=x&&tr<=y)
    {
        pushdown(id);
        if(z==0)
        {
            tree[id].maxsum=tr-tl+1;
            tree[id].lsum=tr-tl+1;
            tree[id].rsum=tr-tl+1;
            tree[id].flag=0;
        }
        else
        {
            tree[id].maxsum=0;
            tree[id].lsum=0;
            tree[id].rsum=0;
            tree[id].flag=1;
        }
        return ;
    }
    pushdown(id);
    add(id*2,x,y,z);
    add(id*2+1,x,y,z);
    pushup(id);
}
int query(int id,int cnt)
{
    int tl=tree[id].l;
    int tr=tree[id].r;
    pushdown(id);
    if(tl==tr)
        return tl;
    if(tree[1].maxsum<cnt)
        return 0;
    if(tree[id*2].maxsum>=cnt)
        return query(id*2,cnt);
    else if(tree[id*2].rsum+tree[id*2+1].lsum>=cnt)
         return (tree[id*2].r-tree[id*2].rsum+1);
    else
        return query(id*2+1,cnt);
}
int  main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        build(1,1,n);
        while(m--)
        {
            int t,x,y;
            scanf("%d",&t);
            if(t==1)
            {
                scanf("%d",&x);
                ans=query(1,x);
                printf("%d\n",ans);
                if(ans!=0)
                    add(1,ans,ans+x-1,1);
            }
            else
            {
                scanf("%d%d",&x,&y);
                add(1,x,y+x-1,0);
            }
        }
    }
return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值