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

哎最近在学线段树,让我有种前几天学的数论好像不是太难的感觉。。。。。。太难了 。

题意:给N个房间,M个操作,操作分两种,一种是找有没有相连的K个房间,如果有输出左端点最小的区间的那个的左端点;

还有一个操作就是清除房间。

思路:这个是线段树区间合并的板题。找这个连续空间无非只有三种,左边右边和中间,打代码的时候能搞清楚就好了 。但是我刚学线段树,做板题都感觉很难,做不出来。我个人认为区间合并不难想,只是小细节太多,不大好写。我个人觉得情况有点多,反正目前的我是想不出来的 ,思路能想出来,但是写不出来。艾。

 

code:

#include <iostream>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=50010;
struct node{
    int l,r;
    int ls,rs,maxs,col;}node[maxn*4];   // l,r是区间端点  ls,rs左右合并区间,maxs区间最大值,col判断是否有房住
//col代表这个区间的居住情况,1是住满了,0是全部没有人住,-1是一部分住一部分没住。
int N,M,t1,t2,t3,flag; // flag用来传导居住情况;
void buildtree(int i,int l,int r)    // 建树
{
    node[i].l=l;
    node[i].r=r;
    node[i].rs=node[i].ls=node[i].maxs=r-l+1;
    node[i].col=0;
    if(l==r)
    {
        return ;
    }
    int m=(l+r)/2;
    buildtree(i<<1,l,m);
    buildtree((i<<1)+1,m+1,r);
}
void pushup(int i)
{
    if(node[2*i].col==0)
    {
        node[i].ls=node[i*2].maxs+node[i*2+1].ls;
    }
    else node[i].ls=node[i*2].ls;
    if(node[2*i+1].col==0)
    {
        node[i].rs=node[2*i+1].maxs+node[i*2].rs;
    }
    else node[i].rs=node[2*i+1].rs;
    node[i].maxs=max(node[2*i].maxs,node[2*i+1].maxs);
    node[i].maxs=max(node[2*i].rs+node[2*i+1].ls,node[i].maxs);
    if(node[i].maxs==node[i].r-node[i].l+1)
    {
        node[i].col=0;
    }
    else if(node[i].maxs==0)
    {
        node[i].col=1;
    }
    else node[i].col=-1;
}
int query(int i,int num) 
{
    if(node[i].ls==num && node[i].r-node[i].l==num)
    {
        return node[i].l;
    }      //如果说遇到区间符合的就返回。正好整个区间满足。
    if(node[i].maxs>=num)    //如果说存在一个比num大的连续区间
    {
        if(node[i].col!=-1)   // 对node[i].col为0和1的两种情况分类讨论
        {
            node[i*2].col=node[i*2+1].col=node[i].col;
            if(node[i].col==1)    // 如果是0的话,那么两个子节点都住满
            {
                node[i*2].ls=node[i*2].rs=node[i*2].maxs=0;
                node[i*2+1].ls=node[i*2+1].rs=node[i*2+1].maxs=0;
            }
            else if(node[i].col==0) // 如果是1的话,那么两个子节点都没人
            {
                node[i*2].ls=node[i*2].rs=node[i*2].maxs=node[i*2].r-node[i*2].l+1;
                node[2*i+1].ls=node[i*2+1].rs=node[i*2+1].maxs=node[i*2+1].r-node[i*2+1].l+1;
            }
            node[i].col=-1;   // 把此节点更新
        }
        if(node[i*2].maxs>=num)   // 分三种 ,左边的,中间的,右边的。
            return query(i*2,num);
        if(node[i*2].rs+node[i*2+1].ls>=num)
            return node[i*2].r-node[i*2].rs+1;  // 中间的直接返回做儿子的又区间的起点就行
        if(node[2*i+1].maxs>=num)
            return query(i*2+1,num);
    }
    return 0;
}
void update(int i,int l,int r)
{
    if(node[i].l==l && node[i].r==r)   //找到这个区间
    {
        if(flag==0)     //flag看是什么操作需要更新
        {
            node[i].ls=node[i].rs=node[i].maxs=node[i].r-node[i].l+1;
        }
        if(flag==1)
        {
            node[i].ls=node[i].rs=node[i].maxs=0;
        }
        node[i].col=flag;  
        return ;
    }
    int mid=(node[i].l+node[i].r)/2;
    if(node[i].col!=-1)  // 还是对0和1两种情况分类讨论
    {
        node[i*2].col=node[i*2+1].col=node[i].col;
        if(node[i].col==0)
        {
            node[i*2].ls=node[i*2].rs=node[i*2].maxs=node[i*2].r-node[i*2].l+1;
            node[i*2+1].ls=node[i*2+1].rs=node[i*2+1].maxs=node[i*2+1].r-node[i*2+1].l+1;
        }
        if(node[i].col==1)
        {
            node[i*2].ls=node[i*2].rs=node[i*2].maxs=0;
            node[i*2+1].ls=node[i*2+1].rs=node[i*2+1].maxs=0;
        }
        node[i].col=-1;
    }
    if(l>=mid+1)      // 分三种情况
        update(i*2+1,l,r);
    else if(r<=mid)
        update(i*2,l,r);
    else {
        update(2*i,l,mid);
        update(2*i+1,mid+1,r);
    }
    pushup(i);    //更新这个节点
}
int main()
{
    scanf("%d%d",&N,&M);
    buildtree(1,1,N);   // 减数,ls,rs,maxs一开始都初始化为r-l+1;因为都为空
    while(M--)
    {
        scanf("%d",&t1);
        if(t1==1)
        {
            scanf("%d",&t2);
            int ins=query(1,t2);   //从树的根节点开始找,有没有一个符合的;
            printf("%d\n",ins);    
            if(ins)     //如果有符合的,就把这个区间的col设置为1,意思是全部住满了。
            {
                flag=1;
                update(1,ins,ins+t2-1);  
            }
        }
        else {           // 另一种操作只需要把这一段区间给设置成0,就行了。
            flag=0;
            scanf("%d%d",&t2,&t3);
            update(1,t2,t2+t3-1);
        }
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值