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


  • 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.


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'.


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


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

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

minhthai: 2016-01-26 04:49:22

confirmed! I fell in love with BIT <3
p/s: time is too strict for java

GAURAV CHANDEL: 2016-01-13 02:59:36

After solving this , You will fall in love with BIT....

dhkhtn: 2015-12-29 22:26:57

C++ 14 (AC) runs a bit faster than C++ 5.1 (TLE). But crazy time-limit makes coding less fun. 1s is quite good.

NEXES: 2015-12-28 19:48:23

cordination compression+BIT+Binary search!!!!!..........AC

ariel: 2015-12-16 21:02:39

coordinate compresion + segment tree + unordered_map = accepted!
O(n log n) to build tree, O(log n) each query.

Anubhav Gupta: 2015-11-01 09:45:32

BIT is awesome!!

Obliterator: 2015-10-31 15:31:51

crazy time limit

Abhinandan Agarwal: 2015-10-19 14:56:29

Accepted in 0.21s :-)

Sonu Sharma: 2015-09-13 10:52:02

Century(100th) on spoj with a best problem..:D

Added by:Duc
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