因为是整体加减,所以直接记录在外面。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #define inf 0x3f3f3f3f 6 #define N 1000005 7 using namespace std; 8 int sum; 9 int li,root; 10 int cnt,size[N],ch[N][2],fa[N],n,m,k[N]; 11 void push_up(int x) 12 { 13 size[x]=size[ch[x][0]]+size[ch[x][1]]+1; 14 } 15 void rotate(int p) 16 { 17 int q=fa[p],y=fa[q],x=(ch[q][1]==p); 18 ch[q][x]=ch[p][x^1];fa[ch[q][x]]=q; 19 ch[p][x^1]=q;fa[q]=p;fa[p]=y; 20 if(y) 21 { 22 if(ch[y][0]==q)ch[y][0]=p; 23 else ch[y][1]=p; 24 } 25 push_up(q); 26 return ; 27 } 28 void splay(int x) 29 { 30 for(int y;y=fa[x];rotate(x)) 31 { 32 if(fa[y]) 33 { 34 if((ch[y][0]==x&&ch[fa[y]][0]==y)||(ch[y][1]==x&&ch[fa[y]][1]==y))rotate(y); 35 else rotate(x); 36 } 37 } 38 push_up(x);root=x; 39 } 40 void del(int x) 41 { 42 splay(x); 43 if(!ch[x][0]) 44 { 45 root=ch[x][1];fa[ch[x][1]]=0; 46 } 47 else if(!ch[x][1]) 48 { 49 root=ch[x][0],fa[ch[x][0]]=0; 50 } 51 else 52 { 53 int tmp=ch[x][0];fa[tmp]=0; 54 while(ch[tmp][1])tmp=ch[tmp][1]; 55 splay(tmp);ch[tmp][1]=ch[x][1];fa[ch[x][1]]=tmp; 56 push_up(tmp); 57 }return ; 58 } 59 int find(int x,int kk) 60 { 61 //cout<<kk<<endl; 62 if(size[ch[x][0]]+1==kk)return k[x]; 63 if(size[ch[x][0]]>=kk)return find(ch[x][0],kk); 64 return find(ch[x][1],kk-size[ch[x][0]]-1); 65 } 66 void insert(int z) 67 { 68 //cout<<z<<endl; 69 int x=root;size[x]++; 70 while(ch[x][k[x]<z])x=ch[x][k[x]<z],size[x]++; 71 ch[x][k[x]<z]=++cnt;k[cnt]=z;size[cnt]=1;fa[cnt]=x;splay(cnt); 72 } 73 int mn() 74 { 75 int tmp=root;while(ch[tmp][0])tmp=ch[tmp][0]; 76 return tmp; 77 } 78 int main() 79 { 80 //freopen("out.txt","w",stdout); 81 int now=0; 82 scanf("%d%d",&n,&m); 83 cnt=1;k[1]=inf;sum=0;size[1]=1;root=1; 84 for(int i=1;i<=n;i++) 85 { 86 char c[2];int tmp; 87 scanf("%s%d",c,&tmp); 88 if(c[0]=='I') 89 { 90 if(tmp<m)continue; 91 else now++,insert(tmp-sum); 92 } 93 else if(c[0]=='A') 94 { 95 sum+=tmp; 96 } 97 else if(c[0]=='S') 98 { 99 sum-=tmp; 100 while(1) 101 { 102 //cout<<root<<endl; 103 int u=mn(); 104 if(k[u]+sum<m) 105 { 106 li++; 107 del(u);continue; 108 } 109 break; 110 } 111 } 112 else if(c[0]=='F') 113 { 114 if(tmp>now-li) 115 { 116 puts("-1");continue; 117 } 118 printf("%d ",find(root,now-li-tmp+1)+sum); 119 } 120 } 121 printf("%d ",li); 122 return 0; 123 }