题目描述:
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.
输入:输入n,m表示房间数和询问次数,接下来的询问如果是类型1,就查询是否存在连续的a个空房间,有就输出最靠前的房间的位置,没有就输出0,类型2的话就从位置a的房间开始把连续b个房间清空。数据范围题目描述中都给出了;线段树的区间更新,要用到lazy-tag,也是线段树必须会的基础操作之一;
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<algorithm>
using namespace std;
const int MAXM=4*50001;
struct node
{
int l,r;
int ls,rs,ms;
int tag;
};
struct node tree[MAXM];
int n,m;
void pushup(int num)
{
tree[num].ls=tree[num*2].ls;
tree[num].rs=tree[num*2+1].rs;
tree[num].ms=max(tree[num*2].ms,tree[num*2+1].ms);
if (tree[num*2].ls==tree[num*2].r-tree[num*2].l+1)
tree[num].ls+=tree[num*2+1].ls;
if (tree[num*2+1].rs==tree[num*2+1].r-tree[num*2+1].l+1)
tree[num].rs+=tree[num*2].rs;
tree[num].ms=max(tree[num*2].rs+tree[num*2+1].ls,tree[num].ms);
}
void build_tree(int l,int r,int num)
{
tree[num].l=l;
tree[num].r=r;
tree[num].ls=tree[num].rs=tree[num].ms=r-l+1;
tree[num].tag=0;
if (l==r) return ;
int mid=(l+r)/2;
build_tree(l,mid,num*2);
build_tree(mid+1,r,num*2+1);
pushup(num);
}
void pushdown(int num)
{
if (tree[num].tag==-1) return ;
if (tree[num].tag==1)
{
tree[num*2].ls=tree[num*2].rs=tree[num*2].ms=0;
tree[num*2+1].ls=tree[num*2+1].rs=tree[num*2+1].ms=0;
}
else if (tree[num].tag==0)
{
tree[num*2].ls=tree[num*2].rs=tree[num*2].ms=tree[num*2].r-tree[num*2].l+1;
tree[num*2+1].ls=tree[num*2+1].rs=tree[num*2+1].ms=tree[num*2+1].r-tree[num*2+1].l+1;
}
tree[num*2].tag=tree[num*2+1].tag=tree[num].tag;
tree[num].tag=-1;
}
int query(int a,int num)
{
if (tree[num].l==tree[num].r) return tree[num].l;
pushdown(num);
int mid=(tree[num].l+tree[num].r)/2;
if (tree[num*2].ms>=a) return query(a,num*2);
else if (tree[num*2].rs+tree[num*2+1].ls>=a) return mid-tree[num*2].rs+1;
return query(a,num*2+1);
}
void update(int va,int l,int r,int num)
{
if (tree[num].l==l&&tree[num].r==r)
{
if (va==1)
{
tree[num].ls=tree[num].rs=tree[num].ms=0;
tree[num].tag=va;
}
else if(va==0)
{
tree[num].ls=tree[num].rs=tree[num].ms=r-l+1;
tree[num].tag=va;
}
return ;
}
pushdown(num);
int mid=(tree[num].l+tree[num].r)/2;
if (l>mid)
update(va,l,r,num*2+1);
else if (mid>=r) update(va,l,r,num*2);
else
{
update(va,l,mid,num*2);
update(va,mid+1,r,num*2+1);
}
pushup(num);
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
build_tree(1,n,1);
while(m--)
{
int x;
scanf("%d",&x);
if (x==1)
{
int a;
scanf("%d",&a);
int ans=0;
if (a>tree[1].ms) printf("0\n");
else
{
ans=query(a,1);
printf("%d\n",ans);
update(1,ans,ans+a-1,1);
}
}
else if (x==2)
{
int a,b;
scanf("%d%d",&a,&b);
update(0,a,a+b-1,1);
}
}
}
return 0;
}