POJ 3667 Hotel 线段树区间合并+延迟标记

                                                                                     Hotel

Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 21646 Accepted: 9427

Description

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 Di (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

题意:

一群人去住旅馆,有两种操作,操作1 :“1   x”查询一段连续的房间号给这x个人住(每人一个房间),输出第一个房间号,操作2:“2    x  y” 清空从 x 开始的 y 个房间

思路:

线段树区间合并 和 标记

这个题目,需要对区间进行更新,区间更新 <--> 延迟更新(Lazy-tag);结构体中的 f 用来延迟更新的,f =  -1 表示清除标记时的状态, f =0 表示将区间全部占用时的状态,f  = 1 表示区间为空的状态,一般来说,更新的状态有 i 种,就对应着 f 的值就有i+1 种,因为其中一种是用来清除标记的(讲解转自:https://blog.csdn.net/ACMore_Xiong/article/details/47403479)

 

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cmath>
#include<stack>
#include<vector>
#define ls rt<<1
#define rs rt<<1|1
#define mod 10007
using namespace std;
typedef long long ll;
const int maxn = 5e4+10;
struct node
{
    int lmax;  //包含左端点的最大空间
    int sum;   //最大空间
    int rmax;  //包含右端点的最大空间
    int f;  //标记
}t[maxn<<2];
void pushup(int l,int r,int rt)   //区间合并 找出最大区间
{
    int m = (l+r)>>1;
    t[rt].lmax = t[ls].lmax;
    t[rt].rmax = t[rs].rmax;
    t[rt].sum = max(max(t[ls].sum,t[rs].sum) , t[ls].rmax+t[rs].lmax);

    if(t[ls].lmax == m-l+1)
        t[rt].lmax += t[rs].lmax;
    if(t[rt].rmax == r-m)
        t[rt].rmax += t[ls].rmax;
}
void build(int l,int r,int rt)
{
    t[rt].lmax=t[rt].rmax=t[rt].sum=r-l+1;
    t[rt].f = -1;
    if(l!=r)
    {
        int m = (l+r)>>1;
        build(l,m,ls);
        build(m+1,r,rs);
    }
}
void pushdown(int l,int r,int rt)  //标记下传
{
    int m = (l+r)>>1;
    if(t[rt].f==1)  //为空
    {
        t[ls].lmax = t[ls].sum = t[ls].rmax = m-l+1;
        t[rs].lmax = t[rs].sum = t[rs].rmax = r-m;
        t[ls].f = t[rs].f = 1;
        t[rt].f = -1;
    }
    else if(t[rt].f==0)  //入住
    {
        t[ls].lmax = t[ls].sum = t[ls].rmax = 0;
        t[rs].lmax = t[rs].sum = t[rs].rmax = 0;
        t[ls].f = t[rs].f = 0;
        t[rt].f = -1;
    }
}
void update(int k,int L,int R,int l,int r,int rt)  //k表示状态  L~R为更新范围
{
    if(L<=l && R>=r)
    {
        if(k==0)   //入住
        {
            t[rt].lmax=t[rt].sum=t[rt].rmax=0;
            t[rt].f = 0;
        }
        else if(k==1)  //清空
        {
            t[rt].lmax=t[rt].sum=t[rt].rmax=r-l+1;
            t[rt].f = 1;
        }
        return ;
    }
    pushdown(l,r,rt);  //向下更新子节点
    int m = (l+r)>>1;
    if(L<=m)
        update(k,L,R,l,m,ls);
    if(R>m)
        update(k,L,R,m+1,r,rs);
    pushup(l,r,rt);  //区间合并
}
int query(int x,int l,int r,int rt)
{
    if(l==r)   
        return l;    //返回左端点
    pushdown(l,r,rt);
    int m = (l+r)>>1;

    if(t[ls].sum >= x)   //优先查找左区间
        return query(x,l,m,ls);
    else if(t[ls].rmax+t[rs].lmax >= x) //查找左区间的右部分和有区间的左部分是否能形成需要空间
        return m-t[ls].rmax+1;
    else                        //查找右区间
        return query(x,m+1,r,rs);

}
int main()
{
    int n,m;
    int k,x,y;
    scanf("%d%d",&n,&m);
    build(1,n,1);
    for(int i=0;i<m;i++)
    {
        scanf("%d",&k);
        if(k==1)   //住人
        {
            scanf("%d",&x);
            if(t[1].sum >= x)  //有满足条件的连续房间
            {
                int a = query(x,1,n,1); //查询位置
                printf("%d\n",a);
                update(0,a,a+x-1,1,n,1);
            }
            else               //没有合适的房间
                printf("0\n");

        }
        else    //离开
        {
            scanf("%d%d",&x,&y);
            update(1,x,x+y-1,1,n,1);  //清空
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值