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,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 退房,即让房间为空。

    输入输出格式

    输入格式:

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

    输入输出样例

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

    线段树维护每个节点左边有多少连续的空的,右边有少连续的空的, 

    最长有多少连续的空的,最长的空位从什么地方开始

    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    #define N 400010
    int sum[N],lm[N],rm[N],m[N],tag[N];
    int n,T;
    inline void update(int rt)
    {
        if(m[rt<<1]==sum[rt<<1]){
            lm[rt]=m[rt<<1]+lm[rt<<1|1];
        }else {
            lm[rt]=lm[rt<<1];
        }
        if(m[rt<<1|1]==sum[rt<<1|1]){
            rm[rt]=m[rt<<1|1]+rm[rt<<1];
        }else {
            rm[rt]=rm[rt<<1|1];
        }
        m[rt]=max(max(m[rt<<1],m[rt<<1|1]),rm[rt<<1]+lm[rt<<1|1]);
        
    }
    inline void pushdown(int rt)
    {
        if(tag[rt]==1){
            tag[rt<<1]=tag[rt<<1|1]=1;
            m[rt<<1]=lm[rt<<1]=rm[rt<<1]=0;
            m[rt<<1|1]=lm[rt<<1|1]=rm[rt<<1|1]=0;
        }
        else {
            tag[rt<<1]=tag[rt<<1|1]=2;
            m[rt<<1]=lm[rt<<1]=rm[rt<<1]=sum[rt<<1];
            m[rt<<1|1]=lm[rt<<1|1]=rm[rt<<1|1]=sum[rt<<1|1];
        }
        tag[rt]=0;
    }
    void build(int l,int r,int rt)
    {
        lm[rt]=rm[rt]=m[rt]=sum[rt]=r-l+1;
        tag[rt]=0;
        if(l==r)return ;
        int mid=(l+r)>>1;
        build(l,mid,rt<<1);
        build(mid+1,r,rt<<1|1);
    }
    
    int query(int l,int r,int rt,int len)
    {
        if(tag[rt]) pushdown(rt);
        if(l==r) return l;
        int mid=(l+r)>>1;
        if(m[rt<<1]>=len) return query(l,mid,rt<<1,len);
        if(rm[rt<<1]+lm[rt<<1|1]>=len)return mid-rm[rt<<1]+1;
        else return query(mid+1,r,rt<<1|1,len);
    }
    
    void modify(int l,int r,int rt,int L,int R,int c)
    {
        if(tag[rt])pushdown(rt);
        if(L<=l&&r<=R){
            if(c==1) m[rt]=lm[rt]=rm[rt]=0;
            else m[rt]=lm[rt]=rm[rt]=sum[rt];
            tag[rt]=c;
            return;
        }
        int mid=(l+r)>>1;
        if(L<=mid) modify(l,mid,rt<<1,L,R,c);
        if(R>mid) modify(mid+1,r,rt<<1|1,L,R,c);
        update(rt);
    }
    int main()
    {
        scanf("%d%d",&n,&T);
        build(1,n,1);
        while(T--)
        {
            int a,b,gg;
            scanf("%d",&gg);
            if(gg==1)
            {
                scanf("%d",&a);
                if(m[1]<a){puts("0");continue;}
                int p=query(1,n,1,a);
                printf("%d
    ",p);
                modify(1,n,1,p,p+a-1,1);
            }else {
                scanf("%d%d",&a,&b);
                modify(1,n,1,a,a+b-1,2);        
            }
        }
        return 0;
     } 
  • 相关阅读:
    Linux基础3-1 Bash及其特性
    三、手写ORM实现数据库更新
    三、TCP协议
    一、OIS七层模型及数据传输过程
    泛型缓存原理
    树莓派公网服务器实现frp内网穿透
    Dto数据传输对象
    Ubuntu下 Nginx静态代理部署网页常见报错
    JWT权限验证
    解决传入的请求具有过多的参数,该服务器支持最多 2100 个参数
  • 原文地址:https://www.cnblogs.com/sssy/p/7305713.html
Copyright © 2011-2022 走看看