zoukankan      html  css  js  c++  java
  • POJ3667——线段树区间合并(未搞透)——Hotel

    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
    #include <cstring>
    #include <cstdio>
    #include <algorithm>
    using namespace std;
    
    const int MAX = 50000 + 10;
    int lazy[MAX<<2];
    int n, m;
    struct edge{
        int lsum, rsum, msum;
    }node[MAX<<2];
    
    void build(int rt, int l, int r)
    {
        lazy[rt] = -1;
        node[rt].lsum = node[rt].rsum = node[rt].msum = r - l + 1;
        if(l == r) return;
        int mid = l + r >> 1;
        build(rt*2, l, mid);
        build(rt*2+1, mid + 1, r);
    }
    
    void push_up(int rt, int len)
    {
        node[rt].lsum = node[rt*2].lsum;
        node[rt].rsum = node[rt*2+1].rsum;
        if(node[rt].lsum == len - (len >> 1))
                node[rt].lsum += node[rt*2+1].lsum;
        if(node[rt].rsum == len >> 1)
                node[rt].rsum += node[rt*2].rsum;
        node[rt].msum = max(node[rt*2].rsum + node[rt*2+1].lsum, max(node[rt*2].msum, node[rt*2+1].msum));
    }
    
    void push_down(int rt, int len)
    {
       lazy[rt*2] = lazy[rt*2+1] = lazy[rt];
       node[rt*2].lsum = node[rt*2].rsum = node[rt*2].msum = lazy[rt] ? 0 : len - (len >> 1);
       node[rt*2+1].lsum = node[rt*2+1].rsum = node[rt*2+1].msum = lazy[rt] ? 0 : len >> 1;
       lazy[rt] = -1;
    }
    
    void update(int l, int r, int val, int rt, int L, int R)
    {
        if(L <= l && R >= r){
            node[rt].lsum = node[rt].rsum = node[rt].msum = val ? 0 : r - l + 1;
            lazy[rt] = val;
            return ;
        }if(~lazy[rt]) push_down(rt, r - l + 1);
        int mid = l + r >> 1;
        if(L <= mid) update(l, mid, val ,rt*2, L, R);
        if(R > mid)  update(mid+1, r, val, rt*2+1, L, R);
        push_up(rt, r - l + 1);
    }
    
    int query(int w, int rt, int l, int r)
    {
        if(l == r) return l;
        if(~lazy[rt]) push_down(rt, r - l + 1);
        int mid = l + r >> 1;
        if(node[rt*2].msum >= w)
            return query(w, rt*2, l, mid);
        else if(node[rt*2].rsum + node[rt*2+1].lsum >= w){
            return mid - node[rt*2].rsum + 1;
        }
        else return query(w, rt*2+1, mid + 1, r);
    }
    
    int main()
    {
        int flag;
        int x, y;
        while(~scanf("%d%d", &n, &m)){
            build(1, 1, n);
            while(m--){
            scanf("%d", &flag);
            if(flag == 1) {
                scanf("%d", &x);
                int ans = query(x, 1, 1, n);
                if(node[1].msum < x) printf("0
    ");
                else {
                printf("%d
    ", ans);
                update(1, n, 1, 1, ans, ans + x - 1);
                } 
            }
            else {
                scanf("%d%d", &x, &y);
                update(1, n, 0, 1, x, x + y - 1);
            }
            }
        }
        return 0;
    }
    

      

  • 相关阅读:
    C语言资料分享
    vue子组件添加事件无效解决
    vue-cli3 vue动画 打包后不生效解决
    setTimeOut定时器实现数组内容循环获取
    echart 之仪表盘 动态分段颜色实现
    elementUI table树默认箭头修改
    《二》打包发布工程-README.md编辑
    《一》打包发布工程--npm 打包发布js库篇
    npm打包发布js库包npm ERR! 403 Forbidden
    安装nrm 后执行报错TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string
  • 原文地址:https://www.cnblogs.com/zero-begin/p/4696738.html
Copyright © 2011-2022 走看看