#include <cstdio> #include <cstring> const int MAXN = 100001; const int INF = 1 << 30 ; struct Node { int c[2], p, v, mul, sz; bool d; }T[MAXN]; int n,ans,root; void upd(int x) { T[x].sz = T[T[x].c[0]].sz + T[T[x].c[1]].sz + T[x].mul; } void sc(int _p, int _c, int _d) { T[_p].c[_d] = _c; T[_c].p = _p; T[_c].d = _d; } void rot(int x) { int y = T[x].p, d= T[x].d; if(y == root) { root = x; T[root].p = 0; } else sc(T[y].p, x, T[y].d); sc(y, T[x].c[!d], d); sc(x, y, !d); upd(y); } void splay(int x, int r)//擦入到r的子树中 { int i = x, p, p0; while((p0 = T[i].p) != r) { p = T[p0].p; if(p == r) rot(i); else { if(T[i].d == T[p0].d) { rot(p0); rot(i); } else { rot(i); rot(i); } } } upd(x); } void ins(int _v) { if(!root)//没有根的情况下 { T[++n].v = _v; T[n].c[0] = T[n].c[1] = T[n].p = 0; T[n].mul = T[n].sz = 1; root = n; return; } int i = root, j; while(1) { T[i].sz++; if(T[i].v == _v) { T[i].mul++; splay(i,0); return ; } j = T[i].c[_v > T[i].v] ; if(!j) break; else i = j; } T[++n].v = _v; T[n].c[0] = T[n].c[1] = 0; T[n].sz = T[n].mul = 1; sc(i, n, _v > T[i].v);//擦入 splay(n, 0); } void del(int lmt) { if(!root) return; int i = root ,_min = INF, b = 0, v0; while(i) { v0 = T[i].v; if(v0 == lmt) { b = i; break; } if(v0 < lmt) i = T[i].c[1]; else { if(v0 < _min) { _min = v0; b = i; } i = T[i].c[0]; } } if(!b) { ans += T[root].sz; root = 0; } else { splay(b, 0); ans += T[T[root].c[0]].sz; T[T[root].c[0]].p = 0; T[root].c[0] = 0; upd(root); } } int Find_Kth(int k) { int i = root, s0, m0; while(1) { s0 = T[T[i].c[0]].sz; m0 = T[i].mul; if(k <= s0) i = T[i].c[0]; else { if(k <= s0 + m0) return T[i].v; else { k -= s0 + m0; i = T[i].c[1]; } } } } int main() { int m, rmin; while(scanf("%d%d", &m, &rmin) != EOF) { char s[10]; int x; int des = 0;//记录增减值 n = 0; ans = 0; root = 0;//初始化 //注意,这个程序假定树上的每个点都是加上了des这个值以后的!! for(int i = 0; i < m; i++) { scanf("%s%d", s, &x); if(s[0] == 'I') { if(x >= rmin) ins(x - des); } else if(s[0] == 'A') { des += x; } else if(s[0] == 'S') { des -= x; del(rmin - des);//小于等于这个值的才要被删除 } else { if(T[root].sz - x + 1 <= 0) printf("-1\n"); else printf("%d\n", Find_Kth(T[root].sz + 1 -x) + des);//注意 } } printf("%d\n", ans); } return 0; }