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
jmr99: 2018-06-18 23:48:44

refer this.. for policy based data structures in C++

http://codeforces.com/blog/entry/6700

divyansh121212: 2018-06-14 23:22:42

Solved using Treaps

scube97: 2018-04-27 19:46:40

Why is the time limit so strict ? Tried everything , unordered_map,changes long long to int,but to no avail still a tle. Approach segment tree after compression.Each query in O(logn*logn)

Last edit: 2018-04-27 19:47:21
alexandro5432: 2018-04-09 22:06:56

easy Treap question

hitman007: 2018-01-21 20:22:58

Any Java programmer here who got it accepted ? I am hearing only c++programs. In Java, even with Compression + BIT + Binary search + Fast I/O, I am getting TLE after test case #6.

hitman007: 2018-01-21 16:58:07

Implemented with BIT. But constantly getting WA after test case #5. Any hint ?

be1035016: 2017-12-29 16:57:27

map with bit->0.56sec
unordered_map with bit->0.45sec

pranavraj219: 2017-12-13 22:51:51

BIT - 0.35s
SEGTREE - 0.75s
Regular map did not give TLE but still its better to use unordered_map.

sarwar__05: 2017-12-10 00:08:14

TLE with std::map, AC with std::unordered_map

amulyagaur: 2017-10-20 07:06:57

Direct implementation of policy based data structures in C++


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