Sphere Online Judge

SPOJ Problem Set (classical)

3273. Order statistic set

Problem code: ORDERSET

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


Added by:Duc
Time limit:0.300s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel Pentium G860 3GHz)
Languages:All except: ERL JS NODEJS PERL 6 SCM chicken VB.net
Resource:© VNOI

hide comments
2015-01-27 12:09:15 Alexander Kulkov
AC in 0.60 using STL
2015-01-26 11:51:28 Paramdeep Singh
For solutions involving segment tree + coordinate compression, in case you are using map for compressing values, fast IO code might be required for the code to pass.
2015-01-14 06:06:42 Archit Jain
segment tree and coordinate compression
2015-01-14 06:06:42 maniAC
Implicit segment tree and cordinate compression with some optimizations will get AC.
2015-01-14 06:06:42 (Tjandra Satria Gunawan)(曾毅昆)
For people who solve this problem with time <2.50s, I recommend you to try SID problem too :-)
2015-01-14 06:06:42 Alexandru Valeanu
The limit is ok. My Treap implementation got AC with scanf/printf.
2015-01-14 06:06:42 Prtoac George Sebastian
I've got TLE using Treaps with O(logN) for all operations and queries. Is it the time limit for this problem too low?

Last edit: 2014-07-27 14:29:01
2015-01-14 06:06:42 Adamos Ttofari
Binary search getting TLE!? O.o
2015-01-14 06:06:42 zicowa
@all i solved this problem using BIT as well as seg tree but the interesting part is that seg tree solution has complexity O(log(n)) for each operation but BIT solution involves O(log^2(n)) to handle 3rd type query still BIT solution submitted with time 4.74 and seg tree with 5.26.
2015-01-14 06:06:42 Przemyslaw Lesniak
Had hard time with this problem (because i'm just so bad at implementing stuff). Used Splay trees, had approximately ~6s time.
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.