1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 #define MAXN 50010 6 struct node 7 { 8 int left,right,sum,lazy,s,e,cnt,cover; 9 }; 10 node tree[MAXN<<2]; 11 inline void PushUp(int mid,int L,int R,int rt) 12 { 13 tree[rt].left=tree[rt<<1].left; 14 tree[rt].right=tree[rt<<1|1].right; 15 if(tree[rt].left==mid-L+1) 16 tree[rt].left+=tree[rt<<1|1].left; 17 if(tree[rt].right==R-mid) 18 tree[rt].right+=tree[rt<<1].right; 19 tree[rt].sum=max(tree[rt<<1].sum,tree[rt<<1|1].sum); 20 tree[rt].sum=max(tree[rt].sum,tree[rt<<1].right+tree[rt<<1|1].left); 21 } 22 inline void PushDown(int mid,int L,int R,int rt) 23 { 24 if(tree[rt].lazy!=-1) 25 { 26 tree[rt<<1].lazy=tree[rt<<1|1].lazy=tree[rt].lazy; 27 tree[rt<<1].sum=tree[rt<<1].left=tree[rt<<1].right=(mid-L+1)*tree[rt].lazy; 28 tree[rt<<1|1].sum=tree[rt<<1|1].left=tree[rt<<1|1].right=(R-mid)*tree[rt].lazy; 29 tree[rt<<1].s=tree[rt<<1|1].s=tree[rt].s; 30 tree[rt<<1].e=tree[rt<<1|1].e=tree[rt].e; 31 tree[rt].lazy=-1; 32 } 33 } 34 void Update(int x,int y,int val,int L,int R,int rt) 35 { 36 if(x<=L&&R<=y) 37 { 38 tree[rt].lazy=val; 39 tree[rt].sum=tree[rt].left=tree[rt].right=val*(R-L+1); 40 if(val) 41 tree[rt].s=tree[rt].e=-1; 42 else 43 { 44 tree[rt].s=x; 45 tree[rt].e=y; 46 } 47 } 48 else 49 { 50 int mid=(L+R)>>1; 51 PushDown(mid,L,R,rt); 52 if(x<=mid) 53 Update(x,y,val,L,mid,rt<<1); 54 if(y>mid) 55 Update(x,y,val,mid+1,R,rt<<1|1); 56 PushUp(mid,L,R,rt); 57 } 58 } 59 int New(int x,int L,int R,int rt) 60 { 61 if(L==R) 62 return L; 63 int mid=(L+R)>>1; 64 PushDown(mid,L,R,rt); 65 if(tree[rt<<1].sum>=x) 66 return New(x,L,mid,rt<<1); 67 else if(tree[rt<<1].right+tree[rt<<1|1].left>=x) 68 return mid-tree[rt<<1].right+1; 69 else 70 return New(x,mid+1,R,rt<<1|1); 71 } 72 int Free(int x,int L,int R,int rt) 73 { 74 if(L==R) 75 return rt; 76 int mid=(L+R)>>1; 77 PushDown(mid,L,R,rt); 78 if(x<=mid) 79 return Free(x,L,mid,rt<<1); 80 else 81 return Free(x,mid+1,R,rt<<1|1); 82 } 83 inline void CountUp(int rt) 84 { 85 tree[rt].cnt=tree[rt<<1].cnt+tree[rt<<1|1].cnt; 86 } 87 inline void CountDown(int rt) 88 { 89 if(tree[rt].cover) 90 { 91 tree[rt<<1].cnt=tree[rt<<1|1].cnt=0; 92 tree[rt<<1].cover=tree[rt<<1|1].cover=1; 93 tree[rt].cover=0; 94 } 95 } 96 int Get(int x,int L,int R,int rt) 97 { 98 if(L==R) 99 return L; 100 else 101 { 102 int mid=(L+R)>>1; 103 CountDown(rt); 104 if(tree[rt<<1].cnt>=x) 105 return Get(x,L,mid,rt<<1); 106 else 107 return Get(x-tree[rt<<1].cnt,mid+1,R,rt<<1|1); 108 } 109 } 110 void Count(int x,int val,int L,int R,int rt) 111 { 112 if(L==R) 113 tree[rt].cnt=val; 114 else 115 { 116 int mid=(L+R)>>1; 117 CountDown(rt); 118 if(x<=mid) 119 Count(x,val,L,mid,rt<<1); 120 else 121 Count(x,val,mid+1,R,rt<<1|1); 122 CountUp(rt); 123 } 124 } 125 inline void Reset(int n) 126 { 127 Update(1,n,1,1,n,1); 128 tree[1].cover=1; 129 tree[1].cnt=0; 130 } 131 int main() 132 { 133 char s[12]; 134 int n,m,x,y; 135 while(~scanf("%d%d",&n,&m)) 136 { 137 Reset(n); 138 while(m--) 139 { 140 scanf(" %s",s); 141 if(!strcmp(s,"Reset")) 142 { 143 Reset(n); 144 puts("Reset Now"); 145 } 146 else if(!strcmp(s,"New")) 147 { 148 scanf("%d",&x); 149 if(tree[1].sum>=x) 150 { 151 y=New(x,1,n,1); 152 printf("New at %d\n",y); 153 Count(y,1,1,n,1); 154 Update(y,y+x-1,0,1,n,1); 155 } 156 else 157 puts("Reject New"); 158 } 159 else if(!strcmp(s,"Free")) 160 { 161 scanf("%d",&x); 162 y=Free(x,1,n,1); 163 if(tree[y].s<0) 164 puts("Reject Free"); 165 else 166 { 167 printf("Free from %d to %d\n",tree[y].s,tree[y].e); 168 Count(tree[y].s,0,1,n,1); 169 Update(tree[y].s,tree[y].e,1,1,n,1); 170 } 171 } 172 else 173 { 174 scanf("%d",&x); 175 if(x>tree[1].cnt) 176 puts("Reject Get"); 177 else 178 printf("Get at %d\n",Get(x,1,n,1)); 179 } 180 } 181 putchar('\n'); 182 } 183 return 0; 184 }