线段树--hotel usaco feb08gold

【Usaco Feb08 Gold】旅馆

Time Limit:10000MS  Memory Limit:65536K
Case Time Limit:1000MS

Description

奶牛们最近的旅游计划,是到苏必利尔湖畔,享受那里的湖光山色,以及明媚的阳光。作为整个旅游的策划者和负责人,贝茜选择在湖边的一家著名的旅馆住宿。这个巨大的旅馆一共有N (1 <= N <= 50,000)间客房,它们在同一层楼中顺次一字排开,在任何一个房间里,只需要拉开窗帘,就能见到波光粼粼的湖面。 

贝茜一行,以及其他慕名而来的旅游者,都是一批批地来到旅馆的服务台,希望能订到D_i (1 <= D_i <= N)间连续的房间。服务台的接待工作也很简单:如果存在r满足编号为r..r+D_i-1的房间均空着,他就将这一批顾客安排到这些房间入住;如果没有满足条件的r,他会道歉说没有足够的空房间,请顾客们另找一家宾馆。如果有多个满足条件的r,服务员会选择其中最小的一个。 

旅馆中的退房服务也是批量进行的。每一个退房请求由2个数字X_i、D_i描述,表示编号为X_i..X_i+D_i-1 (1 <= X_i <= N-D_i+1)房间中的客人全部离开。退房前,请求退掉的房间中的一些,甚至是所有,可能本来就无人入住。 

而你的工作,就是写一个程序,帮服务员为旅客安排房间。你的程序一共需要处理M (1 <= M < 50,000)个按输入次序到来的住店或退房的请求。第一个请求到来前,旅店中所有房间都是空闲的。

Input

* 第1行: 2个用空格隔开的整数:N、M 

* 第2..M+1行: 第i+1描述了第i个请求,如果它是一个订房请求,则用2个数字1、D_i描述,数字间用空格隔开;如果它是一个退房请求,用3个以空格隔开的数字2、X_i、D_i描述

Output

* 第1..??行: 对于每个订房请求,输出1个独占1行的数字:如果请求能被满足 ,输出满足条件的最小的r;如果请求无法被满足,输出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 feb2008 金组

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#define inf 1e9
using namespace std;
const int maxn=50005;
struct node{
	int a,b;
	int lm,rm,m;
	int sum;
	int lazy;
};
node tree[maxn*4];
int a[maxn];
int triplemax(int a,int b,int c){
	return max(a,max(b,c));
}
void build_tree(int p,int x,int y){
	tree[p].a=x;tree[p].b=y;
	tree[p].lazy=1;
	int mid=(x+y)>>1;
	if(x<y){
		build_tree((p<<1),x,mid);
		build_tree((p<<1)+1,mid+1,y);
		tree[p].lm=tree[p].rm=tree[p].m=tree[p].sum=y-x+1;
	}
	else
		tree[p].lm=tree[p].rm=tree[p].m=tree[p].sum=1;
}
void update(int p){
	int ls=(p<<1),rs=(p<<1)+1;
	if(tree[ls].m==tree[ls].sum)tree[p].lm=tree[ls].sum+tree[rs].lm;
	else tree[p].lm=tree[ls].lm;
	if(tree[rs].m==tree[rs].sum)tree[p].rm=tree[rs].sum+tree[ls].rm;
	else tree[p].rm=tree[rs].rm;
	tree[p].m=triplemax(tree[ls].m,tree[rs].m,tree[ls].rm+tree[rs].lm);
}
void putdown(int p){
	if(tree[p].a==tree[p].b)return;
	int ls=(p<<1),rs=(p<<1)+1;
	if(tree[p].lazy==1){
		tree[ls].lm=tree[ls].rm=tree[ls].m=tree[ls].sum;
		tree[rs].lm=tree[rs].rm=tree[rs].m=tree[rs].sum;
		tree[ls].lazy=1;
		tree[rs].lazy=1;
	}
	else if(tree[p].lazy==2){
		tree[ls].lm=tree[ls].rm=tree[ls].m=0;
		tree[rs].lm=tree[rs].rm=tree[rs].m=0;
		tree[ls].lazy=2;
		tree[rs].lazy=2;
	}
	tree[p].lazy=0;
}
void change(int p,int x,int y,int t){
	if(y<tree[p].a||tree[p].b<x)return;
	if(tree[p].lazy)putdown(p);
	int mid=(tree[p].a+tree[p].b)/2;
	if(x<=tree[p].a&&tree[p].b<=y){
		if(t==1)tree[p].lm=tree[p].rm=tree[p].m=tree[p].sum;
		else tree[p].lm=tree[p].rm=tree[p].m=0;
		tree[p].lazy=t;
		return;
	}
	if(tree[p].a==tree[p].b)return;
	change((p<<1),x,y,t);
	change((p<<1)+1,x,y,t);
	update(p);
}
int query(int p,int x){
	int mid=(tree[p].b+tree[p].a)/2;
	if(tree[p].lazy)putdown(p);
	if(tree[p].a==tree[p].b)return tree[p].a;
	if(tree[(p<<1)].m>=x)return query((p<<1),x);
	if(tree[(p<<1)].rm+tree[(p<<1)+1].lm>=x)return mid-tree[(p<<1)].rm+1;
	return query((p<<1)+1,x);
}
int main(){
	int n,m;
	scanf("%d%d",&n,&m);
	build_tree(1,1,n);
	while(m--){
		int k,x,y;
		scanf("%d",&k);
		if(k==1){
			scanf("%d",&x);
			if(tree[1].m<x){
				printf("0\n");continue;
			}
			int p=query(1,x);
			printf("%d\n",p);
			change(1,p,p+x-1,2);
		}
		else{
			scanf("%d%d",&x,&y);
			change(1,x,x+y-1,1);
		}
	}
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值