POJ 3667 Hotel (线段树区间合并 )


Language:
Default

Hotel

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 12417   Accepted: 5346

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

Source

USACO 2008 February Gold

题意:一个旅馆有n件连续的房子,开始都为空,有m个操作,每一个操作,如果第一个数为1 ,下一个数为le,那么输出长度le的最左边的编号,并且把这le个房间住人(如果没有那么多空房输出0),如果第一个数为2 后面  le,ri,那么把le 到le+ri-1的房间清空

就是线段树区间合并,需要注意的一个问题我在代码中标出了:

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>

#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)
using namespace std;

#define N 50005

int n,m;

struct stud{
int le,ri;
int ls,rs,ms;
int cover;
}f[N*4];

void pushdown(int pos)
{
	if(f[pos].cover==-1) return ;

	f[L(pos)].ls=f[L(pos)].rs=f[L(pos)].ms=f[pos].cover ?0:f[L(pos)].ri-f[L(pos)].le+1;
	f[R(pos)].ls=f[R(pos)].rs=f[R(pos)].ms=f[pos].cover ?0:f[R(pos)].ri-f[R(pos)].le+1;

    f[L(pos)].cover=f[R(pos)].cover=f[pos].cover;
    f[pos].cover=-1;
    //唯一需要注意的一点就是把f[pos].cover更改为-1,因为如果不改,下次还会pushdown
//这里它已经把值pushdown了,下次不需要pushdown

}

void pushup(int pos)
{
	f[pos].ls=f[L(pos)].ls;
	f[pos].rs=f[R(pos)].rs;
	f[pos].ms=max(f[L(pos)].ms,f[R(pos)].ms);

	if(f[L(pos)].ls==f[L(pos)].ri-f[L(pos)].le+1)
		f[pos].ls+=f[R(pos)].ls;

	if(f[R(pos)].rs==f[R(pos)].ri-f[R(pos)].le+1)
		f[pos].rs+=f[L(pos)].rs;

	f[pos].ms=max(f[pos].ms,f[L(pos)].rs+f[R(pos)].ls);
}

void build(int pos,int le,int ri)
{
	f[pos].le=le;
	f[pos].ri=ri;
	f[pos].ls=f[pos].rs=f[pos].ms=ri-le+1;
	f[pos].cover=0;
	if(le==ri) return ;

	int mid=MID(le,ri);
	build(L(pos),le,mid);
	build(R(pos),mid+1,ri);

	pushup(pos);
}

void update(int pos,int le,int ri,int va)
{
	if(f[pos].le==le&&f[pos].ri==ri)
	{
		f[pos].ls=f[pos].rs=f[pos].ms=va? 0:ri-le+1;
		f[pos].cover=va;
		return ;
	}

    pushdown(pos);

    int mid=MID(f[pos].le,f[pos].ri);

    if(mid>=ri)
		update(L(pos),le,ri,va);
	else
		if(mid<le)
		update(R(pos),le,ri,va);
	else
	{
		update(L(pos),le,mid,va);
		update(R(pos),mid+1,ri,va);
	}

  pushup(pos);
}

int query(int pos,int le)
{
	if(f[pos].le==f[pos].ri)
		return f[pos].le;

	pushdown(pos);

	int mid=MID(f[pos].le,f[pos].ri);

	if(f[L(pos)].ms>=le)
		return query(L(pos),le);
	else
		if(f[L(pos)].rs+f[R(pos)].ls>=le)
		return mid-f[L(pos)].rs+1;

	return query(R(pos),le);

}

int main()
{
	int i,j,x,le,ri;
	while(~scanf("%d%d",&n,&m))
	{

		build(1,1,n);

		while(m--)
		{
			scanf("%d",&x);
			if(x==1)
			{
				scanf("%d",&le);
				if(f[1].ms<le)
				{
					printf("0\n");
				}
				else
				{
					int ans=query(1,le);
					printf("%d\n",ans);
					update(1,ans,ans+le-1,1);
				}
			}
           else
		   {
		   	scanf("%d%d",&le,&ri);
		   	update(1,le,le+ri-1,0);
		   }
		}
	}
    return 0;
}
时间: 2024-10-13 23:25:19

POJ 3667 Hotel (线段树区间合并 )的相关文章

POJ 3667 Hotel(线段树区间合并)

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 Stree

POJ 3667 Hotel ( 线段树区间合并 )

题目链接~~> 做题感悟:这题是接触线段树区间合并的第一题,做的很纠结. 解题思路: 注意线段树上节点代表的信息 : 每个节点需要维护 lc , rc , mc ,add ,见下图: add 为懒惰标记.假设 i 代表父亲节点编号,左儿子为  i * 2  ,右儿子为 i * 2  + 1 ,那么我们可以得到 : T [ i ] .lc 首先加上左儿子的左边的空格数,然后需要判断一下,如果左儿子的左节点占满了整个左区间时需要再加上右儿子的左边的空格数.同理 T [ i ] .rc 也可以这样得到

POJ 3667 Hotel 线段树 区间合并

题意: 1 输入a:询问是不是有连续长度为a的空房间,有的话住进最左边 2 输入a b:将[a,a+b-1]的房间清空 思路:记录区间中最长的空房间 线段树操作: update:区间替换 query:询问满足条件的最左端点 #include <cstdio> #include <iostream> #include <algorithm> #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1

POJ 3667(线段树区间合并)

http://poj.org/problem?id=3667 题意:两个操作 : 1 选出靠左的长度为a的区间. 2 把从 a到a+b的区间清空. 线段树区间合并+lazy // by caonima // hehe #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <cmath> using namespace std; co

poj 3667 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 v

POJ 3667 Hotel 【线段树 区间合并 + Lazy-tag】

Hotel Time Limit: 3000MS Memory Limit: 65536K 链接:POJ 3667   Description The cows are journeying north to ThunderBay in Canada to gain cultural enrichment and enjoy a vacation on the sunnyshores of Lake Superior. Bessie, ever the competent travel agen

线段树(区间合并) POJ 3667 Hotel

题目传送门 1 /* 2 题意:输入 1 a:询问是不是有连续长度为a的空房间,有的话住进最左边 3 输入 2 a b:将[a,a+b-1]的房间清空 4 线段树(区间合并):lsum[]统计从左端点起最长连续空房间数,rsum[]类似,sum[]统计区间最长连续的空房间数, 5 它有三种情况:1.纯粹是左端点起的房间数:2.纯粹是右端点的房间数:3.当从左(右)房间起都连续时,加上另一个子节点 6 从左(右)房间起的数,sum[]再求最大值更新维护.理解没错,表达能力不足 7 详细解释:htt

Poj 3667——hotel——————【线段树区间合并】

Hotel Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 13124   Accepted: 5664 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

POJ 3667 Hotel (初遇线段树区间合并)

题意: 有一个线段,从1到n,下面m个操作,操作分两个类型,以1开头的是查询操作,以2开头的是更新操作 1 w 表示在总区间内查询一个长度为w的可用区间并且要最靠左,能找到的话返回这个区间的左端点并占用了这个区间,找不到返回0 2 a len , 表示从单位a开始,清除一段长度为len的区间(将其变为可用,不被占用),不需要输出. 思路: 这是第一次遇到线段树区间合并的题目,写下感悟,还是对线段的更新和查询工作,但是查询的对象的性质已经不像单点那样,查询的是某个线段的最大可用区间是多少,还要一并