【线段树】Hotel POJ3667

Hotel
Time Limit: 3000MS Memory Limit: 65536K

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




很经典的线段树模式

在每个节点上新加3个域Z,Y,S


Z表示区间左边连续的空房个数

Y表示区间有边连续的空房个数

S表示区间上最大的一段空房的长度。



那么:

z[p]=z[p<<1](左区间不都为空时)
z[p]=z[p<<1]+z[(p<<1)+1](左区间为空时)
y[p]类似。
s[p]=max{ s[p<<1] , s[(p<<1)+1] , y[p<<1]+z[(p<<1)+1] }

查询时:


        1、如果1~n区间的S值都比X小,就无解,否则有解。


        2、对于每一个区间,如果它的左儿子的S值大于等于X,到左儿子里去找。


        3、如果左儿子的Y加上有儿子的Z大于等于X,直接返回左儿子的右端点减去左儿子的Y值。


        4、否则到右儿子里去找。


        5、如果满足2,则不再考虑3,4;同样,满足3就不再考虑4





总体写下来处理方法有点像小白逛公园那一题(当时还不知道怎么写)

这下有思路了



测评情况(cena,POJ)





C++ AC Code

/*http://blog.csdn.net/jiangzh7
By jiangzh*/
#include<cstdio>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
const int N=50000+10;
const int inf=0x3f3f3f3f;
int n,m;
int val[N*4];
int z[N*4],y[N*4],s[N*4];

inline void downloading(int p,int l,int r,int m)
{
	if(val[p]!=-1)
	{
		if(val[p]==1)
		{
			z[p<<1]=y[p<<1]=s[p<<1]=0;
			z[(p<<1)+1]=y[(p<<1)+1]=s[(p<<1)+1]=0;
		}
		if(val[p]==0)
		{
			z[p<<1]=y[p<<1]=s[p<<1]=m-l+1;
			z[(p<<1)+1]=y[(p<<1)+1]=s[(p<<1)+1]=r-(m+1)+1;
		}
		val[p<<1]=val[(p<<1)+1]=val[p];
		val[p]=-1;
	}
}
inline void update(int p)
{
	if(val[p]==-1&&val[p<<1]==val[(p<<1)+1])
	    val[p]=val[p<<1];
}

void change(int p,int l,int r,int a,int b,int c)
{
	if(a<=l&&b>=r)
	{
		val[p]=c;
		z[p]=y[p]=s[p]=(1-c)*(r-l+1);//简写了,仔细看就明白了
		return;
	}
	int m=(l+r)>>1;
	downloading(p,l,r,m);
	if(a<=m) change(p<<1,l,m,a,b,c);
	if(b>m) change((p<<1)+1,m+1,r,a,b,c);
	//z[p]
	if(val[p<<1]==0) z[p]=z[p<<1]+z[(p<<1)+1];
	else z[p]=z[p<<1];
	//y[p]
	if(val[(p<<1)+1]==0) y[p]=y[(p<<1)+1]+y[p<<1];
	else y[p]=y[(p<<1)+1];
	//s[p]
	s[p]=max(y[p<<1]+z[(p<<1)+1],max(s[p<<1],s[(p<<1)+1]));
	update(p);
}

int getres(int d)//写成非递归了
{
	if(s[1]<d) return 0;
	int p=1,l=1,r=n;
	while(l<r)
	{
		int m=(l+r)>>1;
		downloading(p,l,r,m);
		if(s[p<<1]>=d){p<<=1;r=m;continue;}
		if(y[p<<1]+z[(p<<1)+1]>=d) return m-y[p<<1]+1;
		p<<=1;p++;l=m+1;
		update(p);
	}
	return l;
}

void order(int d)
{
	int res=getres(d);
	printf("%d\n",res);
	if(res) change(1,1,n,res,res+d-1,1);
}

int main()
{
    freopen("hotel.in","r",stdin);
    freopen("hotel.out","w",stdout);
    scanf("%d%d",&n,&m);
    change(1,1,n,1,n,0);//让所有人离开,相当于给z[],y[],s[]赋初值
    int temp,x,d;
    for(int i=1;i<=m;i++)
    {
        scanf("%d",&temp);
        if(temp==1)//order
        {
            scanf("%d",&d);
            order(d);
        }
        else if(temp==2)//leave
        {
            scanf("%d%d",&x,&d);
            change(1,1,n,x,x+d-1,0);
        }
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值