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
shubham_7616: 2017-08-17 22:08:43

Just loved it......Learned a lot !!
Nice application of BIT.
For those getting TLE use unordered map instead of map and use scanf printf .

starbot: 2017-08-17 20:19:00

Amazing problem...learnt a lot...time to play cs

fz0718: 2017-07-25 04:34:21

The four magic lines of code... :)

#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

akt_1998: 2017-07-03 11:29:26

Must do question for beginners in BIT

rajeev_mnnit: 2017-06-25 14:23:40

somthing new... Tree policy ;)

Last edit: 2017-06-25 14:24:07
leafbebop: 2017-06-06 14:36:48

Having trouble to compress my code.
Why, I love B-Trees.

atulshanbhag: 2017-03-30 15:11:01

Do not print invalid on delete if number does not exist! cost me 7 WAs :(

jedyn: 2017-03-25 21:23:49

isn't time limit too strict for java? @Duc

hasan356: 2017-03-17 21:16:21

Learned a lot !!

s_jindal00: 2017-03-04 21:19:07

Take care of 'invalid'. Don't print 'INVALID'! costed me 1 WA!

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