POJ3667 Hotel【线段树 区间合并+区间覆盖】

Hotel

http://poj.org/problem?id=3667

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 21810   Accepted: 9513

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

Source

USACO 2008 February Gold

题意

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

题解

cover[i] 表示 i 控制的区间是否空闲,1表示全部空闲,0表示全部不空闲,-1表示部分空闲(即只有部分房间被分配)

sum[i]表示 i 控制的区间最大的连续空闲房间数

lsum[i] 表示 i 控制的区间中以左端点为起点的最大连续空闲房间数(从左向右数)

rsum[i] 表示 i 控制的区间中以右端点为起点的最大连续空闲房间数(从右向左数)

C++代码

#include
#include

using namespace std;

#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1

const int N=50100;

int sum[N<<2],lsum[N<<2],rsum[N<<2],cover[N<<2];

void build(int l,int r,int rt)
{
	sum[rt]=lsum[rt]=rsum[rt]=r-l+1;
	cover[rt]=1;//开始都空 
	if(l==r) return;
	int m=(l+r)>>1;
	build(ls);
	build(rs);
}

void pushup(int rt)
{
	sum[rt]=max(max(sum[rt<<1],sum[rt<<1|1]),rsum[rt<<1]+lsum[rt<<1|1]);
	lsum[rt]=lsum[rt<<1]+(cover[rt<<1]==1?lsum[rt<<1|1]:0);
	rsum[rt]=rsum[rt<<1|1]+(cover[rt<<1|1]==1?rsum[rt<<1]:0);
	if(cover[rt<<1]==-1||cover[rt<<1|1]==-1||cover[rt<<1]!=cover[rt<<1|1])
	  cover[rt]=-1;
	else
	  cover[rt]=cover[rt<<1];
}

void pushdown(int l,int r,int rt)
{
	if(cover[rt]!=-1)
	{
		int m=(l+r)>>1;
		cover[rt<<1]=cover[rt<<1|1]=cover[rt];
		sum[rt<<1]=lsum[rt<<1]=rsum[rt<<1]=(cover[rt]?(m-l+1):0);
		sum[rt<<1|1]=lsum[rt<<1|1]=rsum[rt<<1|1]=(cover[rt]?(r-m):0);
	}
}

int query(int w,int l,int r,int rt)
{
	if(l==r) return l;
	int m=(l+r)>>1,ans;
	pushdown(l,r,rt);
	if(w<=sum[rt<<1])
	  ans=query(w,ls);
	else if(w<=rsum[rt<<1]+lsum[rt<<1|1])
	  ans=m-rsum[rt<<1]+1;
	else
	  ans=query(w,rs);
	return ans;
};

void update(int L,int R,int C,int l,int r,int rt)
{
	if(L<=l&&r<=R)
	{
		cover[rt]=C;
		sum[rt]=lsum[rt]=rsum[rt]=cover[rt]?(r-l+1):0;
		return;
	}
	int m=(l+r)>>1;
	pushdown(l,r,rt);
	if(L<=m)
	  update(L,R,C,ls);
	if(R>m)
	  update(L,R,C,rs);
	pushup(rt);
}

int main()
{
	int n,q;
	while(~scanf("%d%d",&n,&q))
	{
		build(1,n,1);
		while(q--)
		{
			int op,a,b;
			scanf("%d",&op);
			if(op==1)
			{
				scanf("%d",&a);
				if(a>sum[1])
				  printf("%d\n",0);
				else
				{
					int ans=query(a,1,n,1);
					printf("%d\n",ans);
					update(ans,ans+a-1,0,1,n,1);
				} 
			}
			else
			{
				scanf("%d%d",&a,&b);
				update(a,a+b-1,1,1,n,1);
			}
		} 
	}
	return 0;
}
 

 

你可能感兴趣的:(数据结构)