题目
B - Hotel
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 rto 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
题意:
对于线段树有两种操作,
一是:1 x 表示酒店有x个人来入住,此时要你输出这批旅客所住房间房间号最小的一个,要求是每批旅客住的房间必须是挨着的,房间号尽量小
二是:
2 x y 表示旅客从酒店房间为x号的开始,共有y人离开酒店,其实就是 x,x+1,x+2 .....x+y-1的酒店都为空了
思路:
其实就是利用线段树区间修改,区间查询,其中查询的东西比较复杂,线段树保存的信息也比较多
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<vector>
#include<cmath>
#include<string>
#include<map>
#include<queue>
using namespace std;
using namespace std;
const int maxn=50080;
struct hotel{
int lmax,rmax,all_max;//lmax分别表示区间的从左开始的最长连续空房间数,rmax从右开始的连续空房间数,all_max表示这个区间的最长连续空房间数
int lazy;//线段树的懒惰标记
}a[maxn<<2];
void push_up(int l,int r,int root)
{
a[root].all_max=max(max(a[root<<1].all_max,a[root<<1|1].all_max),a[root<<1].rmax+a[root<<1|1].lmax);
a[root].lmax=a[root<<1].lmax;
a[root].rmax=a[root<<1|1].rmax;
if(a[root<<1].lmax==(l+r)/2-l+1)
{
a[root].lmax+=a[root<<1|1].lmax;
}
if(a[root<<1|1].rmax==r-(l+r)/2)
{
a[root].rmax+=a[root<<1].rmax;
}
}
void push_down(int root,int len)//ϳÁ±ê¼Ç
{
if(a[root].lazy!=-1)
{
a[root<<1].all_max=a[root<<1].lmax=a[root<<1].rmax=a[root].lazy*((len+1)/2);
a[root<<1|1].all_max=a[root<<1|1].lmax=a[root<<1|1].rmax=a[root].lazy*(len/2);
a[root<<1].lazy=a[root<<1|1].lazy=a[root].lazy;
a[root].lazy=-1;
}
}
void build(int l,int r,int root)
{
a[root].all_max=a[root].lmax=a[root].rmax=r-l+1;
a[root].lazy=-1;
if(l==r)
{
return ;
}
int mid=(l+r)>>1;
build(l,mid,root<<1);
build(mid+1,r,root<<1|1);
//push_up(l,r,root);
}
void update(int l,int r,int root,int la,int ra,int val)
{
if(l>=la&&r<=ra)
{
a[root].all_max=val*(r-l+1);
a[root].lmax=val*(r-l+1);
a[root].rmax=val*(r-l+1);
a[root].lazy=val;
return ;
}
push_down(root,r-l+1);
int mid=(l+r)>>1;
if(la<=mid)
{
update(l,mid,root<<1,la,ra,val);
}
if(ra>mid)
{
update(mid+1,r,root<<1|1,la,ra,val);
}
push_up(l,r,root);
}
int query(int l,int r,int root,int val)
{
int mid=(l+r)>>1;
push_down(root,r-l+1);
if(a[root].lmax>=val)
{
return l;
}
//if(l==r){
// return l;
//}
//push_down(root,r-l+1);
else if(a[root<<1].all_max>=val)
{
query(l,mid,root<<1,val);
}
else if(a[root<<1].rmax+a[root<<1|1].lmax>=val)
{
return mid-a[root<<1].rmax+1;
}
else{
query(mid+1,r,root<<1|1,val);
}
}
int main()
{
int n,m,a1,a2,a3,i,j;
scanf("%d%d",&n,&m);
build(1,n,1);
for(i=0;i<m;i++)
{
scanf("%d",&a1);
if(a1==1)
{
scanf("%d",&a2);
if(a[1].all_max<a2)
{
printf("0\n");
continue;
}
a3=query(1,n,1,a2);
printf("%d\n",a3);
update(1,n,1,a3,a3+a2-1,0);
}
else{
scanf("%d%d",&a2,&a3);
update(1,n,1,a2,a2+a3-1,1);
}
}
return 0;
}