Luogu2894 [USACO08FEB]Hotel G

原题链接:https://www.luogu.com.cn/problem/P2894

Hotel G

题目描述

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。若找得到,在这x个空房间中住上人。

若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

题解

比较传统的线段树题目了,维护左端点开头的空房间长度、右端点开头的空房间个数和区间最长连续空房间个数即可合并统计出整棵线段树的连续空房间信息。

对于入住和退房操作,直接打标记即可,注意这两个标记无法共存,打上一个标记的时候要直接清除另外一个标记。

具体维护可以参考代码,个人觉得还是比较易懂(忽略我的三目运算……

代码

第一次交的时候少了特判,即使传回的房间编号是 0 0 0也去入住,痛失一 A A A

#include<bits/stdc++.h>
#define ls v<<1
#define rs v<<1|1
using namespace std;
const int M=5e4+5;
struct node{
    
    
    int le,ri,llen,rlen,mlen;
    bool clear,fill;
}tree[M<<2];
int n,m;
void up(int v)
{
    
    
    tree[v].llen=(tree[ls].llen==tree[ls].ri-tree[ls].le+1)?tree[ls].llen+tree[rs].llen:tree[ls].llen;
    tree[v].rlen=(tree[rs].rlen==tree[rs].ri-tree[rs].le+1)?tree[rs].rlen+tree[ls].rlen:tree[rs].rlen;
    tree[v].mlen=max(tree[ls].rlen+tree[rs].llen,max(tree[ls].mlen,tree[rs].mlen));
}
void push1(int v)
{
    
    
    tree[v].llen=tree[v].rlen=tree[v].mlen=tree[v].ri-tree[v].le+1;
    tree[v].clear=1,tree[v].fill=0;
}
void push2(int v)
{
    
    
    tree[v].llen=tree[v].rlen=tree[v].mlen=0;
    tree[v].fill=1,tree[v].clear=0;
}
void down(int v)
{
    
    
    if(tree[v].clear)
    {
    
    
        push1(ls),push1(rs);
        tree[v].clear=0;
    }
    if(tree[v].fill)
    {
    
    
        push2(ls),push2(rs);
        tree[v].fill=0;
    }
}
void build(int v,int le,int ri)
{
    
    
    tree[v].le=le,tree[v].ri=ri;
    //tree[v].llen=tree[v].rlen=tree[v].mlen=tree[v].ri-tree[v].le+1;
    if(le==ri)
    {
    
    
        tree[v].llen=tree[v].rlen=tree[v].mlen=1;
        return;
    }
    int mid=le+ri>>1;
    build(ls,le,mid),build(rs,mid+1,ri);
    up(v);
}
void fill(int v,int le,int ri)
{
    
    
    if(le<=tree[v].le&&tree[v].ri<=ri)
    {
    
    
        tree[v].llen=tree[v].rlen=tree[v].mlen=0;
        tree[v].fill=1,tree[v].clear=0;
        return;
    }
    down(v);
    int mid=tree[v].le+tree[v].ri>>1;
    if(le<=mid)fill(ls,le,ri);
    if(mid<ri)fill(rs,le,ri);
    up(v);
}
void clear(int v,int le,int ri)
{
    
    
    if(le<=tree[v].le&&tree[v].ri<=ri)
    {
    
    
        tree[v].llen=tree[v].rlen=tree[v].mlen=tree[v].ri-tree[v].le+1;
        tree[v].clear=1,tree[v].fill=0;
        return;
    }
    down(v);
    int mid=tree[v].le+tree[v].ri>>1;
    if(le<=mid)clear(ls,le,ri);
    if(mid<ri)clear(rs,le,ri);
    up(v);
}
int find(int v,int len)
{
    
    
    if(tree[v].mlen<len)return 0;
    down(v);
    if(tree[ls].mlen>=len)return find(ls,len);
    if(tree[ls].rlen+tree[rs].llen>=len)return tree[ls].ri-tree[ls].rlen+1;
    return find(rs,len);
}
void in()
{
    
    
    scanf("%d%d",&n,&m);
}
void ac()
{
    
    
    build(1,1,n);
    for(int i=1,a,b;i<=m;++i)
    {
    
    
        scanf("%d%d",&a,&b);
        if(a-1)scanf("%d",&a),clear(1,b,b+a-1);
        else
        {
    
    
            printf("%d\n",a=find(1,b));
            if(a)fill(1,a,a+b-1);
        }
    }
}
int main()
{
    
    
    in(),ac();
    system("pause");
}

猜你喜欢

转载自blog.csdn.net/ShadyPi/article/details/113329015
今日推荐