# tjut 2871

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define lson 2*i
#define rson 2*i+1
#define lc l,mid,2*i
#define rc mid+1,r,2*i+1
const int L = 50000+10;

struct node
{
int start,end;
int ls,rs,ms;
int lazy,cnt,cover;
} a[L<<2];

void pushdown(int mid,int L,int R,int i)
{
if(a[i].lazy!=-1)
{
a[lson].lazy = a[rson].lazy = a[i].lazy;
a[lson].ms = a[lson].ls = a[lson].rs = (mid-L+1)*a[i].lazy;
a[rson].ms = a[rson].ls = a[rson].rs = (R-mid)*a[i].lazy;
a[lson].start = a[rson].start = a[i].start;
a[lson].end = a[rson].end = a[i].end;
a[i].lazy = -1;
}
}

void pushup(int mid,int L,int R,int i)
{
a[i].ls = a[lson].ls;
a[i].rs = a[rson].rs;
if(a[i].ls == mid-L+1)
a[i].ls+=a[rson].ls;
if(a[i].rs == R-mid)
a[i].rs+=a[lson].rs;
a[i].ms = max(max(a[lson].ms,a[rson].ms),a[lson].rs+a[rson].ls);
}

void insert(int L,int R,int t,int l,int r,int i)//t为0释放该区间，为1则分配区间
{
if(L<=l && r<=R)
{
a[i].lazy = t;
a[i].ls = a[i].rs = a[i].ms = t*(r-l+1);
if(t)
a[i].start = a[i].end = -1;
else
{
a[i].start = L;
a[i].end = R;
}
}
else
{
int mid = (l+r)>>1;
pushdown(mid,l,r,i);
if(L<=mid)
insert(L,R,t,lc);
if(R>mid)
insert(L,R,t,rc);
pushup(mid,l,r,i);
}
}

void Reset(int n)
{
insert(1,n,1,1,n,1);
a[1].cover = 1;
a[1].cnt = 0;
}

int New(int x,int l,int r,int i)
{
if(l == r)
return l;
int mid = (l+r)>>1;
pushdown(mid,l,r,i);
if(a[lson].ms>=x)
return New(x,lc);
else if(a[lson].rs+a[rson].ls>=x)
return mid-a[lson].rs+1;
else
return New(x,rc);
}

int Free(int x,int l,int r,int i)
{
if(l == r)
return i;
int mid = (l+r)>>1;
pushdown(mid,l,r,i);
if(x<=mid)
return Free(x,lc);
else
return Free(x,rc);
}

void countup(int i)
{
a[i].cnt = a[lson].cnt+a[rson].cnt;
}

void countdown(int i)
{
if(a[i].cover)
{
a[lson].cnt = a[rson].cnt = 0;
a[lson].cover = a[rson].cover = 1;
a[i].cover = 0;
}
}

int Get(int x,int l,int r,int i)
{
if(l == r)
return l;
else
{
int mid = (l+r)>>1;
countdown(i);
if(a[lson].cnt>=x)
return Get(x,lc);
else
return Get(x-a[lson].cnt,rc);
}
}

void count(int x,int t,int l,int r,int i)
{
if(l == r)
a[i].cnt = t;
else
{
int mid = (l+r)>>1;
countdown(i);
if(x<=mid)
count(x,t,lc);
else
count(x,t,rc);
countup(i);
}
}

int main()
{
char s[20];
int n,m,x,ans;
while(~scanf("%d%d",&n,&m))
{
Reset(n);//初始状态既是内存全部释放的状态
while(m--)
{
scanf(" %s",s);
if(!strcmp(s,"Reset"))
{
Reset(n);
printf("Reset Now\n");
}
else if(!strcmp(s,"New"))
{
scanf("%d",&x);
if(a[1].ms>=x)
{
ans = New(x,1,n,1);
printf("New at %d\n",ans);
count(ans,1,1,n,1);
insert(ans,ans+x-1,0,1,n,1);
}
else
printf("Reject New\n");
}
else if(!strcmp(s,"Free"))
{
scanf("%d",&x);
ans = Free(x,1,n,1);
if(a[ans].start<0)
printf("Reject Free\n");
else
{
printf("Free from %d to %d\n",a[ans].start,a[ans].end);
count(a[ans].start,0,1,n,1);
insert(a[ans].start,a[ans].end,1,1,n,1);
}
}
else
{
scanf("%d",&x);
if(x>a[1].cnt)
printf("Reject Get\n");
else
printf("Get at %d\n",Get(x,1,n,1));
}
}
printf("\n");
}

return 0;
}  

#### HDU2871:Memory Control(线段树区间合并)

2013-11-21 23:13:38

#### HDU2871Memory Control（线段树）

2016-04-23 17:40:34

#### hdu2871_Memory Control

2011-08-25 22:21:43

#### 区间合并 内纯释放问题 hdu2871 Memory control

2015-10-11 18:01:06

#### hdu 4283（区间dp）

2015-05-22 16:51:06

#### 洛谷P3367 并查集模版

2017-04-12 20:53:10

2009-06-11 10:52:00

#### 20180321整数奇偶排序

2018-03-21 16:37:20

#### poj 2871

2014-11-08 17:09:12

#### HDU 2871

2011-04-07 09:24:00