ORDERSET - Order statistic set

Đọc đề đẹp hơn ở:

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
maheshwari7778: 2021-01-30 11:01:03

it can easily solve with set in c++

fub_123: 2020-09-30 12:13:18

pbds :)

stormgate078: 2020-09-02 15:13:28

Can be solved with modified AVL

yaseenmollik: 2020-08-06 12:26:01

Can be easily solve using pbds. But, should be solved using segment tree for practice purpose.

amar_shukla1: 2020-07-28 07:34:51

easy to solve by policy data structure

fahimcp495: 2020-06-02 00:05:53

it's worthy to solve after learning Treap

anirudh_1999: 2020-05-03 09:28:28

Getting TLE in java even after using segment trees with coordinate compression.

pareksha: 2020-04-29 07:53:08

Submitted solution -> got TLE
Submitted same solution again -> AC

Added by:Jimmy
Time limit:1.5s-2s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL JS-RHINO NODEJS PERL6 VB.NET