POJ--3667 Hotel

Hotel

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 ≤ 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个房间,一开始全空,要入住人,输入1为入住,接下来数字为入住人数,要求要为连续房间号,如果有空的连续的房间,输出最左边的房间号,否则输出0,输入2为清空操作,后面两个数字为起始清空的位置和清空的房间数(没人的也算)
思路:
几乎是线段树区间合并的模板题,有一些小思维,

/*****************************************
线段树的区间合并 
*****************************************/
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>

#define lson rt<<1
#define rson rt<<1|1

using namespace std;

const int maxn = 5e4+5;

struct segtree{
    int l,r;
    int lazy;
    int lsum,rsum,sum;
}ss[maxn<<2];

int Max(int a,int b) {
    return a > b ? a : b;
}

void pushUp(int rt) { 
    ss[rt].lsum = ss[lson].lsum; ss[rt].rsum = ss[rson].rsum;
    ss[rt].sum = Max(ss[lson].sum,ss[rson].sum);
    if ( ss[lson].lsum == ss[lson].r - ss[lson].l + 1 ) ss[rt].lsum += ss[rson].lsum;
    if ( ss[rson].rsum == ss[rson].r - ss[rson].l + 1 ) ss[rt].rsum += ss[lson].rsum;
    ss[rt].sum = Max(ss[rt].sum,ss[lson].rsum + ss[rson].lsum);
}

void pushDown(int rt,int k) {
    if ( ss[rt].lazy != -1 ) {
        ss[lson].lazy = ss[rson].lazy = ss[rt].lazy;
        ss[lson].lsum = ss[lson].rsum = ss[lson].sum = ( ss[rt].lazy ? 0 : (k - (k>>1)) ) ;
        ss[rson].lsum = ss[rson].rsum = ss[rson].sum = ( ss[rt].lazy ? 0 : (k >> 1) );
        ss[rt].lazy = -1;
    }
}

void build(int l,int r,int rt) { 
    ss[rt].l = l,ss[rt].r = r;
    ss[rt].rsum = ss[rt].lsum = ss[rt].sum = r-l+1;
    ss[rt].lazy = -1;
    if(l == r) return;
    int mid = (l+r)>>1;
    build(l,mid,lson); build(mid+1,r,rson);
}

void update(int l,int r,int rt,int op) {  
    if(ss[rt].l >= l && ss[rt].r <= r) {
        ss[rt].lsum = ss[rt].rsum = ss[rt].sum = ( op ? 0 : (ss[rt].r-ss[rt].l+1) );
        ss[rt].lazy = op;
        return;
    }
    pushDown(rt,ss[rt].r-ss[rt].l+1);
    int mid = (ss[rt].l+ss[rt].r)>>1;
    if ( mid >= l ) update(l,r,lson,op);
    if ( r > mid ) update(l,r,rson,op);
    pushUp(rt);
}

int query(int w,int rt) { 
    if (ss[rt].l == ss[rt].r) return 1;
    pushDown(rt,ss[rt].r-ss[rt].l+1);
    int mid = (ss[rt].l+ss[rt].r)>>1;
    if ( ss[lson].sum >= w )
        return query(w,lson);
    else if ( ss[lson].rsum + ss[rson].lsum >= w )
        return mid - ss[lson].rsum + 1 ;
    else return query(w, rson);
}

int main() {
    int n,m,op,a,b;
    scanf("%d %d",&n,&m);
    build(1,n,1);
    while(m--){
        scanf("%d",&op);
        if ( op == 1 ) {
            scanf("%d",&a);
            if(ss[1].sum >= a) {
                int pos = query(a,1);
                printf("%d\n",pos);
                update( pos, pos+a-1, 1, 1);
            } else {
                printf("0\n");
            }
        } else if ( op == 2 ) {
            scanf("%d %d",&a,&b);
            update( a, a+b-1, 1, 0);
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Acer12138/article/details/81776986