Hotel--POJ3667(线段树简单区间合并详解)

Hotel

Time Limit: 3000MS Memory Limit: 65536K

题目链接http://poj.org/problem?id=3667

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 rto 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

题目大意:n个连续的房间m个操作。操作分两种,第一种以1 x形式给出,找到最左的能连续容下x个人的连续房间,并输出左端点的编号,如果找不到就输出0.第二种以2 l x的形式给出,表示以l为起点的x个房间都清空。

线段树区间合并的板子题。。。对刚开始触及到的萌新还是稍微提一下:

我们设置变量lsum,rsum,sum,f分别表示节点从左端点开始最大的连续区间,从右端点开始的连续区间,整段区间的最大连续区间,标记。

区间和并简单来讲就是当一个节点的左儿子的左端点最大连续区间==区间长度的时候,我们将该节点的lsum赋值为左儿子的区间长度+右儿子的左端点开始的最大连续区间,同样的道理,右儿子的右端点最大连续区间==区间长度的时候也是一样的操作:

if (tree[ls].lsum==(mid-l+1)) tree[rt].lsum=tree[ls].sum+tree[rs].lsum;//区间合并
if (tree[rs].rsum==(r-mid)) tree[rt].rsum=tree[rs].sum+tree[ls].rsum;//

事实上,区间合并就是这两行,其他的都是线段树的操作了。。。就看你怎么维护这些变量了

更新操作的时候,其他的都好办,和区间染色一样,f直接更新,这里就稍微提一下下传标记和上传值。下传标记我们和区间染色一样是直接覆盖的,那么我们左右儿子的值就直接等于父亲的标记*区间长度。上传值的时候我们就需要区间合并了,不过在这之前我们还应该普通更新一下,也就是该节点的sum的值是又左右儿子的sum或者左右儿子合并之后值得来的,那么也就是说:sum=max(max(ls->sum,rs->sum),ls->rsum+rs->lsum)。那么他的lsum和rsum可以直接由左儿子的左端点和右儿子的右端点直接转移过来:

void pushup(int l,int r,int rt)
{
    int mid=(l+r)>>1;
	tree[rt].sum=max(max(tree[ls].sum,tree[rs].sum),tree[ls].rsum+tree[rs].lsum);
    tree[rt].lsum=tree[ls].lsum;tree[rt].rsum=tree[rs].rsum;
    if (tree[ls].lsum==(mid-l+1)) tree[rt].lsum=tree[ls].sum+tree[rs].lsum;//区间合并 
    if (tree[rs].rsum==(r-mid)) tree[rt].rsum=tree[rs].sum+tree[ls].rsum;//
}


void pushdown(int l,int r,int rt)
{
    int mid=(l+r)>>1;
    tree[ls].sum=tree[ls].rsum=tree[ls].lsum=tree[rt].f*(mid-l+1);
    tree[rs].sum=tree[rs].rsum=tree[rs].lsum=tree[rt].f*(r-mid);
    tree[ls].f=tree[rs].f=tree[rt].f;
    tree[rt].f=-1;
}

查询的时候我们要怎么找呢?我们先判断一下该节点的sum值是否大于等于所需要的空间,如果满足的话就开始判断了,否则直接返回

0。我们从小开始找,那么我们先判断左儿子的sum是否>=size,如果ok的话直接递归就不管了,否则的话,判断合并之后是否中间的合并区间>=size,即tree[ls].rsum+tree[rs].lsum>=size,如果ok的话我们就可以直接确定最小编号了,为mid-tree[ls].rsum+1。然后我们继续找右区间,和左区间一样的写法就不多说了:

int query(int l,int r,int rt,int siz)
{
    if (l==r && siz==1) return l; //特判
    int mid=(l+r)>>1;
    if (tree[rt].f!=-1) pushdown(l,r,rt);
    if (tree[rt].sum>=siz){
    	if (tree[ls].sum>=siz) return query(lson,siz);
    	else if (tree[ls].rsum+tree[rs].lsum>=siz) return mid-tree[ls].rsum+1;
		else return query(rson,siz); 
	}
	return 0;
}

如果上面理解了的话,下面的AC代码也可以自己写了。

以下是AC代码:

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;

#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define ls rt<<1
#define rs rt<<1|1

const int mac=5e4+10;

struct node
{
    int lsum,rsum,sum;
    int f;
}tree[mac<<2];

void pushup(int l,int r,int rt)
{
    int mid=(l+r)>>1;
	tree[rt].sum=max(max(tree[ls].sum,tree[rs].sum),tree[ls].rsum+tree[rs].lsum);
    tree[rt].lsum=tree[ls].lsum;tree[rt].rsum=tree[rs].rsum;
    if (tree[ls].lsum==(mid-l+1)) tree[rt].lsum=tree[ls].sum+tree[rs].lsum;//区间合并 
    if (tree[rs].rsum==(r-mid)) tree[rt].rsum=tree[rs].sum+tree[ls].rsum;//
}

void build(int l,int r,int rt)
{
    tree[rt].f=-1;
    tree[rt].lsum=tree[rt].rsum=tree[rt].sum=(r-l+1);
    //printf ("%d %d :%d %d %d\n",l,r,tree[rt].lsum,tree[rt].sum,tree[rt].rsum);
    if (l==r) return;
    int mid=(l+r)>>1;
    build(lson);build(rson);
}

void pushdown(int l,int r,int rt)
{
    int mid=(l+r)>>1;
    tree[ls].sum=tree[ls].rsum=tree[ls].lsum=tree[rt].f*(mid-l+1);
    tree[rs].sum=tree[rs].rsum=tree[rs].lsum=tree[rt].f*(r-mid);
    tree[ls].f=tree[rs].f=tree[rt].f;
    tree[rt].f=-1;
}

void update(int l,int r,int rt,int L,int R,int val)
{
    if (l>=L && r<=R){
        tree[rt].lsum=tree[rt].rsum=tree[rt].sum=(r-l+1)*val;
        tree[rt].f=val;
        return;
    }
    if (tree[rt].f!=-1) pushdown(l,r,rt);
    int mid=(l+r)>>1;
    if (mid>=L) update(lson,L,R,val);
    if (mid<R) update(rson,L,R,val);
    pushup(l,r,rt);
}

int query(int l,int r,int rt,int siz)
{
    if (l==r && siz==1) return l; 
    int mid=(l+r)>>1;
    if (tree[rt].f!=-1) pushdown(l,r,rt);
    if (tree[rt].sum>=siz){
    	if (tree[ls].sum>=siz) return query(lson,siz);
    	else if (tree[ls].rsum+tree[rs].lsum>=siz) return mid-tree[ls].rsum+1;
		else return query(rson,siz); 
	}
	return 0;
}

int main()
{
    //freopen("in.txt","r",stdin);
	int n,m;
    scanf ("%d%d",&n,&m);
    build(1,n,1);
    for (int i=1; i<=m; i++){
        int opt,x,d;
        scanf ("%d",&opt);
        if (opt==1){
            scanf ("%d",&d);
            int ans=query(1,n,1,d);
            printf ("%d\n",ans);
            if (!ans) continue;
            update(1,n,1,ans,ans+d-1,0);
        }
        else {
            scanf ("%d%d",&x,&d);
            update(1,n,1,x,x+d-1,1);
        }
    }
    return 0;
}

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值