#include #include #include #include using namespace std; #define LL(x) (x<<1) #define RR(x) (x<<1|1) #define MID(a,b) (a+((b-a)>>1)) #define INF (1<<30) const int N=50005; int n,q,ed[N]; int st_len,ed_len; struct Segtree { set T[N]; int st[N*4],len[N*4]; void PushUp(int ind) { if(st[LL(ind)] query(int ist,int ied,int ind,int lft,int rht) { if(ist<=lft&&rht<=ied) return make_pair(st[ind],len[ind]); else { int mid=MID(lft,rht); pair tmp1,tmp2; tmp1.first=INF,tmp2.first=INF; if(ist<=mid) tmp1=query(ist,ied,LL(ind),lft,mid); if(ied> mid) tmp2=query(ist,ied,RR(ind),mid+1,rht); if(tmp1.first=0;i--) { ind^=(1<T[ind]) k-=T[ind]; else ind^=(1<st2) return false; ilen=st2-st1+1; return true; } int main() { int t,t_cnt=0; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&q); seg.build(1,n,1); bit.init(); st_len=n,ed_len=n; printf("Case #%d:\n",++t_cnt); while(q--) { char cmd[5]; scanf("%s",cmd); if(cmd[0]=='A') { int m,a,b; scanf("%d%d%d",&m,&a,&b); if(st_len>=m) { if(st_len==n) { ed[1]=m; bit.insert(1,1); st_len=0; ed_len-=m; printf("%d\n",1); } else { int tmp=calu(1,st_len,m,a,b); ed[tmp]=tmp+m-1; bit.insert(tmp,1); printf("%d\n",tmp); int len,st; if(judge(ed[tmp]+1,st_len,len,st)) seg.updata(len,st,1,1,1,n); st_len=tmp-1; } } else { pair res=seg.query(m,m+a+b,1,1,n); if(res.first!=INF) { seg.updata(res.second,res.first,0,1,1,n); int tmp=calu(res.first,res.second,m,a,b); ed[tmp]=tmp+m-1; bit.insert(tmp,1); printf("%d\n",tmp); int len,st; if(judge(res.first,tmp-1,len,st)) seg.updata(len,st,1,1,1,n); if(judge(ed[tmp]+1,res.first+res.second-1,len,st)) seg.updata(len,st,1,1,1,n); } else { if(ed_lenbit.getsum()||k<=0) continue; int st=bit.query(k); int st1=1; if(k>1) st1=ed[bit.query(k-1)]+1; int len1=st-st1; if(len1>0&&st1!=1) seg.updata(len1,st1,0,1,1,n); int st2=bit.query(k+1)-1,len2; if(st2>n) len2=0,st2=n; else len2=st2-ed[st]; if(len2>0&&!(st2==n)) seg.updata(len2,ed[st]+1,0,1,1,n); if(st1!=1&&st2!=n) seg.updata(st2-st1+1,st1,1,1,1,n); if(st1==1) st_len=st2-st1+1; if(st2==n) ed_len=st2-st1+1; bit.insert(st,-1); } } } return 0; }