/**
SPOJ: ORDERSET
initially array is empty. Then queries are:
I x: insert x
D x delete x
C x: print the number of elements of S smaller than x
K x: print the k'th element
*/
#include<bits/stdc++.h>
using namespace std;
typedef struct node
{
int siz, key, prior;
struct node *l, *r;
node(){}
node(int key_):key(key_),siz(1),prior(rand()), l(NULL), r(NULL){}
}node;
typedef node* pnode;
int siz(pnode t)
{
return t?t->siz:0;
}
void upd_siz(pnode &t)
{
if(t) t->siz = 1 + siz(t->l) + siz(t->r);
}
void split(pnode t, pnode &l, pnode &r, int key)
{
if(!t) l = r = NULL;
else if(key < t->key ) split(t->l, l, t->l, key), r = t;
else split(t->r, t->r, r, key), l=t;
upd_siz(t);
}
void merg(pnode &t, pnode lf, pnode rg)
{
if(!lf || !rg) t = lf?lf:rg;
else if(lf->prior > rg->prior) merg(lf->r, lf->r, rg), t = lf;
else merg(rg->l, lf, rg->l), t = rg;
upd_siz(t);
}
void eras(pnode &t, int key)
{
if(!t) return;
if(t->key==key) {pnode tmp = t;merg(t, t->l, t->r);free(tmp);}
else eras(key<=t->key?t->l:t->r, key);
upd_siz(t);
}
void inseert(pnode &t, pnode nt)
{
if(!t) t = nt;
else if(nt->prior > t->prior) split(t, nt->l, nt->r, nt->key), t = nt;
else inseert(nt->key <= t->key?t->l:t->r, nt);
upd_siz(t);
}
int cnt(pnode t, int key)
{
pnode l, r;
split(t, l, r, key-1);
int ans = siz(l);
merg(t, l, r);
return ans;
}
int kth(pnode t, int k)
{
int cntLeft = siz(t->l);
if(cntLeft==k-1)
return t->key;
else if(cntLeft>=k)
return kth(t->l, k);
else return kth(t->r, k - cntLeft - 1);
}
bool Find(pnode t, int x)
{
if(!t) return 0;
if(t->key == x)
return 1;
if(x <= t->key)
return Find(t->l, x);
return Find(t->r, x);
}
int main()
{
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
ios_base::sync_with_stdio(false); cin.tie(NULL);
int n, x;
cin>>n;
pnode t = NULL;
while(n--)
{
string op;
cin>>op>>x;
if(op[0]=='I' && !Find(t, x))
{
inseert(t, new node(x));
//print(t);
}
else if(op[0]=='D' && Find(t, x))
{
eras(t, x);
}
else if(op[0]=='C')
{
cout << cnt(t, x) << "\n";
}
else if(op[0]=='K')
{
if(x>siz(t))
cout << "invalid\n";
else cout << kth(t, x) << "\n";
}
}
return 0;
}
LyoqCiAgICBTUE9KOiBPUkRFUlNFVAogICAgaW5pdGlhbGx5IGFycmF5IGlzIGVtcHR5LiBUaGVuIHF1ZXJpZXMgYXJlOgogICAgSSB4OiBpbnNlcnQgeAogICAgRCB4ICBkZWxldGUgeAogICAgQyB4OiBwcmludCB0aGUgbnVtYmVyIG9mIGVsZW1lbnRzIG9mIFMgc21hbGxlciB0aGFuIHggCiAgICBLIHg6IHByaW50IHRoZSBrJ3RoIGVsZW1lbnQKKi8KI2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKIAp0eXBlZGVmIHN0cnVjdCBub2RlCnsKICAgIGludCBzaXosIGtleSwgcHJpb3I7CiAgICBzdHJ1Y3Qgbm9kZSAqbCwgKnI7CiAgICBub2RlKCl7fQogICAgbm9kZShpbnQga2V5Xyk6a2V5KGtleV8pLHNpeigxKSxwcmlvcihyYW5kKCkpLCBsKE5VTEwpLCByKE5VTEwpe30KfW5vZGU7CiAKdHlwZWRlZiBub2RlKiBwbm9kZTsKaW50IHNpeihwbm9kZSB0KQp7CiAgICByZXR1cm4gdD90LT5zaXo6MDsKfQp2b2lkIHVwZF9zaXoocG5vZGUgJnQpCnsKICAgIGlmKHQpIHQtPnNpeiA9IDEgKyBzaXoodC0+bCkgKyBzaXoodC0+cik7Cn0Kdm9pZCBzcGxpdChwbm9kZSB0LCBwbm9kZSAmbCwgcG5vZGUgJnIsIGludCBrZXkpCnsKICAgIGlmKCF0KSBsID0gciA9IE5VTEw7CiAgICBlbHNlIGlmKGtleSA8IHQtPmtleSApIHNwbGl0KHQtPmwsIGwsIHQtPmwsIGtleSksIHIgP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Y250TGVmdD09ay0xKQogICAgICAgIHJldHVybiB0LT5rZXk7CiAgICBlbHNlIGlmKGNudExlZnQ+PWspCiAgICAgICAgcmV0dXJuIGt0aCh0LT5sLCBrKTsKICAgIGVsc2UgcmV0dXJuIGt0aCh0LT5yLCBrIC0gY250TGVmdCAtIDEpOwp9CmJvb2wgRmluZChwbm9kZSB0LCBpbnQgeCkKewogICAgaWYoIXQpIHJldHVybiAwOwogICAgaWYodC0+a2V5ID09IHgpCiAgICAgICAgcmV0dXJuIDE7CiAgICBpZih4IDw9IHQtPmtleSkKICAgICAgICByZXR1cm4gRmluZCh0LT5sLCB4KTsKICAgIHJldHVybiBGaW5kKHQtPnIsIHgpOwp9CiAKaW50IG1haW4oKQp7CiAgICAvL2ZyZW9wZW4oImluLnR4dCIsICJyIiwgc3RkaW4pOwogICAgLy9mcmVvcGVuKCJvdXQudHh0IiwgInciLCBzdGRvdXQpOwogICAgaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbyhmYWxzZSk7IGNpbi50aWUoTlVMTCk7CiAgICBpbnQgbiwgeDsKICAgIGNpbj4+bjsKICAgIHBub2RlIHQgPSBOVUxMOwogICAgd2hpbGUobi0tKQogICAgewogICAgICAgIHN0cmluZyBvcDsKICAgICAgICBjaW4+Pm9wPj54OwogICAgICAgIGlmKG9wWzBdPT0nSScgJiYgIUZpbmQodCwgeCkpCiAgICAgICAgewogICAgICAgICAgICBpbnNlZXJ0KHQsIG5ldyBub2RlKHgpKTsKICAgICAgICAgICAgLy9wcmludCh0KTsKICAgICAgICB9CiAgICAgICAgZWxzZSBpZihvcFswXT09J0QnICYmIEZpbmQodCwgeCkpCiAgICAgICAgewogICAgICAgICAgICBlcmFzKHQsIHgpOwogICAgICAgIH0KICAgICAgICBlbHNlIGlmKG9wWzBdPT0nQycpCiAgICAgICAgewogICAgICAgICAgICBjb3V0IDw8IGNudCh0LCB4KSA8PCAiXG4iOwogICAgICAgIH0KICAgICAgICBlbHNlIGlmKG9wWzBdPT0nSycpCiAgICAgICAgewogICAgICAgICAgICBpZih4PnNpeih0KSkKICAgICAgICAgICAgICAgIGNvdXQgPDwgImludmFsaWRcbiI7CiAgICAgICAgICAgIGVsc2UgY291dCA8PCBrdGgodCwgeCkgPDwgIlxuIjsKICAgICAgICB9CiAgICB9CiAgICByZXR1cm4gMDsKfQo=