• 【POJ 3667】Hotel


    Hotel
    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 14238   Accepted: 6181

    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

     
    线段树秒之。
    线段树维护3个变量:可以向后延伸到哪间房、可以向前延伸到哪间房、此区间内最长的连续没人住的房间的值。
     
    首先,说说找房操作,二分查找+线段树O(log2N*log2N),把每段区间二分,如果左区间的最大值满足,递归左区间,否则右区间满足就递归,再否则,就没有任何一间房子符合。
    进房,进r~r+d-1很明显我们要找到r+d最后可以延伸到哪间房,这段区间也是要更新的!
    退房这个操作就比较复杂一点了,找x-1这间房的最前可以延伸到哪间房以及x+d这间房最后可以到哪间房,更新这段区间即可。
    虽然话是这么说,但是细节还是比较多。
    代码猥琐程度简直让人不敢直视。
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    
    using namespace std;
    
    const int MAXN = 50005;
    
    int prev[MAXN << 2], sufv[MAXN << 2], msv[MAXN << 2], _last, _msv, _fr, y1, y2;
    int n, m, d;
    
    void maintain(int o, int l, int r)
    {
        int mid = (l + r) >> 1;
        int lc = o << 1, rc = lc + 1;
        if (l == r)
        {
            msv[o] = sufv[o] - l + 1;
        }
        else
        {
            if (sufv[o]) msv[o] = sufv[o] - l + 1;
            else msv[o] = max(msv[lc], msv[rc]);
        }
    }
    
    void pushdown(int o)
    {
        int lc = o << 1, rc = lc + 1;
        if (sufv[o])
        {
            sufv[lc] = sufv[rc] = sufv[o];
            sufv[o] = 0;
        }
        if (prev[o])
        {
            prev[lc] = prev[rc] = prev[o];
            prev[o] = 0;
        }
    }
    
    void update(int o, int l, int r, int st, int ed)
    {
        if (y1 <= l && r <= y2)
        {
            prev[o] = st;
            sufv[o] = ed;
            msv[o] = ed - l + 1;
        }
        else
        {
            pushdown(o);
            int mid = (l + r) >> 1;
            int lc = o << 1, rc = lc + 1;
            if (mid >= y1) update(lc, l, mid, st, ed);
            else maintain(lc, l, mid);
            if (mid + 1 <= y2) update(rc, mid + 1, r, st, ed);
            else maintain(rc, mid + 1, r);
        }
        maintain(o, l, r);
    }
    
    void query(int o, int l, int r)
    {
        if (sufv[o] || prev[o])
        {
            _last = sufv[o];
            _fr = prev[o];
            _msv = max(_msv, msv[o]);
            return;
        }
        if ((y1 <= l) && (r <= y2))
        {
            _last = sufv[o];
            _fr = prev[o];
            _msv = max(_msv, msv[o]);
        }
        else
        {
            int mid = (l + r) >> 1;
            int lc = o << 1, rc = lc + 1;
            if (mid >= y1) query(lc, l, mid);
            if (mid + 1 <= y2) query(rc, mid + 1, r);
        }
    }
    
    int binarySearch(int l, int r)
    {
        if (l == r)
        {
            return l;
        }
        int mid = (l + r) >> 1;
        _msv = 0;
        y1 = l;
        y2 = mid;
        query(1, 1, n);
        if (_msv >= d) return binarySearch(l, mid);
        else
        {
            y1 = mid + 1;
            y2 = r;
            _msv = 0;
            query(1, 1, n);
            if (_msv >= d) return binarySearch(mid + 1, r);
        }
        return 0;
    }
    
    int main()
    {
        scanf("%d%d", &n, &m);
        memset(prev, 0, sizeof(prev));
        memset(sufv, 0, sizeof(sufv));
        memset(msv, 0, sizeof(msv));
        y1 = 1;
        y2 = n;
        update(1, 1, n, 1, n);
        for (int i = 0; i < m; ++i)
        {
            int cmd;
            scanf("%d%d", &cmd, &d);
            if (cmd == 1)
            {
                int r = binarySearch(1, n);
                printf("%d
    ", r);
                if (r)
                {
                    y1 = r;
                    y2 = r + d - 1;
                    update(1, 1, n, -1, -1);
                    y1 = y2 = r + d;
                    query(1, 1, n);
                    if (_last > 0)
                    {
                        y1 = r + d;
                        y2 = _last;
                        update(1, 1, n, r + d, _last);
                    }
                }
            }
            else
            {
                int x, ml, mr;
                scanf("%d", &x);
                y1 = y2 = d - 1;
                _fr = 0;
                if (d > 1) query(1, 1, n);
                if (_fr > 0) ml = _fr;
                else ml = d;
                y1 = y2 = d + x;
                _last = 0;
                if (d + x <= n) query(1, 1, n);
                if (_last > 0) mr = _last;
                else mr = d + x - 1;
                y1 = ml;
                y2 = mr;
                update(1, 1, n, y1, y2);
            }
        }
        return 0;
    }
  • 相关阅读:
    逆向分析实战
    打印工程内所有方法的调用
    JAVA B/S系统实现客户端屏幕截图,Java版的QQ截图
    让input支持 ctrl v上传粘贴图片? 让input支持QQ截图或剪切板中的图像数据(Java实现保存)
    spring4 security 4 +websocket 实现单点登录
    Spring quartz 单机、集群+websocket集群实现文本、图片、声音、文件下载及推送、接收及显示
    spring 4.2.0后jdbcTemplate中不用queryForLong了(之系统升级发现)
    友好解决POI导入Excel文件行是不是为空
    解决POI读取Excel如何判断行是不是为空
    集群: 如何在spring 任务中 获得集群中的一个web 容器的端口号?
  • 原文地址:https://www.cnblogs.com/albert7xie/p/4820606.html
Copyright © 2020-2023  润新知