Блог пользователя Tareq_Ali

Автор Tareq_Ali, история, 5 лет назад, По-английски

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 $$$\textbf{" less_equal \lt data_type \gt "}$$$ to allow occurrences, after that insertion will work fine, but the function erase will do nothing ever.

I noticed that using the comparison operator $$$\textbf{" less_equal \lt data_type \gt "}$$$ makes the two functions $$$\textbf{" 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 $$$\textbf{" 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, and this is my submission.

About time and space complexity:

Time complexity is $$$\textbf{O( log2(N) )}$$$ for each operation.

Space complexity is less than double the space complexity of the general $$$\textbf{STL set}$$$ for reasonable cases ( N <= 10000000 ).

Some people use an ordered_set of pairs to count occurrences in $$$\textbf{" 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 in the set 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 <3 .

  • Проголосовать: нравится
  • +18
  • Проголосовать: не нравится

»
5 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by Tareq_Ali (previous revision, new revision, compare).

»
5 лет назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится

If I'm not mistaken, s.erase(--s.lower_bound(value)) also works. Would be nice if someone confirmed.

»
4 года назад, скрыть # |
 
Проголосовать: нравится -11 Проголосовать: не нравится

Glad, I have come across this post. Otherwise i would have cursed the ordered set for not working. Thanks for the help :)

»
4 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Noobs using pbds, gods writing treap.

»
4 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

int Count(ordered_set &s,int x){ //this function returns the number of occurrences of the value (x).

if(!Exist(s,x)){
    return 0;
 }
 return LastIdx(s,x)-FirstIdx(s,x)+1;

} what is the complexity of this function ?? because in the set we can only use distance(ptr,pointer) but it take O(n).

»
3 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can any one tell me. Why I am getting tle. D. Pashmak and Parmida's problem using ordered multiset.