HDU-3667 Hetol 线段树 区间合并

题目链接

题目大意:一共有n个房间,初始全为空,会有两种操作,一种是询问是否有r个连续的房间,若有,则将最左端有r个连续的房间全部入住,若没有则输出0,另一种是将从u开始的d个位置退房。




#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<math.h>
#include<functional>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int maxn = 50500;
const int inf = 1<<30;//0x7f;
typedef __int64 LL;
int n,m;
struct node
{
	int cov,mmax,lmax,rmax;
}tree[maxn<<2];
int Max( int a,int b,int c )
{
	a = a>b?a:b;
	return a>c?a:c;
}
void Pushup( int rt,int m )
{
	tree[rt].lmax = tree[rt<<1].lmax;
	tree[rt].rmax = tree[rt<<1|1].rmax;
	if( tree[rt<<1].lmax == m-(m>>1) )	tree[rt].lmax += tree[rt<<1|1].lmax;
	if( tree[rt<<1|1].rmax == (m>>1) )	tree[rt].rmax += tree[rt<<1].rmax;
	tree[rt].mmax = Max( tree[rt<<1].mmax,tree[rt<<1|1].mmax,tree[rt<<1].rmax + tree[rt<<1|1].lmax );
}
void Pushdown( int rt,int m )
{
	if( tree[rt].cov != -1 )
	{
		tree[rt<<1].cov = tree[rt<<1|1].cov = tree[rt].cov;
		tree[rt<<1].mmax = tree[rt<<1].lmax = tree[rt<<1].rmax = tree[rt].cov?0:m-(m>>1);
		tree[rt<<1|1].mmax = tree[rt<<1|1].lmax = tree[rt<<1|1].rmax = tree[rt].cov?0:(m>>1);
		tree[rt].cov = -1;
	}
}
void buildtree( int rt,int ld,int rd )
{
	tree[rt].cov = -1;
	tree[rt].mmax = tree[rt].lmax = tree[rt].rmax = rd-ld+1;
	if( ld < rd ){
		int mid = (ld+rd)>>1;
		buildtree( rt<<1,ld,mid );
		buildtree( rt<<1|1,mid+1,rd );
	}
}
int query( int rt,int ld,int rd,int D )
{
	if( ld == rd )	return ld;
	Pushdown( rt,rd-ld+1 );
	int mid = (ld+rd)>>1;
	if( tree[rt<<1].mmax >= D )	return query( rt<<1,ld,mid,D );
	else if( tree[rt<<1].rmax + tree[rt<<1|1].lmax >= D )	return mid-tree[rt<<1].rmax+1;
	return query( rt<<1|1,mid+1,rd,D );
}
void updata( int rt,int ld,int rd,int x,int y,int c )
{
	if( x <= ld && rd <= y ){
		tree[rt].mmax = tree[rt].lmax = tree[rt].rmax = c?0:rd-ld+1;
		tree[rt].cov = c;
		return;
	}
	Pushdown( rt,rd-ld+1 );
	int mid = (ld+rd)>>1;
	if( x <= mid )	updata( rt<<1,ld,mid,x,y,c );
	if( mid < y )	updata( rt<<1|1,mid+1,rd,x,y,c );
	Pushup( rt,rd-ld+1 );

}
int main() 
{
	#ifndef ONLINE_JUDGE
    freopen("data.txt","r",stdin);
	#endif
	int op,x,d;
	while( scanf("%d%d",&n,&m) != EOF )
	{
		buildtree( 1,1,n );
		for( int i = 0; i < m; i ++ )
		{
			scanf("%d",&op);
			if( op == 1 ){
				scanf("%d",&d);
				if( tree[1].mmax < d )	puts("0");
				else
				{
					x = query( 1,1,n,d );
					printf("%d\n",x);
					updata( 1,1,n,x,x+d-1,1 );
				}
			}
			else{
				scanf("%d%d",&x,&d);
				updata( 1,1,n,x,x+d-1,0 );
			}
		}
	}
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值