zoukankan      html  css  js  c++  java
  • poj3667Hotel

    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

    分析:就像那道区间种树的题
    每个节点维护该区间内最长的0串(空房)长度,靠左的最长0串长度,靠右的0串长度
    每一次询问住房时,看一下tree[1]的最长0串长度是否大于len
    若大于,则递归查找最优位置,
    若leftchild的最长0串>=len,那答案一定在左儿子中,
    同理若rightchild的最长0串>=len,那答案一定在右儿子中,
    若leftchild的靠右0串+rightchild的靠左0串>=len
    那答案就在tree[lc].y-tree[lc].rs+1

    较简单的代码,然而调了半个小时:
    在所有我注明//==的地方
    我这个zz把==写成了=。。。orz。。。
    同时还有注意在线段树构建和修改的时候不要忘写return和update

    这里写代码片
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    
    using namespace std;
    
    const int INF=0x33333333;
    const int N=50001;
    struct node{
        int x,y,ls,rs,ms,lazy;
    };
    node tree[N<<2];
    int n,m;
    
    void update(int bh)
    {
        int lc=bh<<1;
        int rc=bh<<1|1;
        if (tree[bh].x!=tree[bh].y)
        { 
            if (tree[lc].ms==tree[lc].y-tree[lc].x+1) //都为空
                tree[bh].ls=tree[lc].ms+tree[rc].ls; 
            else tree[bh].ls=tree[lc].ls;
            if (tree[rc].ms==tree[rc].y-tree[rc].x+1)  //==
                tree[bh].rs=tree[rc].ms+tree[lc].rs;
            else tree[bh].rs=tree[rc].rs;
            tree[bh].ms=max(tree[lc].ms,tree[rc].ms);
            tree[bh].ms=max(tree[bh].ms,tree[lc].rs+tree[rc].ls);
        }
    }
    
    void push(int bh)
    {
        int lc=bh<<1;
        int rc=bh<<1|1;
        if (tree[bh].lazy!=0&&tree[bh].x!=tree[bh].y)
        {
            if (tree[bh].lazy==1)  //add
            {
                tree[lc].ls=tree[lc].rs=tree[lc].ms=0;
                tree[rc].ls=tree[rc].rs=tree[rc].ms=0;
            }
            else
            {
                tree[lc].ls=tree[lc].rs=tree[lc].ms=tree[lc].y-tree[lc].x+1;
                tree[rc].ls=tree[rc].rs=tree[rc].ms=tree[rc].y-tree[rc].x+1;
            }
            tree[lc].lazy=tree[rc].lazy=tree[bh].lazy;
            tree[bh].lazy=0;
        }
    }
    
    void build(int bh,int l,int r)
    {
        tree[bh].x=l;
        tree[bh].y=r;
        if (l==r)
        {
            tree[bh].ls=tree[bh].rs=tree[bh].ms=1;
            return;
        }
        int mid=(l+r)>>1;
        build(bh<<1,l,mid);
        build(bh<<1|1,mid+1,r);
        update(bh);
    }
    
    void add(int bh,int l,int r)
    {
        push(bh);
        if (tree[bh].x>=l&&tree[bh].y<=r) 
        {
            tree[bh].ls=tree[bh].rs=tree[bh].ms=0;  //计算0序列长度 
            tree[bh].lazy=1;
            return;  //
        }
        int mid=(tree[bh].x+tree[bh].y)>>1;
        if (l<=mid) add(bh<<1,l,r);
        if (r>mid) add(bh<<1|1,l,r);
        update(bh);  //
    }
    
    void jian(int bh,int l,int r)  //退房 
    {
        push(bh);
        if (tree[bh].x>=l&&tree[bh].y<=r) 
        {
            tree[bh].ls=tree[bh].rs=tree[bh].ms=tree[bh].y-tree[bh].x+1;
            tree[bh].lazy=-1;
            return;  //
        }
        int mid=(tree[bh].x+tree[bh].y)>>1;
        if (l<=mid) jian(bh<<1,l,r);
        if (r>mid) jian(bh<<1|1,l,r);
        update(bh);  //
    }
    
    int doit(int bh,int len)
    {
        push(bh);
        if (tree[bh].x==tree[bh].y) return tree[bh].x;  //==
        int lc=bh<<1;
        int rc=bh<<1|1;
        push(lc); push(rc);
        if (tree[lc].ms>=len) doit(lc,len);
        if (tree[lc].rs+tree[rc].ls>=len) return tree[lc].y-tree[lc].rs+1;  //位置
        else if (tree[rc].ms>=len) doit(rc,len);  //else 
    }
    
    int main()
    {
        scanf("%d%d",&n,&m);
        build(1,1,n);
        for (int i=1;i<=m;i++)
        {
            int opt,u,w;
            scanf("%d",&opt);
            if (opt==1)
            {
                scanf("%d",&u);
                if (tree[1].ms<u) printf("0
    ");
                else 
                {
                    int t=doit(1,u);
                    printf("%d
    ",t);  //
                    add(1,t,t+u-1);
                }
            }
            else
            {
                scanf("%d%d",&u,&w);
                jian(1,u,u+w-1);
            }
        }
        return 0;
    } 
  • 相关阅读:
    网络安全之常见攻击
    引入element-ui
    引入sass
    浏览器解析流程
    JDK8 HashMap--removeNode()移除节点方法
    JDK8 HashMap--treeify()树形化方法
    JDK1.8 HashMap--treeifyBin()方法
    二叉查找树ADT
    队列ADT

  • 原文地址:https://www.cnblogs.com/wutongtong3117/p/7673499.html
Copyright © 2011-2022 走看看