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:2s
Source limit:50000B
Memory limit:256MB
Cluster: Pyramid (Intel Pentium III 733 MHz)
Languages:All except: ERL JS NODEJS PERL 6
Resource:© VNOI

hide comments
2014-06-09 21:39:29 Adamos Ttofari
Binary search getting TLE!? O.o
2014-04-22 16:47:18 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.
2014-01-13 03:00:09 Przemyslaw Lesniak
Had hard time with this problem (because i'm just so bad at implementing stuff). Used Splay trees, had approximately ~6s time.
2014-01-04 05:50:03 Jordan Alexander
Skip List runs as long as you optimize things decently, but a very sloppy implementation won't work.
2014-01-01 20:11:46 Jordan Alexander
For people having problems, try this test case, it worked for me:
I -100000002
D -100000001
C -100000001
K 1
2013-12-31 00:26:19 Fabián Gómez González
Finally got AC :). I implemented an AVL without any extra optimization and it worked fine even with the largest input data.
2013-12-29 18:18:17 Savan Popat
BIT is much easier to implement for this problem. nice question :)
2013-12-28 08:26:31 Ravi Kiran
AC using Q * log^2 Q solution.
Passed with fast input + cout (with sync_with_stdio = false).
Initially TLE'd with just plain cout.
Hope it helps!

Last edit: 2013-12-28 08:26:52
2013-12-18 00:03:34 MOAZZAM HUSSAIN
I don't know which test case is causing my program wrong.. Anyway what does cleaning buffer mean here as mentioned by one of the user above
2013-09-01 11:53:01 Nishchay Kala
I guess its a hell lot tough to get AC with Segment Tree in Java
I had to use C++.
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.