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-08-26 12:00:26 maniAC
Implicit segment tree and cordinate compression with some optimizations will get AC.
2014-08-23 07:05:42 (Tjandra Satria Gunawan)(曾毅昆)
For people who solve this problem with time <2.50s, I recommend you to try SID problem too :-)
2014-07-28 22:54:44 Alexandru Valeanu
The limit is ok. My Treap implementation got AC with scanf/printf.
2014-07-27 14:27:38 Pîrtoacã 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
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.
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.