zoukankan      html  css  js  c++  java
  • poj Hotel

    Hotel
    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 7782   Accepted: 3299

    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

     
    分析:线段树:成段替换,区间最大值需要区间合并,成段查询。
    #include<cstdio>
    #include<cstring>
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    
    inline int max(int a, int b) {
        return a > b ? a : b;
    }
    int col[1 << 17], lsum[1 << 17], rsum[1 << 17], msum[1 << 17];
    
    void pushup(int rt, int len) {
        if (lsum[rt << 1] == (len - (len >> 1)))
            lsum[rt] = lsum[rt << 1] + lsum[rt << 1 | 1];
        else
            lsum[rt] = lsum[rt << 1];
        if (rsum[rt << 1 | 1] == (len >> 1))
            rsum[rt] = rsum[rt << 1] + rsum[rt << 1 | 1];
        else
            rsum[rt] = rsum[rt << 1 | 1];
        msum[rt] = max(rsum[rt << 1] + lsum[rt << 1 | 1], max(msum[rt << 1], msum[rt << 1 | 1]));
    }
    
    void build(int l, int r, int rt) {
        if (l == r) {
            col[rt] = 0;
            lsum[rt] = rsum[rt] = msum[rt] = 1;
            return;
        }
        int m = (r + l) >> 1;
        build(lson);
        build(rson);
        pushup(rt, r - l + 1);
    }
    
    void pushdown(int rt, int len) {
        if (col[rt] == 1) {
            col[rt << 1] = col[rt << 1 | 1] = 1;
            col[rt] = -1;
            lsum[rt << 1] = rsum[rt << 1] = msum[rt << 1] = 0;
            lsum[rt << 1|1] = rsum[rt << 1|1] = msum[rt << 1|1] = 0;
        } else if (col[rt] == 0) {
            col[rt << 1] = col[rt << 1 | 1] = 0;
            col[rt] = -1;
            lsum[rt << 1] = rsum[rt << 1] = msum[rt << 1] = len - (len >> 1);
            lsum[rt << 1|1] = rsum[rt << 1|1] = msum[rt << 1|1] = len >> 1;
        }
    }
    
    void update(int L, int R, int c, int l, int r, int rt) {
        if (L <= l && R >= r) {
            col[rt] = c;
            if (c == 1)
                lsum[rt] = rsum[rt] = msum[rt] = 0;
            else
                lsum[rt] = rsum[rt] = msum[rt] = r - l + 1;
            return;
        }
        pushdown(rt, r - l + 1);
        int m = (l + r) >> 1;
        if (L <= m)
            update(L, R, c, lson);
        if (R > m)
            update(L, R, c, rson);
        pushup(rt, r - l + 1);
    }
    
    int query(int c, int l, int r, int rt) {
        if (l == r)
            return l;
        pushdown(rt, r - l + 1);
        int m = (l + r) >> 1;
        if (msum[rt << 1] >= c)
            return query(c, lson);
        else if (rsum[rt << 1] + lsum[rt << 1 | 1] >= c)
            return m - rsum[rt << 1] + 1;
        else if (msum[rt << 1 | 1] >= c)
            return query(c, rson);
    }
    
    int main() {
        int n, m, op, a, b, pos;
        scanf("%d%d", &n, &m);
        memset(col,0,sizeof(col));
        build(1, n, 1);
        while (m--) {
            scanf("%d%d", &op, &a);
            if (op == 1) {
                if (msum[1] < a) {
                    printf("0\n");
                } else {
                    pos = query(a, 1, n, 1);
                    printf("%d\n", pos);
                    update(pos, pos + a - 1, 1, 1, n, 1);
                }
            } else if (op == 2) {
                scanf("%d", &b);
                update(a, a + b - 1, 0, 1, n, 1);
            }
        }
        return 0;
    }
  • 相关阅读:
    年终总结 2016-08-28 22:04 422人阅读 评论(26) 收藏
    [mysql]MySQL Daemon failed to start 2016-08-14 21:27 1121人阅读 评论(18) 收藏
    solrr初步了解 2016-07-31 22:29 380人阅读 评论(4) 收藏
    基于spring-boot的测试桩设计--几种常见的controller
    利用Factory-boy和sqlalchemy来批量生成数据库表数据
    job中shell脚本异常(删除不存在容器),导致job被打断执行的问题 脚本优化方法
    利用Factory-boy来生成实例数据
    pytest相关资源收集
    pytest 用 @pytest.mark.usefixtures("fixtureName")装饰类,可以让执行每个case前,都执行一遍指定的fixture
    pytest fixture 利用 params参数实现用例集合
  • 原文地址:https://www.cnblogs.com/baidongtan/p/2717046.html
Copyright © 2011-2022 走看看