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

      

  • 相关阅读:
    PHP 关于字符串操作的练习
    PHP 字符串操作
    JavaScript DOM级事件
    JavaScript 会闪烁的文字
    JavaScript 当输入框获得焦点:如果输入框值为空,提示输入你的姓名,当输入框失去焦点,如果输入框值为空,提示用户名不能为空,边框颜色变为红色,如果输入框值不为0,那么不提示边框默认颜色
    Js 特效之鼠标点击出现小心心特效
    JavaScript BOM对象 DOM对象
    JavaScript 当用户在弹出的输入框中输入手机号码后,将手机号码的前7位转化为*号
    JavaScript 编写代码对用户输入内容的输入框进行排查,看有没有敏感字“草”字
    第一次c++作业小结
  • 原文地址:https://www.cnblogs.com/zero-begin/p/4696738.html
Copyright © 2011-2022 走看看