poj3667 Hotel 线段树(区间合并)

http://poj.org/problem?id=3667

Hotel
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 12174 Accepted: 5276

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.

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

题意:两个操作。1 a:询问是不是有连续长度为a的空房间,有的话住最左边。

2 a b:将[a,a+b-1]房间清空。

思路:经典的线段树区间合并。维护一个区间最大连续可用区间及其从左起从右起最大连续可用区间,并且lazy标记下这个区间是否全住还是清空,在查询的时候要注意顺序,从左到右找看有没有,,,具体见代码:

/**
 * @author neko01
 */
//#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <cstring>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#include <set>
#include <map>
using namespace std;
typedef long long LL;
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define clr(a) memset(a,0,sizeof a)
#define clr1(a) memset(a,-1,sizeof a)
#define dbg(a) printf("%d\n",a)
typedef pair<int,int> pp;
const double eps=1e-9;
const double pi=acos(-1.0);
const int INF=0x3f3f3f3f;
const LL inf=(((LL)1)<<61)+5;
const int N=50005;
struct node{
	int l,r;
	int col;   //初始为-1,0表示这个区间清空,1表示住满
	int lsum,rsum;  //记录最大左右各(从最左边最右边开始)连续区间
	int sum;   //记录该区间最大连续区间
}tree[N*4];
void build(int x,int l,int r)
{
	tree[x].l=l,tree[x].r=r;
	tree[x].col=-1;
	tree[x].sum=tree[x].lsum=tree[x].rsum=r-l+1;
	if(l==r) return;
	int mid=(l+r)>>1;
	build(x<<1,l,mid);
	build(x<<1|1,mid+1,r);
}
void push_up(int x)
{
	tree[x].lsum=tree[x<<1].lsum;
	tree[x].rsum=tree[x<<1|1].rsum;
	if(tree[x].lsum==tree[x<<1].r-tree[x<<1].l+1)
		tree[x].lsum+=tree[x<<1|1].lsum;
	if(tree[x].rsum==tree[x<<1|1].r-tree[x<<1|1].l+1)
		tree[x].rsum+=tree[x<<1].rsum;
	tree[x].sum=max3(tree[x<<1].rsum+tree[x<<1|1].lsum,tree[x<<1].sum,tree[x<<1|1].sum);
}
void push_down(int x)
{
	if(tree[x].col!=-1)
	{
		tree[x<<1].col=tree[x<<1|1].col=tree[x].col;
		tree[x<<1].lsum=tree[x<<1].rsum=tree[x<<1].sum=tree[x].col==0?tree[x<<1].r-tree[x<<1].l+1:0;
		tree[x<<1|1].lsum=tree[x<<1|1].rsum=tree[x<<1|1].sum=tree[x].col==0?tree[x<<1|1].r-tree[x<<1|1].l+1:0;
		tree[x].col=-1;
	}
}
int query(int x,int k)
{
	if(tree[x].l==tree[x].r) return tree[x].l;
	push_down(x);
	int mid=(tree[x].l+tree[x].r)>>1;
	if(tree[x<<1].sum>=k) return query(x<<1,k);
	else if(tree[x<<1].rsum+tree[x<<1|1].lsum>=k)
		return mid-tree[x<<1].rsum+1;
	else return query(x<<1|1,k);
}
void update(int x,int l,int r,int col)
{
	if(tree[x].l==l&&tree[x].r==r)
	{
		tree[x].col=col;
		tree[x].lsum=tree[x].rsum=tree[x].sum=col==0?r-l+1:0;
		return;
	}
	push_down(x);
	int mid=(tree[x].l+tree[x].r)>>1;
	if(r<=mid) update(x<<1,l,r,col);
	else if(l>mid) update(x<<1|1,l,r,col);
	else
	{
		update(x<<1,l,mid,col);
		update(x<<1|1,mid+1,r,col);
	}
	push_up(x);
}
int main()
{
	int n,m;
	scanf("%d%d",&n,&m);
	build(1,1,n);
	while(m--)
	{
		int x,u,v;
		scanf("%d",&x);
		if(x==1)
		{
			scanf("%d",&u);
			if(tree[1].sum<u)
			{
				puts("0");
				continue;
			}
			int pos=query(1,u);
			printf("%d\n",pos);
			update(1,pos,pos+u-1,1);
		}
		else
		{
			scanf("%d%d",&u,&v);
			update(1,u,u+v-1,0);
		}
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值