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
surayans tiwari(http://bit.ly/1EPzcpv): 2017-01-15 05:12:54

nice !!

geoffreymace7: 2017-01-01 13:18:13

map container gives TLE, use unordered_map instead.

rihaz_zahir: 2016-12-29 10:39:23

I have used segment tree+coordinate compression to solve with O(QlogN) complexity.
I am going to revisit the problem after I learn BIT and/or Red-Black tree :)

rohitranjan017: 2016-08-30 22:30:12

map -> TLE
uordered_map -> 0.72 AC

rohit96: 2016-06-02 21:59:11

tree policy

Nguyen Cuong: 2016-05-20 05:06:48

compression + BIT + binary search ~ O(n log^2(n)) got AC ^^

ruizhang: 2016-05-19 19:29:43

Spent a day implementing the augmented red-black tree on CLRS, good problem!

Integralisha: 2016-04-29 00:43:31

Use tree policy [for C++ coders].

fnf: 2016-04-27 12:39:01

ios_base:: sync_with_stdio(false); cin.tie(0);
if u are using this fast I/O then do this
#define endl "\n"

vedang: 2016-01-26 14:55:55

Had to replace cout with printf to get rid of TLE.
Nice problem anyway :D


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