向前走莫回头❤

【POJ 3667】Hotel(线段树)

Hotel
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 16521   Accepted: 7167

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

[Submit]   [Go Back]   [Status]   [Discuss]

Home Page   Go Back  To top

【题解】【线段树】
维护三个值最靠左的最长空区间、最靠右的最长空区间和最长空区间】 
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct tree{
	int len,ll,rl;
}sum[200010];
int delta[200010],n,m;
inline void update(int now)
{
	sum[now].ll=sum[now].rl=sum[now].len=sum[(now<<1)].len+sum[(now<<1)|1].len;
}
inline void updata(int now,int mid,int l,int r)
{
  sum[now].len=max(max(sum[(now<<1)].len,sum[(now<<1)|1].len),sum[(now<<1)].rl+sum[(now<<1)|1].ll);//在左子树最长空区间、右子树最长空区间、和左子树最右最长空区间加上右子树最最右长空区间中选最大的 
  if (sum[(now<<1)].ll==mid-l+1)//如果左子树最左最长空区间是整个左子树区间,即能和右子树连上 
    sum[now].ll=sum[(now<<1)].ll+sum[(now<<1)|1].ll;//就将此节点的最左最长空区间存为两个最左最长空区间的和 
   else  sum[now].ll=sum[(now<<1)].ll;//否则存为左子树的最左最长空区间 
  if (sum[(now<<1)|1].rl==r-mid)	//最右最长空区间同理 
    sum[now].rl=sum[(now<<1)|1].rl+sum[(now<<1)].rl;
   else sum[now].rl=sum[(now<<1)|1].rl;
}  
inline void pushdown(int now,int mid,int l,int r)
{
	if (delta[now])
	  {
	  	if (!sum[now].len)//如果没有空区间 
	  	  {
	  	  	sum[(now<<1)].len=sum[(now<<1)].ll=sum[(now<<1)].rl=0;
	  	    sum[(now<<1)|1].len=sum[(now<<1)|1].ll=sum[(now<<1)|1].rl=0;
	  	    delta[(now<<1)]=1; delta[(now<<1)|1]=1; delta[now]=0;
			} 
		 else//如果有空区间,更新为整个区间 
		  {
		  	sum[(now<<1)].len=sum[(now<<1)].ll=sum[(now<<1)].rl=mid-l+1;
	  	    sum[(now<<1)|1].len=sum[(now<<1)|1].ll=sum[(now<<1)|1].rl=r-mid;
	  	    delta[(now<<1)]=1; delta[(now<<1)|1]=1; delta[now]=0;
		  }
	  }
}
inline void build(int now,int l,int r)
{
	if (l==r)
	 {sum[now].len=sum[now].ll=sum[now].rl=1; return;}
	int mid=(l+r)>>1;
	build((now<<1),l,mid);
	build((now<<1)|1,mid+1,r);
	update(now);
}
inline void change(int now,int al,int ar,int l,int r,int v)
{
	if (al<=l&&r<=ar)
	 {
	 	if (v)
	 	  sum[now].len=sum[now].ll=sum[now].rl=r-l+1;
	 	 else
	 	  sum[now].len=sum[now].ll=sum[now].rl=0;
	 	delta[now]=1;
	 	return;
	 }
	int mid=(l+r)>>1;
	pushdown(now,mid,l,r);
	if (al<=mid) change((now<<1),al,ar,l,mid,v);
	if (ar>mid) change((now<<1)|1,al,ar,mid+1,r,v);
	updata(now,mid,l,r);
}
inline int find(int now,int l,int r,int v)
{
	if (l==r) return l;
	int mid=(l+r)>>1;
	pushdown(now,mid,l,r);
	if (sum[(now<<1)].len>=v)//如果左子树的总空区间长度比待查入区间大,则在左子树里找 
	    return find((now<<1),l,mid,v);
	 else
	  if (sum[(now<<1)].rl+sum[(now<<1)|1].ll>=v)//如果左子树的最左最长空区间和最右最长空区间的和大于待查入区间,即左区间的末尾加上右区间的开头能构成合法方案,就从左区间中选择尽量多的范围 
	     return mid-sum[(now<<1)].rl+1;
	    else return find((now<<1)|1,mid+1,r,v);//再否则从右子树中找 
}
int main()
{
	int i,j;
	scanf("%d%d",&n,&m);
	build(1,1,n);
	for (i=1;i<=m;++i)
	 {
	 	int t;
	 	scanf("%d",&t);
	 	if (t==1)
	 	 {
	 	 	int x,ans;
			scanf("%d",&x);
			if (sum[1].len<x) //先判断这样的区间在整棵线段树中是否存在 
			  {printf("0\n"); continue;}
			ans=find(1,1,n,x);
			printf("%d\n",ans);
			change(1,ans,ans+x-1,1,n,0);
		  }
		 else
		  {
		  	int x,y;
		  	scanf("%d%d",&x,&y);
		  	change(1,x,x+y-1,1,n,1);
		  }
	 }
	return 0;
}


posted @ 2016-11-16 17:24  lris0-0  阅读(63)  评论(0编辑  收藏  举报
过去的终会化为美满的财富~o( =∩ω∩= )m