自己yy的写法 可能有点奇怪吧 详情看代码 还是蛮短的
#include#include #include using namespace std;const int M=100055,inf=0x3f3f3f3f;int read(){ int ans=0,f=1,c=getchar(); while(c<'0'||c>'9'){ if(c=='-') f=-1; c=getchar();} while(c>='0'&&c<='9'){ans=ans*10+(c-'0'); c=getchar();} return ans*f;}int t,root,sum,n,mn,delta,now,leave;int c[M][2],v[M],size[M],fa[M];void up(int k){size[k]=size[c[k][0]]+size[c[k][1]]+1;}void rotate(int x,int& k){ int y=fa[x],z=fa[y],l=0,r=1; if(c[y][1]==x) l=1,r=0; if(y==k) k=x; else{ if(c[z][0]==y) c[z][0]=x; else c[z][1]=x;} fa[y]=x; fa[x]=z; fa[c[x][r]]=y; c[y][l]=c[x][r]; c[x][r]=y; up(y); up(x);}void splay(int x,int& k){ while(x!=k){ int y=fa[x],z=fa[y]; if(y!=k){ if(c[z][0]==y^c[y][0]==x) rotate(y,k); else rotate(x,k); } rotate(x,k); }}void insert(int &k,int w,int last){ if(!k){k=++sum; now=k; fa[k]=last; size[k]=1; v[k]=w; return ;} if(w =rank) return find(l,rank); else return find(r,rank-size[l]-1);}int main(){ n=read(); mn=read(); char ch[15]; int k,s; while(n--){ scanf("%s",ch); k=read(); if(ch[0]=='I'&&k>=mn) insert(root,k-delta,0),splay(now,root); if(ch[0]=='A') delta+=k; if(ch[0]=='S') { delta-=k,t=0,push_before(root,mn-delta); if(!t) continue; splay(t,root); int l=c[t][0]; leave+=size[l]+1; root=c[t][1]; fa[root]=0; } if(ch[0]=='F'){ if(k>size[root]) printf("-1\n"); else s=find(root,size[root]-k+1),splay(s,root),printf("%d\n",v[root]+delta); } //printf("%d ",root); printf("%d [%d %d %d]\n",v[root],size[root],c[root][0],c[root][1]); } printf("%d\n",leave); return 0;}