splay真的很慢
#include<stdio.h>
#include<ctype.h>
#define N 100005
#define INF 100000007
int m,lowst,o,tmp,leave;
char c;
int root,fa[N],ch[N][2],val[N],size[N],cnt;
int son(int t) {
return t==ch[fa[t]][1];
}
void pushup(int t) {
size[t]=size[ch[t][0]]+size[ch[t][1]]+1;
}
void node(int v,int p) {
cnt++;
val[cnt]=v, fa[cnt]=p, size[cnt]=1;
}
void rotate(int t) {
int p=fa[t], d=son(t);
ch[p][d]=ch[t][!d];
if(ch[t][!d]) fa[ch[t][!d]]=p;
fa[t]=fa[p];
if(fa[p]) ch[fa[p]][son(p)]=t;
else root=t;
fa[p]=t, ch[t][!d]=p;
pushup(p), pushup(t);
}
void splay(int t,int r) {
while(fa[t]!=r) {
int p=fa[t];
if(fa[p]==r) rotate(t);
else {
if(son(t)==son(p)) rotate(p), rotate(t);
else rotate(t), rotate(t);
}
}
}
void insert(int v) {
if(!root) {
node(v,0);
root=cnt;
return;
}
int t=root;
while(ch[t][v>val[t]]) t=ch[t][v>val[t]];
node(v,t);
splay(ch[t][v>val[t]]=cnt,0);
}
int suc(int v) {
int t=root,ret=0;
while(t) {
if(v>val[t]) t=ch[t][1];
else ret=t, t=ch[t][0];
}
return ret;
}
void erase() {
int t=suc(lowst-o);
splay(t,0);
leave+=size[ch[t][0]];
size[t]-=size[ch[t][0]];
ch[t][0]=0;
}
int select(int k) {
int t=root,sz;
while(sz=size[ch[t][1]],k!=sz+1) {
if(k<=sz) t=ch[t][1];
else k-=sz+1, t=ch[t][0];
}
return t ? val[t]+o : -1;
}
int main() {
scanf("%d%d",&m,&lowst);
insert(INF);
while(m--) {
while(isspace(c=getchar()));
scanf("%d",&tmp);
switch(c) {
case 'I': if(tmp>=lowst) insert(tmp-o); break;
case 'A': o+=tmp; break;
case 'S': o-=tmp; erase(); break;
case 'F': printf("%d\n",select(tmp+1)); break;
}
}
printf("%d\n",leave);
}