zoukankan      html  css  js  c++  java
  • luogu P2894 [USACO08FEB]酒店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.

    奶牛们最近的旅游计划,是到苏必利尔湖畔,享受那里的湖光山色,以及明媚的阳光。作为整个旅游的策划者和负责人,贝茜选择在湖边的一家著名的旅馆住宿。这个巨大的旅馆一共有N (1 <= N <= 50,000)间客房,它们在同一层楼中顺次一字排开,在任何一个房间里,只需要拉开窗帘,就能见到波光粼粼的湖面。 贝茜一行,以及其他慕名而来的旅游者,都是一批批地来到旅馆的服务台,希望能订到D_i (1 <= D_i <= N)间连续的房间。服务台的接待工作也很简单:如果存在r满足编号为r..r+D_i-1的房间均空着,他就将这一批顾客安排到这些房间入住;如果没有满足条件的r,他会道歉说没有足够的空房间,请顾客们另找一家宾馆。如果有多个满足条件的r,服务员会选择其中最小的一个。 旅馆中的退房服务也是批量进行的。每一个退房请求由2个数字X_i、D_i 描述,表示编号为X_i..X_i+D_i-1 (1 <= X_i <= N-D_i+1)房间中的客人全部离开。退房前,请求退掉的房间中的一些,甚至是所有,可能本来就无人入住。 而你的工作,就是写一个程序,帮服务员为旅客安排房间。你的程序一共需要处理M (1 <= M < 50,000)个按输入次序到来的住店或退房的请求。第一个请求到来前,旅店中所有房间都是空闲的。

    输入输出格式

    输入格式:

    • 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

    输出格式:

    • 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.

    参考样例,第一行输入n,m ,n代表有n个房间,编号为1---n,开始都为空房,m表示以下有m行操作,以下 每行先输入一个数 i ,表示一种操作:

    若i为1,表示查询房间,再输入一个数x,表示在1--n 房间中找到长度为x的连续空房,输出连续x个房间中左端的房间号,尽量让这个房间号最小,若找不到长度为x的连续空房,输出0。

    若i为2,表示退房,再输入两个数 x,y 代表 房间号 x---x+y-1 退房,即让房间为空。

    输入输出样例

    输入样例#1:
    10 6
    1 3
    1 3
    1 3
    1 3
    2 5 5
    1 6
    
    输出样例#1:
    1
    4
    7
    0
    5

    线段树维护最长子段

    线段树维护
    同最大子段和 + 懒标记

    操作:
    1.查询长度 >= x的子段段的最左端点
    2.区间修改

    对于1操作
    若根节点的max < x, 则无解
    else
      查询最靠左的端点


    查询的优先级
    1.完全包含(做区间的max >= x)于左区间,查询左区间
    2.包含于左右区间的中间, 返回当前区间的中点 - l_max + 1(这个很显然)

    down和update都很好理解

    update时
    最大子段和在确定当前区间的l_max / r_max 时(以l_max为例)

      左儿子的 l_max 和 左儿子的值 + 右儿子l_max
    中,取最大值
    而这道题
      当且仅当
        左儿子的 l_max == 左儿子的max
      时,该节点的l_max = 左儿子的值 + 右儿子的l_max,且不取max
    否则
      该节点的l_max = 左二子的l_max

    还没调出来

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    #include <string>
    #include <cmath>
    
    using namespace std;
    const int N = 5e5 + 5;
    
    struct Node{
        int l, r, w, max, l_max, r_max, f;
    };
    Node T[N];
    int n, m;
    
    struct HOTEL{
        inline int read(){
            int x = 0; char c = getchar();
            while(c < '0' || c > '9') c = getchar();
            while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
            return x; 
        }
        
        void Update(int jd){
            T[jd].max = max(max(T[jd << 1].max, T[jd << 1 | 1].max), T[jd << 1].r_max + T[jd << 1 | 1].l_max);
            if(T[jd << 1].w == T[jd << 1].max) T[jd].l_max = T[jd << 1].w + T[jd << 1 | 1].l_max;
            else T[jd].l_max = T[jd << 1].l_max;
            if(T[jd << 1 | 1].w == T[jd << 1 | 1].max) T[jd].r_max = T[jd << 1 | 1].w + T[jd << 1].r_max;
            else T[jd].r_max = T[jd << 1 | 1].r_max;
            return ;
        }
        
        void build_tree(int l, int r, int jd){
            T[jd].l = l;
            T[jd].r = r;
            T[jd].w = T[jd].l_max = T[jd].r_max = T[jd].max = r - l + 1;
            if(l == r) return ;
            int mid = (l + r) >> 1;
            build_tree(l, mid, jd << 1);
            build_tree(mid + 1, r, jd << 1 | 1);
        }
        
        void down(int jd){
            T[jd << 1].f = T[jd << 1 | 1].f = T[jd].f;
            if(T[jd].f == 1){//1 wei qing ling biao shi zhu shang le people
                T[jd << 1].max = T[jd << 1].l_max = T[jd << 1].r_max = 0;
                T[jd << 1 | 1].max = T[jd << 1 | 1].l_max = T[jd << 1 | 1].r_max = 0;
            }
            else{
                T[jd << 1].max = T[jd << 1].l_max = T[jd << 1].r_max = T[jd << 1].w;
                T[jd << 1 | 1].max = T[jd << 1 | 1].l_max = T[jd << 1 | 1].r_max = T[jd << 1 | 1].w;
            }
            T[jd].f = 0;
        }
        
        int Ask(int l, int r, int jd, int len){
            if(l == r) return l;
            if(T[jd].f) down(jd);
            int mid = (l + r) >> 1;
            if(T[jd << 1].max >= len) Ask(l, mid, jd << 1, len);
            if(T[jd << 1].r_max + T[jd << 1 | 1].l_max >= len) return mid - T[jd << 1].r_max + 1;
            return Ask(mid + 1, r, jd << 1 | 1, len);
        }
        void Sec_g(int l, int r, int jd, int x, int y, int yg){
            if(x <= l && r <= y){
                if(yg == 1){
                    T[jd].max = T[jd].l_max = T[jd].r_max = 0;
                    T[jd].f = 1;
                    return ;
                }
                else{
                    T[jd].max = T[jd].l_max = T[jd].r_max = T[jd].w;
                    T[jd].f = 2;
                    return ;
                }
            }
            if(T[jd].f) down(jd);
            int mid = (l + r) >> 1;
            if(x <= mid) Sec_g(l, mid, jd << 1, x, y, yg);
            if(y > mid) Sec_g(mid + 1, r, jd << 1 | 1, x, y, yg);
            Update(jd);
        }
    }h;
    
    int main(){
        n = h.read();
        m = h.read();
        h.build_tree(1, n, 1);
        for(int i = 1; i <= m; i ++){
            int how = h.read();
            if(how == 1){
                int len = h.read();
                if(T[1].max < len) printf("0
    ");
                else{
                    int l = h.Ask(1, n, 1, len);
                    printf("%d
    ", l);
                    h.Sec_g(1, n, 1, l, l + len - 1, 1);
                }
            }
            else{
                int x = h.read();
                int y = h.read();
                h.Sec_g(1, n, 1, x, x + y - 1, 2);
            }
        }
        return 0;
    }
    /*
    10 6
    1 3
    1 3
    1 3
    1 3
    2 5 5
    1 6
    */


  • 相关阅读:
    var s=+newDate();
    sql2005+调用c#扩展
    fileAs访问拒绝and net后台打开服务器端文件和关闭服务器端文件
    js中的数组引用类型or值类型
    安装vfp9遇到的问题
    JQuery EasyUI TabPanel
    图标库
    SQL根据指定月份获取当前季度
    JQuery EasyUI DataGrid
    (int)、Int32.Parse()、Convert.ToInt32()类型区别
  • 原文地址:https://www.cnblogs.com/lyqlyq/p/7718564.html
Copyright © 2011-2022 走看看