POJ3667 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 ≤ 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
Source
USACO 2008 February Gold
题意&思路
这道题要求维护两种操作:
- 找到最靠前的连续x个房间标记为已使用
- 从第x个房间开始把连续d个房间标记为未使用
容易想到维护连续最长区间的线段树,重点是query()函数的使用
Code
#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#define FA(i,s,t) for(int i=(s);i<=(t);i++)
#define FD(i,s,t) for(int i=(s);i>=(t);i--)
#define PRI pari<int,int>
#define LLD long long
using namespace std;
int getint()
{
int x=0,f=1; char ch=getchar();
while(ch<'0'||ch>'9') {if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9') {x=x*10+ch-'0';ch=getchar();}
return x*f;
}
const int INF=(int) 2e9;
const int MAXN=50000+50;
struct tnode
{
int l,r,ll,rl,tl,e;
tnode() {e=0;} //构造函数不能有返回值
}st[MAXN*4];
inline int max3(int a,int b,int c)
{
return max(a,max(b,c));
}
inline int t_mid(int t)
{
return (st[t].l+st[t].r)>>1;
}
inline int t_len(int t)
{
return st[t].r-st[t].l+1;
}
inline bool t_ins(int t,int l,int r)
{
return st[t].l>=l&&st[t].r<=r;
}
inline bool t_out(int t,int l,int r)
{
return st[t].l>r||st[t].r<l;
}
inline void t_pre(int t,int x)
{
st[t].tl=st[t].ll=st[t].rl=x==1? t_len(t):0;
}
#define ls (t<<1)
#define rs (t<<1|1)
void pushup(int t)
{
st[t].tl=max3(st[ls].rl+st[rs].ll,st[ls].tl,st[rs].tl);
st[t].ll=st[ls].ll==t_len(ls)?
t_len(ls)+st[rs].ll:st[ls].ll;
st[t].rl=st[rs].rl==t_len(rs)?
t_len(rs)+st[ls].rl:st[rs].rl;
}
void pushdown(int t)
{
if(st[t].e) //因为判断的条件分别是e=1和e=2,所以当e=0的时候不能修改
{
t_pre(ls,st[t].e);
t_pre(rs,st[t].e);
st[ls].e=st[rs].e=st[t].e;
st[t].e=0;
}
}
void build(int t,int l,int r)
{
st[t].l=l;
st[t].r=r;
st[t].tl=st[t].ll=st[t].rl=t_len(t);
if(l!=r)
{
build(ls,l,t_mid(t));
build(rs,t_mid(t)+1,r);
}
}
int query(int t,int x) //这里的思路是确定有解以后在进入query函数
{
if(st[t].l==st[t].r&&x==1) //叶子节点没有子树嘛-_-#
{
return st[t].l;
}
pushdown(t);
if(st[ls].tl>=x)
{
return query(ls,x);
}
else if(st[ls].rl+st[rs].ll>=x)//除了w==1的特殊情况,所有答案的出口都在这里
{
return st[ls].r-st[ls].rl+1;
}
else if(st[rs].tl>=x)
{
return query(rs,x);
}
return 0;
}
void cover(int t,int l,int r,int x)
{
if(t_out(t,l,r))
{
return;
}
if(t_ins(t,l,r))
{
st[t].e=x;
t_pre(t,x);
return;
}
pushdown(t);
cover(ls,l,r,x);
cover(rs,l,r,x);
pushup(t);
}
int n,m;
int main()
{
n=getint();m=getint();
build(1,1,n);
FA(i,1,m)
{
int op_n=getint();
if(op_n==1)
{
int x=getint();
if(st[1].tl<x)
{
printf("0\n");
continue;
}
int ans=query(1,x);
cover(1,ans,ans+x-1,2);
printf("%d\n",ans);
}
else
{
int d=getint(),x=getint();
cover(1,d,d+x-1,1);
}
}
return 0;
}