题目大意:一共有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;
}