(简单) 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 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 ≤ XiN-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.

  题目大致就是说对一段区间进行两种操作,分别是找到能够容下D的最左边的位置,然后就是更新一段区间为0或1。

  很典型的区间覆盖问题,维护最大空房间和前缀最大,后缀最大三个值。

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring> #define lson L,M,po*2
#define rson M+1,R,po*2+1
#define lc po*2
#define rc po*2+1 using namespace std; const int maxn=; int msum[maxn*],lsum[maxn*],rsum[maxn*];
int COL[maxn*]; void pushDown(int po,int len)
{
if(COL[po]==)
{
COL[lc]=COL[rc]=;
msum[lc]=rsum[lc]=lsum[lc]=;
msum[rc]=rsum[rc]=lsum[rc]=; COL[po]=-;
}
else if(COL[po]==)
{
COL[lc]=COL[rc]=;
msum[lc]=rsum[lc]=lsum[lc]=len-(len/);
msum[rc]=rsum[rc]=lsum[rc]=len/; COL[po]=-;
}
} void pushUP(int po,int len)
{
msum[po]=max(msum[lc],msum[rc]);
msum[po]=max(msum[po],rsum[lc]+lsum[rc]); lsum[po]=lsum[lc];
if(lsum[lc]==(len-(len/)))
lsum[po]+=lsum[rc]; rsum[po]=rsum[rc];
if(rsum[rc]==len/)
rsum[po]+=rsum[lc];
} void build_tree(int L,int R,int po)
{
COL[po]=-;
msum[po]=rsum[po]=lsum[po]=R-L+; if(L==R)
return; int M=(L+R)/; build_tree(lson);
build_tree(rson);
} void update(int ul,int ur,int ut,int L,int R,int po)
{
if(ul<=L&&ur>=R)
{
COL[po]=ut;
msum[po]=lsum[po]=rsum[po]=(ut ? : R-L+); return;
} pushDown(po,R-L+); int M=(L+R)/; if(ul<=M)
update(ul,ur,ut,lson);
if(ur>M)
update(ul,ur,ut,rson); pushUP(po,R-L+);
} int query(int len,int L,int R,int po)
{
if(L==R)
return L; pushDown(po,R-L+); int M=(L+R)/; if(msum[lc]>=len)
return query(len,lson);
else if(rsum[lc]+lsum[rc]>=len)
return M-rsum[lc]+;
else
return query(len,rson);
} int main()
{
int M,N;
int a,b,c; while(~scanf("%d %d",&N,&M))
{
build_tree(,N,); for(int i=;i<M;++i)
{
scanf("%d %d",&a,&b); if(a==)
{
if(msum[]<b)
printf("%d\n",);
else
{
c=query(b,,N,);
update(c,c+b-,,,N,);
printf("%d\n",c);
}
}
else
{
scanf("%d",&c);
update(b,b+c-,,,N,);
}
}
} return ;
}
上一篇:关于spring的源码的理解


下一篇:mysql in 查询优化