ORDERSET - Order statistic set


In this problem, you have to maintain a dynamic set of numbers which support the two fundamental operations

  • INSERT(S,x): if x is not in S, insert x into S
  • DELETE(S,x): if x is in S, delete x from S

and the two type of queries

  • K-TH(S) : return the k-th smallest element of S
  • COUNT(S,x): return the number of elements of S smaller than x

Input

  • Line 1: Q (1 ≤ Q ≤ 200000), the number of operations
  • In the next Q lines, the first token of each line is a character I, D, K or C meaning that the corresponding operation is INSERT, DELETE, K-TH or COUNT, respectively, following by a whitespace and an integer which is the parameter for that operation.

If the parameter is a value x, it is guaranteed that 0 ≤ |x| ≤ 109. If the parameter is an index k, it is guaranteed that 1 ≤ k ≤ 109.

Output

For each query, print the corresponding result in a single line. In particular, for the queries K-TH, if k is larger than the number of elements in S, print the word 'invalid'.

Example

Input
8
I -1
I -1
I 2
C 0
K 2
D -1
K 1
K 2

Output
1
2
2
invalid

hide comments
satya_jha123: 2015-09-06 22:21:53

COULD NOT WRITE MY OWN CODE BUT LEARNT A LOT

anando_du: 2015-07-23 21:45:44

^_^ finally got AC ^_^ rank #3 ^_^

xxbloodysantaxx: 2015-07-21 23:31:16

For K type of queries O (Log N ^ 2 ) else wise O (Log N) and I used encoding . BIT <3 is simply fast and unstoppable

Ayush Mishra: 2015-07-19 04:56:51

Finally accepted using treap!

i_am_looser: 2015-05-28 13:09:25

Finally AC..... ;)
BIT rocks...

Last edit: 2015-05-28 14:09:49
Koderok: 2015-04-21 10:37:40

Sometimes, even C++ STL containers are not fast enough!

m0hamed: 2015-04-16 04:51:51

I think the time limit is too strict to pass even efficient solutions. Can you recheck the time limit?

albertg: 2015-03-27 11:52:32

time limit is too strict

(Tjandra Satria Gunawan)(曾毅昆): 2015-01-14 06:06:42

For people who solve this problem with time <2.50s, I recommend you to try SID problem too :-)

aristofanis: 2015-01-14 06:06:42

The nicest problem I have solved in this site!


Added by:Duc
Date:2008-10-28
Time limit:0.300s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL JS-RHINO NODEJS PERL6 VB.NET
Resource:© VNOI