Hotel poj 3667

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hotel poj 3667相关的知识,希望对你有一定的参考价值。

Language:
 
                                               
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 18020   Accepted: 7823

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

Source

[Submit]   [Go Back]   [Status]   [Discuss]

题意 :

  两种操作  

    1)  选一段 长度为 D 的可用区间 ,并占用它

    2)  从 X 开始 长度为 D 的区间 标记为可用

  开始时均为可用 

  区间范围 1--n

   询问次数 m

  1 ≤  n  ,m < 50,000

思路:

  考虑区间合并线段树,带lazy标记

  

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <functional>

#define lson l,mid,pos<<1
#define rson mid+1,r,pos<<1|1

using namespace std;

const int maxn = 50000+10;
int lazy[maxn<<2];
int pre[maxn<<2];
int suf[maxn<<2];
int now[maxn<<2];
int a[maxn];
void push_down(int l,int r,int pos)
{
    if (l==r){
        a[l]=lazy[pos]-1;
        pre[pos]=suf[pos]=now[pos]=(a[l]==0)*1;
        lazy[pos]=0;
        return ;
    }
    int mid=(l+r)>>1;
    int lpos=pos<<1,rpos=pos<<1|1;
    a[l]=a[r]=a[mid]=a[mid+1]=lazy[pos]-1;
    pre[pos]=suf[pos]=now[pos]=(a[l]==0)*(r-l+1);
    lazy[lpos]=lazy[rpos]=lazy[pos];
    lazy[pos]=0;
}
void push_up(int l,int r,int pos)
{
    int mid=(l+r)>>1;
    int lpos=pos<<1,rpos=pos<<1|1;
    if (lazy[lpos])push_down(lson);
    if (lazy[rpos])push_down(rson);
    if (pre[lpos]+l==mid+1)pre[pos]=pre[lpos]+pre[rpos];
    else pre[pos]=pre[lpos];
    if (r-suf[rpos]==mid)suf[pos]=suf[lpos]+suf[rpos];
    else suf[pos]=suf[rpos];
    now[pos]=max(now[lpos],now[rpos]);
    if (a[mid]==0&&a[mid+1]==0)now[pos]=max(now[pos],suf[lpos]+pre[rpos]);
}
int getit(int l,int r,int pos)
{
    if (lazy[pos])push_down(l,r,pos);
    return now[pos];
}
void build(int l,int r,int pos)
{
    lazy[pos]=0;
    if (l==r){
        a[l]=0;
        suf[pos]=pre[pos]=now[pos]=1;
        return ;
    }
    int mid=(l+r)>>1;
    build(lson);
    build(rson);
    push_up(l,r,pos);
}
void upd(int l,int r,int pos,int l1,int r1,int v)
{
    if (lazy[pos])push_down(l,r,pos);
    if (l1<=l&&r1>=r){
        lazy[pos]=v;
        a[l]=a[r]=lazy[pos]-1;
        pre[pos]=suf[pos]=now[pos]=(a[l]==0)*(r-l+1);
        return ;
    }
    int mid=(l+r)>>1;
    if (mid>=l1)upd(lson,l1,r1,v);
    if (mid+1<=r1)upd(rson,l1,r1,v);
    push_up(l,r,pos);
}
int query(int l,int r,int pos,int len)
{
    if (lazy[pos])push_down(l,r,pos);
    if (now[pos]<len)return 0;
    if (l==r)return l;
    int mid=(l+r)>>1;
    int lpos=pos<<1,rpos=pos<<1|1;
    if (getit(lson)>=len)return query(lson,len);
    int v2=getit(rson);
    int v3=suf[lpos];
    if (a[mid]==0&&a[mid+1]==0)v3=suf[lpos]+pre[rpos];
    if (v3>=len)return mid-suf[lpos]+1;
    return query(rson,len);
}
int main()
{
    int n,m;
    int tr,x,y;
    while (~scanf("%d%d",&n,&m)){
        memset(a,0,sizeof(a));
        memset(lazy,0,sizeof(lazy));
        build(1,n,1);
        for (int i=0;i<m;i++){
            scanf("%d%d",&tr,&x);
            if (tr==1){
                int ans=query(1,n,1,x);
                printf("%d\n",ans);
                if (ans)
                upd(1,n,1,ans,ans+x-1,2);
            }else {
                scanf("%d",&y);
                upd(1,n,1,x,x+y-1,1);
            }
        }
    }
    return 0;
}

 

在 vj 上看到了一个极快的vector暴力: 代码

* 分块这题应该也是可做的。(留坑,有空补上代码)

* 应该有瞎搞做法?(想到了补上)

以上是关于Hotel poj 3667的主要内容,如果未能解决你的问题,请参考以下文章

线段树区间合并POJ3667-Hotel

POJ 3667 Hotel

POJ 3667 Hotel

poj 3667 Hotel - 线段树

[POJ3667]Hotel

Hotel poj 3667