OIER公司是一家大型專業化軟件公司,有着數以萬計的員工。做爲一名出納員,個人任務之一即是統計每位員工的工資。這原本是一份不錯的工做,可是使人鬱悶的是,咱們的老闆反覆無常,常常調整員工的工資。若是他心情好,就可能把每位員工的工資加上一個相同的量。反之,若是心情很差,就可能把他們的工資扣除一個相同的量。我真不知道除了調工資他還作什麼其它事情。工資的頻繁調整很讓員工反感,尤爲是集體扣除工資的時候,一旦某位員工發現本身的工資已經低於了合同規定的工資下界,他就會馬上氣憤地離開公司,而且不再會回來了。每位員工的工資下界都是統一規定的。每當一我的離開公司,我就要從電腦中把他的工資檔案刪去,一樣,每當公司招聘了一位新員工,我就得爲他新建一個工資檔案。老闆常常到我這邊來詢問工資狀況,他並不問具體某位員工的工資狀況,而是問如今工資第k多的員工拿多少工資。每當這時,我就不得不對數萬個員工進行一次漫長的排序,而後告訴他答案。好了,如今你已經對個人工做了解很多了。正如你猜的那樣,我想請你編一個工資統計程序。怎麼樣,不是很困難吧?php
輸出文件的行數爲F命令的條數加一。對於每條F命令,你的程序要輸出一行,僅包含一個整數,爲當前工資第k多的員工所拿的工資數,若是k大於目前員工的數目,則輸出-1。輸出文件的最後一行包含一個整數,爲離開公司的員工的總數。node
I命令的條數不超過100000 A命令和S命令的總條數不超過100 F命令的條數不超過100000 每次工資調整的調整量不超過1000 新員工的工資不超過100000ios
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int INF=0x3f3f3f3f;
const int MAXN=200009;
int n,mina;
int fa[MAXN],ch[MAXN][2],key[MAXN],cnt[MAXN],size[MAXN],root,sz;
void init()
{
root=sz=0;
memset(ch,0,sizeof(ch));
memset(fa,0,sizeof(fa));
memset(cnt,0,sizeof(cnt));
memset(size,0,sizeof(size));
}
inline void clear(int x) { fa[x]=ch[x][0]=ch[x][1]=cnt[x]=size[x]=0; }
inline int get(int x) { return ch[fa[x]][1]==x; }
inline void update(int x)
{
if(x){
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 father=fa[x],ffather=fa[father],which=get(x);
ch[father][which]=ch[x][!which];fa[ch[father][which]]=father;
ch[x][!which]=father;fa[father]=x;
fa[x]=ffather;
if(ffather) ch[ffather][ch[ffather][1]==father]=x;
update(father);
update(x);
}
inline void splay(int x,int goal)
{
for(int father;(father=fa[x])!=goal;rotate(x))
if(fa[father]!=goal) rotate((get(x)==get(father)?father:x));
if(goal==0) root=x;
}
inline void insert(int x)
{
if(root==0) { root=++sz;fa[sz]=ch[sz][0]=ch[sz][1]=0;cnt[sz]=size[sz]=1;key[sz]=x;return; }
int now=root,father=0;
while(1){
if(key[now]==x) { cnt[now]++;update(now);update(father);splay(now,0);return; }
father=now;
now=ch[father][key[now]<x];
if(now==0){
sz++;
fa[sz]=father;
ch[father][key[father]<x]=sz;
ch[sz][0]=ch[sz][1]=0;
cnt[sz]=size[sz]=1;
key[sz]=x;
update(father);
splay(sz,0);
return;
}
}
}
inline int findnode(int x)//找到x的節點的位置
{
int now=root;
while(1){
if(x<key[now]) now=ch[now][0];
else{
if(key[now]==x) { splay(now,0);return now; }
now=ch[now][1];
}
}
}
inline int find(int x)//找到x的排名
{
int now=root,ans=0;
while(1){
if(x<key[now]) now=ch[now][0];
else{
if(ch[now][0]) ans+=size[ch[now][0]];
if(x==key[now]) { splay(now,0);return ans+1; }
ans+=cnt[now];
now=ch[now][1];
}
}
}
inline int findx(int x)//找到排名爲x的數
{
int now=root;
while(1){
if(ch[now][0]&&x<=size[ch[now][0]]) now=ch[now][0];
else{
int tmp=(ch[now][0]?size[ch[now][0]]:0)+cnt[now];
if(x<=tmp) return key[now];
x=x-tmp;
now=ch[now][1];
}
}
}
inline int pre()//找前驅
{
int now=ch[root][0];
while(ch[now][1]) now=ch[now][1];
return now;
}
inline int suf()//找後繼
{
int now=ch[root][1];
while(ch[now][0]) now=ch[now][0];
return now;
}
inline void del(int x)//刪去一個x
{
find(x);
if(cnt[root]>1) { cnt[root]--;update(root);return; }
else if(!ch[root][0]&&!ch[root][1]) { root=sz=0;clear(root);return; }
else if(!ch[root][0]){
int oldroot=root;
root=ch[root][1];
fa[root]=0;
clear(oldroot);
return;
}else if(!ch[root][1]){
int oldroot=root;
root=ch[root][0];
fa[root]=0;
clear(oldroot);
return;
}
int leftbig=pre(),oldroot=root;
splay(leftbig,0);
ch[root][1]=ch[oldroot][1];
fa[ch[root][1]]=root;
clear(oldroot);
update(root);
}
int main()
{
//freopen("in.txt","r",stdin);
init();
insert(INF);
insert(-INF);
int tag=0,cnt=0;
scanf("%d%d",&n,&mina);
while(n--){
char s[3];
int x;
scanf("%s%d",s,&x);
if(s[0]=='I'){
if(x<mina) continue;
insert(x-tag);
cnt++;
}else if(s[0]=='A') tag+=x;
else if(s[0]=='S'){
tag-=x;
insert(mina-tag);
int tmp1=findnode(-INF);
int tmp2=findnode(mina-tag);
splay(tmp1,0);
splay(tmp2,tmp1);
ch[ch[root][1]][0]=0;
del(mina-tag);
}else{
int tmp=find(INF)-2;
if(tmp<x) puts("-1");
else{
int ans=findx(tmp+2-x);
printf("%d\n",ans+tag);
}
}
}
int tmp=find(INF)-2;
printf("%d\n",cnt-tmp);
return 0;
}