解析:这道题目就是个裸题 每次操作的时候正常维护就行了
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define maxn 2000005
struct Treap{
Treap *l,*r;
int fix,key,size;
Treap(int key_){
fix = rand();
l = r = NULL;
size = 1;
key = key_;
}
inline void updata(){
size=1+(l?l->size:0)+(r?r->size:0);
}
}*root;
struct Droot{
Treap *first, *second;
Droot(){first = second = NULL;}
};
Droot D_NULL;
inline int Size(Treap *x){return x?x->size:0;}
Treap *Merge(Treap *A,Treap *B){
if(!A)return B;
if(!B)return A;
if(A->fix<B->fix){
A->r=Merge(A->r,B);
A->updata();
return A;
}else{
B->l=Merge(A,B->l);
B->updata();
return B;
}
}
Droot Split(Treap *x,int k){
if(!x)return D_NULL;
Droot y;
if(Size(x->l)>=k){
y=Split(x->l,k);
x->l=y.second;
x->updata();
y.second=x;
}else{
y=Split(x->r,k-Size(x->l)-1);
x->r=y.first;
x->updata();
y.first=x;
}
return y;
}
int Findkth(int k){
Droot x=Split(root,k-1);
Droot y=Split(x.second,1);
Treap *ans=y.first;
root=Merge(Merge(x.first,ans),y.second);
return ans->key;
}
int Getkth(Treap *x,int v){
if(!x)return 0;
return v<=x->key?Getkth(x->l,v):Getkth(x->r,v)+Size(x->l)+1;
}
void Insert(int v){
int k=Getkth(root,v);
Droot x=Split(root,k);
Treap *n=new Treap(v);
root=Merge(Merge(x.first,n),x.second);
}
void Delete(int k){
int pos = Getkth(root, k);
Droot x=Split(root,pos);
Droot y=Split(x.second,1);
root=Merge(x.first,y.second);
}
int pre(int k){
int pos = Getkth(root, k);
if(pos-1 == Size(root)) return 100000000;
Droot x = Split(root, pos-1);
Droot y = Split(x.second, 1);
Treap *ans = y.first;
root=Merge(Merge(x.first,ans),y.second);
return ans->key;
}
int bak(int k){
int pos = Getkth(root, k+1);
if(pos == Size(root)) return 100000000;
Droot x = Split(root, pos);
Droot y = Split(x.second, 1);
Treap *ans = y.first;
root=Merge(Merge(x.first, ans), y.second);
return ans->key;
}
int M, left_sum;
void remove_(int v){
int pos = Getkth(root, v);
if(!pos) return ;
Droot x = Split(root, pos);
root = x.second;
left_sum += pos;
}
int mabs(int r){return r>0?r:-r;}
int main(){
char tmp[4];
int o, k, Min;
scanf("%d%d",&M,&k);
Min = k;
while(M--){
scanf("%s%d", tmp, &o);
if(tmp[0] == 'I'){
if(o >= Min)
Insert(o+k-Min);
}
else if(tmp[0] == 'S'){
k+=o;
remove_(k);
}
else if(tmp[0] == 'F'){
int rk = Size(root) - o + 1;
if(rk <= 0) printf("-1\n");
else printf("%d\n", Findkth(rk)-k+Min);
}else k -= o;
}
printf("%d\n", left_sum);
}
/*
9 10
I 60
I 70
S 50
F 2
I 30
S 15
A 5
F 1
F 2
*/