这题…调了3个小时…去你妹啊
【分析】
一定注意插入!注意插入!用size[root]就好!!!
我脑袋开花了!!!
【代码】
#include<iostream>
#include<cstring>
#include<cstdio>
#define ll long long
#define fo(i,j,k) for(i=j;i<=k;i++)
using namespace std;
const int mxn=100005;
int n,m,k,sz,opt,root,tot,add;
int f[mxn],ch[mxn][2],key[mxn],cnt[mxn],size[mxn];
inline int get(int x) //左子树or右子树
{
if(ch[f[x]][0]==x) return 0;return 1;
}
inline void update(int x) //更新size
{
if(!x) return;
size[x]=cnt[x];
if(ch[x][0]) size[x]+=size[ch[x][0]];
if(ch[x][1]) size[x]+=size[ch[x][1]];
}
inline void rotate(int x)
{
int old=f[x],elder=f[old],which=get(x);
ch[old][which]=ch[x][which^1],f[ch[x][which^1]]=old;
ch[x][which^1]=old,f[old]=x;
f[x]=elder;
if(elder) ch[elder][ch[elder][1]==old]=x;
update(old),update(x);
}
inline void splay(int x)
{
for(int fa;(fa=f[x]);rotate(x))
if(f[fa]) rotate((get(x)==get(fa)?fa:x));
root=x;
}
inline void insert(int x,int czy)
{
if(!root)
{
root=(++sz);
ch[sz][0]=ch[sz][1]=f[sz]=0;
key[sz]=x,size[sz]=cnt[sz]=1;
return;
}
int now=root,fa=0;
while(1)
{
if(key[now]+czy==x)
{
cnt[now]++;
update(now),update(fa);
splay(now);
return;
}
fa=now;now=ch[now][x>key[now]+czy];
if(now==0)
{
sz++;
ch[sz][0]=ch[sz][1]=0,size[sz]=cnt[sz]=1,key[sz]=x;
f[sz]=fa,ch[fa][x>key[fa]+czy]=sz;
update(fa);
splay(sz);
return;
}
}
}
inline int number(int x)
{
int now=root;
while(1)
{
if(ch[now][1] && x<=size[ch[now][1]]) now=ch[now][1];
else
{
int tmp=(ch[now][1]?size[ch[now][1]]:0)+cnt[now];
if(x<=tmp) return now;
x-=tmp;now=ch[now][0];
}
}
}
inline int next()
{
int now=ch[root][1];
while(ch[now][0]) now=ch[now][0];
return now;
}
int main()
{
freopen("cashier.in","r",stdin);
freopen("cashier.out","w",stdout);
int i,j,x,tot=0,sum=0,lll=0; //sum:总共加进去 tot:现有几个
scanf("%d%d",&n,&k);k--;
fo(i,1,n)
{
char c[3];
scanf("%s%d",c,&x);
if(c[0]=='I')
{
if(x>k) insert(x-add,0),sum++;
}
if(c[0]=='A') add+=x;
if(c[0]=='S')
{
add-=x;
insert(k,add);
int tmp=next();
if(!tmp) root=0;
else splay(tmp);
ch[root][0]=0;
update(root);
}
if(c[0]=='F')
{
if(x>size[root]) printf("-1\n");
else printf("%d\n",key[number(x)]+add);
}
}
printf("%d\n",sum-size[root]);
return 0;
}