POJ 3667 Hotel
Time Limit:3000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu
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 解题思路: 题目说,有N间房子,M次操作,操作1来了一堆牛,让你安排连续的房间给他们,没有输出0 否则输出开始的位置,操作2房间x开始后面d个房间清空。 1, 线段树维护区间的题目,感觉很套路,解题很直接 2,操作一就是先寻找位置,确定安排的区间范围,然后update,没找到输出0就ok了。 3,操作二就是这个区间房子都空出来,也就是update设为0。 4,线段树的作用就是维护区间长度。 5,线段树区间维护这一块很好理解的,那几个数组的意义中文也注释了。 6,这一块我的博客里面没有很详细的解释,就是那么合并的嘛,没啥好说的
#include
#include
#include
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int maxn = 500005 ;
int tlen[maxn<<2] ;///tlen表示该区间可用的区间的最大长度
int rlen[maxn<<2] ;///rlen表示一个区间从最右端开始可用的且连续的最大长度
int llen[maxn<<2] ;///llen表示一个区间从最左端开始可用的且连续的最大长度
int setv[maxn<<2] ;
void pushdown(int l,int r,int rt){
if(setv[rt]!=-1){
setv[rt<<1] = setv[rt] ;
setv[rt<<1|1] = setv[rt] ;
int m = (l+r)>>1 ;
tlen[rt<<1] = llen[rt<<1] = rlen[rt<<1] = setv[rt]*(m-l+1) ;
tlen[rt<<1|1] = llen[rt<<1|1] = rlen[rt<<1|1] = setv[rt]*(r-m) ;
setv[rt] = -1 ;
}
return ;
}
void pushup(int l,int r,int rt){
int m = (l+r)>>1 ;
// (llen[rt<<1]==(m-l+1))?(llen[rt] = llen[rt<<1]+llen[rt<<1|1] ): (llen[rt] = llen[rt<<1]) ;
// (rlen[rt<<1|1]==(r-m))?(rlen[rt] = rlen[rt<<1|1]+rlen[rt<<1] ): (rlen[rt] = rlen[rt<<1|1]) ;
if(llen[rt<<1]==(m-l+1))llen[rt] = llen[rt<<1]+llen[rt<<1|1];
else llen[rt] = llen[rt<<1] ;
if(rlen[rt<<1|1]==(r-m))rlen[rt] = rlen[rt<<1|1]+rlen[rt<<1] ;
else rlen[rt] = rlen[rt<<1|1] ;
tlen[rt] = max(tlen[rt<<1],tlen[rt<<1|1]);
tlen[rt] = max(tlen[rt],rlen[rt<<1]+llen[rt<<1|1]);
return ;
}
void build(int l,int r,int rt){
int m = (l+r)>>1 ;
if(l==r){
tlen[rt] = 1 ;
llen[rt] = 1 ;
rlen[rt] = 1 ;
return ;
}
build(lson) ;
build(rson) ;
pushup(l,r,rt) ;
}
int query(int p,int l,int r,int rt ){///找p个连续座位
if(l==r){
return l;
}
int m = (r+l)>>1 ;
pushdown(l,r,rt) ;
if(tlen[rt]>=p){
if(tlen[rt<<1]>=p){
return query(p,lson) ;
}else if(rlen[rt<<1]+llen[rt<<1|1]>=p){//&&rlen[rt<<1]>0){
//return query(min(p,rlen[rt<<1]),lson) ;
return m-rlen[rt<<1]+1 ;
}else{
return query(p,rson) ;
}
//return 0;
}else{
return 0;
}
}
void update(int L,int R,int s,int l,int r,int rt){
if(L<=l&&r<=R){
setv[rt] = s ;
tlen[rt] = llen[rt] = rlen[rt] = s*(r-l+1) ;
return ;
}
pushdown(l,r,rt) ;
int m = (l+r)>>1 ;
if(L<=m)update(L,R,s,lson) ;
if(R>m)update(L,R,s,rson) ;
pushup(l,r,rt) ;
}
int main(){
int n,m;
// freopen("in.txt","r",stdin) ;
// freopen("out2.txt","w",stdout) ;
while(~scanf("%d%d",&n,&m)){
memset(tlen,0,sizeof(tlen)) ;
memset(rlen,0,sizeof(rlen)) ;
memset(llen,0,sizeof(llen)) ;
memset(setv,-1,sizeof(setv)) ;
int op = 0 ;
build(1,n,1) ;
for(int i=0;itlen[1]){
puts("0") ;
continue ;
}
int index = query(w,1,n,1) ;
printf("%d\n",index);
if(index!=0){
update(index,index+w-1,0,1,n,1) ;
}
}else{
int l,len ;
scanf("%d%d",&l,&len);
update(l,l+len-1,1,1,n,1) ;
}
}
}
return 0;
}