nbut线段树专题P - 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 D(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.

和S题一样,是区间合并的基础题,刚学没几天还不是很熟。。。。

题意是这样的:如果一段长度为a的区间(空的),那么找出最左边的位置。如果找不到,输出0.


区间合并,在up的时候实现,节点上存三个信息,从左边开始数连续空格最大数目,从右边开始数连续空格最大数目,区间连续空格最大数目。


#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>

using namespace std;
const int maxn=60010;
struct node
{
int l_blank,r_blank,all_blank,l,r,add;
}tree[maxn<<2];

void pushup(int p)//向上更新时完成区间合并
{
int ll=tree[p<<1].r-tree[p<<1].l+1;
int rl=tree[p<<1|1].r-tree[p<<1|1].l+1;
tree[p].l_blank=tree[p<<1].l_blank;
if(tree[p<<1].l_blank==ll)
tree[p].l_blank+=tree[p<<1|1].l_blank;
tree[p].r_blank=tree[p<<1|1].r_blank;
if(tree[p<<1|1].r_blank==rl)
tree[p].r_blank+=tree[p<<1].r_blank;
tree[p].all_blank=max(tree[p<<1].r_blank+tree[p<<1|1].l_blank,max(tree[p<<1].all_blank,tree[p<<1|1].all_blank));
}

void pushdown(int p)
{
if(tree[p].add!=-1)
{
int m=tree[p].r-tree[p].l+1;
tree[p<<1].add=tree[p<<1|1].add=tree[p].add;
if(tree[p].add==1)//如果是1,说明全部入住,那么空格要全部清空
{
tree[p<<1].l_blank=0;
tree[p<<1].r_blank=0;
tree[p<<1].all_blank=0;
tree[p<<1|1].l_blank=0;
tree[p<<1|1].r_blank=0;
tree[p<<1|1].all_blank=0;
}
else //否则 ,搬出去,空格数全部恢复
{
tree[p<<1].l_blank=(m-(m>>1));
tree[p<<1].r_blank=(m-(m>>1));
tree[p<<1].all_blank=(m-(m>>1));
tree[p<<1|1].l_blank=(m>>1);
tree[p<<1|1].r_blank=(m>>1);
tree[p<<1|1].all_blank=(m>>1);
}
tree[p].add=-1;
}
}


void build(int p,int l,int r)//构建一颗空树,每个节点的空格就是它的区间长度,因为现在还没有人入住
{
tree[p].l=l;
tree[p].r=r;
tree[p].all_blank=(r-l+1);
tree[p].l_blank=(r-l+1);
tree[p].r_blank=(r-l+1);
tree[p].add=-1;
if(l==r)
return ;
int mid=(l+r)>>1;
build(p<<1,l,mid);
build(p<<1|1,mid+1,r);
}

void update(int p,int l,int r,int val)
{
if(l<=tree[p].l && r>=tree[p].r)//找到区间
{
if(val==1)
tree[p].l_blank=tree[p].r_blank=tree[p].all_blank=0;//入住,空格清空
else
tree[p].l_blank=tree[p].r_blank=tree[p].all_blank=tree[p].r-tree[p].l+1;//搬出,空格恢复
tree[p].add=val;
return ;
}
pushdown(p);//用到子区间,down
int mid=(tree[p].r+tree[p].l)>>1;
if(r<=mid)
update(p<<1,l,r,val);
else if(l>mid)
update(p<<1|1,l,r,val);
else
{
update(p<<1,l,mid,val);
update(p<<1|1,mid+1,r,val);
}
pushup(p);
}

int query(int p,int len)
{
if(tree[p].l == tree[p].r)
return tree[p].l;//向下查找直到到单节点,返回位置
pushdown(p);
int mid=(tree[p].r + tree[p].l)>>1;
if(tree[p<<1].all_blank >= len)//假如len长度的可以直接放在左边,那么是最好的,题目要求靠左
return query(p<<1,len);
else if(tree[p<<1].r_blank+tree[p<<1|1].l_blank>=len)//处在左节点的右边+右节点的左边,那么位置就是左节点从右开始数最后一个数的位置
return mid-tree[p<<1].r_blank+1;
else
return query(p<<1|1,len);//否则一定在右节点
}


int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
build(1,1,n);
for(int i=0;i<m;i++)
{
int x,y,z;
scanf("%d",&z);
if(z==1)
{
scanf("%d",&x);
if(x>tree[1].all_blank)
printf("0\n");
else
{
int a=query(1,x);
printf("%d\n",a);
update(1,a,a+x-1,1);
}
}
else
{
scanf("%d%d",&x,&y);
update(1,x,x+y-1,2);
}
}
}
return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值