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

Автор Aly, история, 9 месяцев назад, По-английски

set iterator is not like vector iterator i cant add to the iteratire so how can i do binary search on a set and how can i find the diffrence betwen two positions in a set

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

»
9 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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

»
9 месяцев назад, # |
  Проголосовать: нравится +9 Проголосовать: не нравится

You can use : s.lower_bound(x) s.upper_bound(x)

»
9 месяцев назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

lower_bound(k) //O(n)

s.lower_bound(k) //O(log n)

»
9 месяцев назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

You can solve pbds if you are facing problem due to limitations in set and multiset. You can learn it from here: https://www.geeksforgeeks.org/ordered-set-gnu-c-pbds/amp/

Make sure you are using c++20, because pbds use red-black tree and it applies operations in log(n) but it consumes slidely more time than set and multiset.C++20 is faster. So it should be preferred. You can see the blog to realise that: https://codeforces.com/blog/entry/113537

»
9 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

use ordered_set with s.order_of_key(x) which gives the position of x in log(n) time.