- C++
条
- @ 2025-8-21 17:19:52
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1e5+5;
int siz[N],val[N],dat[N];
int son[N][2];
int root;
int minn,mn,ans;
void up(int p)
{
siz[p]=siz[son[p][0]]+siz[son[p][1]]+1;
}
void split(int p,int x,int &l,int &r)
{
if(!p)
{
l=r=0;
return;
}
if(val[p]<=x)
{
l=p;
split(son[p][1],x,son[p][1],r);
}
else
{
r=p;
split(son[p][0],x,l,son[p][0]);
}
up(p);
}
int merge(int l,int r)
{
if(!l||!r) return l+r;
if(dat[l]>dat[r])
{
//合并r和l的右子树
son[l][1]=merge(son[l][1],r);
up(l);
return l;//根是l
}
else
{
//合并l和的r右子树
son[r][0]=merge(l,son[r][0]);
up(r);
return r;//根是r
}
}
void insert(int x)
{
static int idx=0;
int l,r;
split(root,x,l,r);
val[++idx]=x;
siz[idx]=1;
dat[idx]=rand();
root=merge(merge(l,idx),r);
}
int kth(int p,int k)
{
if(k<=siz[son[p][0]]) return kth(son[p][0],k);
if(k==siz[son[p][0]]+1) return val[p];
return kth(son[p][1],k-siz[son[p][0]]-1);
}
int main()
{
//ios::sync_with_stdio(0);
// cin.tie(0);
int n,tot=0;
cin>>n>>minn;
mn=0,ans=0;
while(n--)
{
char op;
int k;
cin>>op>>k;
if(op=='I')
{
if(k>=minn) insert(k-mn),tot++;
}
else if(op=='A')
{
mn+=k;
minn-=k;
}
else if(op=='S')
{
mn-=k;
minn+=k;
int l,r;
split(root,minn-1,l,r);
ans+=siz[l];
tot-=siz[l];
root=r;
}
else if(op=='F')
{
if(k>tot||k<1) cout<<-1<<'\n';
else
{
cout<<kth(root,tot+1-k)+mn<<'\n';
}
}
}
cout<<ans<<endl;
}
0 条评论
目前还没有评论...