CD
题意:有N个客房,随时来K个人,必须要有连续K个房他们才入住。
另一个操作便是清空一段客房的房客。
思路:用线段树t[left,right]来维护每一段的lmax,smax,rmax(左连续空,右连续空,所有连续的最大空房)。
pushDown,,pushUp,见程序
#include<iostream>
#include<cstdio>
using namespace std;
#define N 51000
struct tree
{
int left,right;
int len;
int mid;
int lmax,smax,rmax;
}t[4*N];
int n,q;
void build(int cur,int l,int r)
{
t[cur].left=l;
t[cur].right=r;
t[cur].mid=(l+r)>>1;
t[cur].lmax=t[cur].rmax=t[cur].smax=t[cur].len=r-l+1;
if (l!=r)
{
build(cur<<1,l,t[cur].mid);
build(cur<<1|1,t[cur].mid+1,r);
}
}
void add(int cur,int l,int r)
{
// cout<<cur<<' '<<t[cur].left<<' '<<t[cur].right
// <<' '<<t[cur].lmax<<' '<<t[cur].smax<<' '<<t[cur].right<<endl;
if (l<=t[cur].left && r>=t[cur].right)
{
t[cur].lmax=t[cur].rmax=t[cur].smax=0;
return ;
}
int L=cur<<1,R=cur<<1|1;
if (t[cur].smax==0 )
{
t[L].smax=t[L].lmax=t[L].rmax=0;
t[R].smax=t[R].lmax=t[R].rmax=0;
}
if (t[cur].smax==t[cur].len)
{
t[L].smax=t[L].lmax=t[L].rmax=t[L].right-t[L].left+1;
t[R].smax=t[R].lmax=t[R].rmax=t[R].right-t[R].left+1;
}
int m=t[cur].mid;
if (r<=m)
add(L,l,r);
else if (l>m)
add(R,l,r);
else
{
add(L,l,m);
add(R,m+1,r);
}
if (t[L].len==t[L].lmax)
t[cur].lmax=t[L].lmax+t[R].lmax;
else
t[cur].lmax=t[L].lmax;
if (t[R].len==t[R].rmax)
t[cur].rmax=t[R].rmax+t[L].rmax;
else
t[cur].rmax=t[R].rmax;
t[cur].smax=max(max(t[L].smax,t[R].smax),t[L].rmax+t[R].lmax);
}
void remove(int cur,int l,int r)
{
if (l<=t[cur].left && r>=t[cur].right)
{
t[cur].lmax=t[cur].rmax=t[cur].smax=r-l+1;
return ;
}
int L=cur<<1,R=cur<<1|1;
if (t[cur].smax==0 )
{
t[L].smax=t[L].lmax=t[L].rmax=0;
t[R].smax=t[R].lmax=t[R].rmax=0;
}
if (t[cur].smax==(t[cur].right-t[cur].left+1))
{
t[L].smax=t[L].lmax=t[L].rmax=t[L].right-t[L].left+1;
t[R].smax=t[R].lmax=t[R].rmax=t[R].right-t[R].left+1;
}
int m=t[cur].mid;
if (r<=m)
remove(L,l,r);
else if (l>m)
remove(R,l,r);
else
{
remove(L,l,m);
remove(R,m+1,r);
}
if (t[L].len==t[L].lmax)
t[cur].lmax=t[L].lmax+t[R].lmax;
else
t[cur].lmax=t[L].lmax;
if (t[R].len==t[R].rmax)
t[cur].rmax=t[R].rmax+t[L].rmax;
else
t[cur].rmax=t[R].rmax;
t[cur].smax=max(max(t[L].smax,t[R].smax),t[L].rmax+t[R].lmax);
}
int query(int cur,int len)
{
// cout<<cur<<' '<<t[cur].left<<' '<<t[cur].right
// <<' '<<t[cur].lmax<<' '<<t[cur].smax<<' '<<t[cur].rmax<<endl;
if (t[cur].smax<len)
return 0;
int L=cur<<1,R=cur<<1|1;
if (t[cur].smax==0 )
{
// cout<<"good";
t[L].smax=t[L].lmax=t[L].rmax=0;
t[R].smax=t[R].lmax=t[R].rmax=0;
}
if (t[cur].smax==(t[cur].right-t[cur].left+1))
{
t[L].smax=t[L].lmax=t[L].rmax=t[L].len;
t[R].smax=t[R].lmax=t[R].rmax=t[R].len;
}
// cout<<t[L].rmax<<' '<<t[R].lmax<<' '<<t[R].smax<<endl;
// cout<<t[L].lmax<<' '<<t[L].smax<<' '<<t[L].rmax+t[R].lmax<<' '<<t[R].smax<<endl;
if (t[L].lmax>=len)
return t[L].left;
else if (t[L].smax>=len)
return query(L,len);
else if (t[L].rmax+t[R].lmax>=len)
return t[cur].mid-t[L].rmax+1;
else if (t[R].smax>=len)
return query(R,len);
return 0;
}
int main()
{
freopen("in.txt","r",stdin);
int i,j,k;
cin>>n>>q;
int cur;
build(1,1,n);
while (q--)
{
scanf("%d",&cur);
if (cur==1)
{
scanf("%d",&i);
k=query(1,i);
printf("%d\n",k);
if (k)
add(1,k,k+i-1);
}
else
{
scanf("%d%d",&i,&j);
remove(1,i,j+i-1);
}
}
return 0;
}
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