Multi Ordered Set Data Structure

Правка en1, от Tareq_Ali, 2021-02-26 02:29:00

Hello everyone

I would like to share with you an efficient trick we can use with ordered_set data structure to make it contain more than one occurrence for any value we want without any problem. But first, if you don't know this data structure, you can read about it here .

Now let's discuss that: First of all, for sure we need to set the comparison operator to less_equal to allow occurrences, after that insertion will work fine, but the function erase will do nothing ever. I noticed that using the comparison operator less_equal makes the two functions " s.lower_bound(value) , s.upper_bound(value) " exchange their functions for any value, depending on that to erase one occurrence of some value from the set we can write " s.erase(s.upper_bound(value)) ", just that simple, it will work efficiently.

To see the important functions of this data structure you can read my code, I have tested the code and got accepted in many problems. for example, this is the problem ([problem:https://mirror.codeforces.com/contest/652/problem/D]), and this is my submission ([submission:https://mirror.codeforces.com/contest/652/submission/108479892]).

About time and space complexity: Time complexity is O( log2(N) ) for each operation. Space complexity is less than double the space complexity of the general (STL) set for reasonable cases ( N <= 10000000 ).

Some people use an ordered set of pairs to count occurrences in "p.second" and do the job, But this is faster to code and can do more tasks, Suppose you have an empty set and 2 types of queries: * inserting the element (x) into the set. * answering this query : what is the k_th element if we sort the elements in ascending order. Now if you use it this way you can answer the query in 1 line of code, and the pair trick can't solve it.

Finally, thank you for reading my first blog, I hope it will be benefit.

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en13 Английский Tareq_Ali 2021-02-26 03:17:41 1 Tiny change: 't this way you can a' -> 't this way, you can a' (published)
en12 Английский Tareq_Ali 2021-02-26 03:10:37 15
en11 Английский Tareq_Ali 2021-02-26 03:07:18 12
en10 Английский Tareq_Ali 2021-02-26 03:00:15 31
en9 Английский Tareq_Ali 2021-02-26 02:56:24 30
en8 Английский Tareq_Ali 2021-02-26 02:54:25 4
en7 Английский Tareq_Ali 2021-02-26 02:49:49 3 Tiny change: 'uss that: \nFirst of' -> 'uss that: \n\nFirst of'
en6 Английский Tareq_Ali 2021-02-26 02:49:03 3 Tiny change: 'cuss that:\n\nFirst of' -> 'cuss that: \nFirst of'
en5 Английский Tareq_Ali 2021-02-26 02:48:40 2 Tiny change: 'ss that:\nFirst of' -> 'ss that:\n\nFirst of'
en4 Английский Tareq_Ali 2021-02-26 02:47:10 52
en3 Английский Tareq_Ali 2021-02-26 02:42:07 24
en2 Английский Tareq_Ali 2021-02-26 02:37:12 60
en1 Английский Tareq_Ali 2021-02-26 02:29:00 2038 Initial revision (saved to drafts)