zoukankan      html  css  js  c++  java
  • POJ 3667 Hotel (线段树)

    Hotel
    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 8736   Accepted: 3701

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

    Source

    题意:bessie 有间旅店有N个房间 每次有一组人来住宿,他们需要连续的房间 。bessie要求你帮他个忙,有以下操作
    op a :如果op == 1 表示有a个人要住宿 要求你找出a个连续的房间 起始房间的号数要求最小,如果没有返回0
    op a b :op == 2 表示从a号房起有b个人有退房 即 a到a+b-1 的房间将置空。

    思路:超经典的线段树 这道题跟上一道Hotel很像,但稍微复杂点。具体见代码说明。

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    
    using namespace std;
    
    #define L(rt) (rt<<1)
    #define R(rt) (rt<<1|1)
    
    const int N=50010;
    
    struct node{
        int l,r,state;  //state -1表空,0表中间态,1表满
        int lma,mma,rma;    //lma表左边连续的房间数,rma表示右边连续的房间数,ma表示最大的连续房间数
    }tree[N*3];
    
    void build(int l,int r,int rt){
        tree[rt].l=l;
        tree[rt].r=r;
        if(l==r)
            return ;
        int mid=(l+r)>>1;
        build(l,mid,L(rt));
        build(mid+1,r,R(rt));
    }
    
    void PushDown(int rt){  //延迟覆盖操作
        if(tree[rt].state==-1){
            tree[rt].state=0;
            tree[L(rt)].state=-1;
            tree[L(rt)].lma=tree[L(rt)].mma=tree[L(rt)].rma=tree[L(rt)].r-tree[L(rt)].l+1;
            tree[R(rt)].state=-1;
            tree[R(rt)].lma=tree[R(rt)].mma=tree[R(rt)].rma=tree[R(rt)].r-tree[R(rt)].l+1;
        }else if(tree[rt].state==1){
            tree[rt].state=0;
            tree[L(rt)].state=1;
            tree[L(rt)].lma=tree[L(rt)].mma=tree[L(rt)].rma=0;
            tree[R(rt)].state=1;
            tree[R(rt)].lma=tree[R(rt)].mma=tree[R(rt)].rma=0;
        }
    }
    
    int query(int len,int rt){
        if(tree[rt].lma>=len)   //如果左连续的房间数满足要求
            return tree[rt].l;
        if(tree[L(rt)].mma>=len)    //左结点的最大连续房间数满足要求 从左搜索(因为要求房号最小)
            return query(len,L(rt));
        if(tree[L(rt)].rma+tree[R(rt)].lma>=len)    //如果中间连续的 满足要求
            return tree[L(rt)].r-tree[L(rt)].rma+1;
        if(tree[R(rt)].mma>=len)    //右结点的最大连续房间数满足要求
            return query(len,R(rt));
        return -1;
    }
    
    void PushUp(int rt){     //合并操作
        if(tree[L(rt)].state==-1)   //左为空 求node[u] 的左最大连续 以下同理
            tree[rt].lma=tree[L(rt)].mma+tree[R(rt)].lma;
        else
            tree[rt].lma=tree[L(rt)].lma;
        if(tree[R(rt)].state==-1)
            tree[rt].rma=tree[R(rt)].mma+tree[L(rt)].rma;
        else
            tree[rt].rma=tree[R(rt)].rma;
        int mid_n=tree[L(rt)].rma+tree[R(rt)].lma;
        int a=max(tree[L(rt)].mma,tree[R(rt)].mma);
        int b=max(tree[rt].lma,tree[rt].rma);
        tree[rt].mma=max(max(a,b),mid_n);
    }
    
    void update(int l,int r,int rt){     //更新操作
        if(l<=tree[rt].l && r>=tree[rt].r){
            tree[rt].state=1;
            tree[rt].lma=tree[rt].mma=tree[rt].rma=0;
            return ;
        }
        if(tree[rt].state!=0)   //修改子结点的信息
            PushDown(rt);
        int mid=(tree[rt].l+tree[rt].r)>>1;
        if(r<=mid)
            update(l,r,L(rt));
        else if(l>=mid+1)
            update(l,r,R(rt));
        else{
            update(l,r,L(rt));
            update(l,r,R(rt));
        }
        PushUp(rt);
        if(tree[L(rt)].state==tree[R(rt)].state)    //递归回来 修改父亲结点的状态
            tree[rt].state=tree[L(rt)].state;
    }
    
    void free(int l,int r,int rt){  //释放操作 跟update 操作差不多
        if(tree[rt].state==-1)
            return ;
        if(tree[rt].l==l && tree[rt].r==r){
            tree[rt].state=-1;
            tree[rt].lma=tree[rt].mma=tree[rt].rma=tree[rt].r-tree[rt].l+1;
            return ;
        }
        if(tree[rt].state==1)
            PushDown(rt);
        int mid=(tree[rt].l+tree[rt].r)>>1;
        if(r<=mid)
            free(l,r,L(rt));
        else if(l>=mid+1)
            free(l,r,R(rt));
        else{
            free(l,mid,L(rt));
            free(mid+1,r,R(rt));
        }
        PushUp(rt);
        if(tree[L(rt)].state==tree[R(rt)].state)
            tree[rt].state=tree[L(rt)].state;
    }
    
    int main(){
    
        //freopen("input.txt","r",stdin);
    
        int n,m;
        while(~scanf("%d%d",&n,&m)){
            build(1,n,1);
            tree[1].state=-1;
            tree[1].lma=tree[1].mma=tree[1].rma=tree[1].r-tree[1].l+1;
            int op,a,b;
            while(m--){
                scanf("%d",&op);
                if(op==1){
                    scanf("%d",&a);
                    int src=query(a,1);
                    if(src==-1)
                        printf("0\n");
                    else{
                        printf("%d\n",src);
                        update(src,src+a-1,1);
                    }
                }else{
                    scanf("%d%d",&a,&b);
                    free(a,a+b-1,1);
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    曲面的外在几何(一)---曲面的基本理论
    几个积性函数的均值
    多重小数部分和的渐近式与小数部分积分(Ⅱ)
    二重小数部分和的渐近式
    一个极限问题
    正整数互素的概率问题
    多重小数部分和的渐近式与Ovidiu Furdui积分问题
    $prodlimits_{substack{(k,n)=1 \ 1leqslant k leqslant n}} k$ 的阶
    2016 年中国科学院大学数学分析考研试题
    无平方因子数的分布 (Ⅰ)
  • 原文地址:https://www.cnblogs.com/jackge/p/3041553.html
Copyright © 2011-2022 走看看