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

Автор Radiohead___, история, 19 часов назад, По-английски

Hello everyone, I hope you are doing well, can anybody explain to me why this code costs O(n) on sets and multiset but in the other side it costs O(log(n)) on the sorted vectors.

the code ->

multiset ms; int x ; auto it = upper_bound(ms.begin() , ms.end() , x) ;

between this code cost O(log(n)) auto it = ms.upper_bound(x);

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

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

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

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

Multisets and sets in c++ cannot be queried for k-th element, in contrast to sorted vectors. The upper_bound function implemented for sets is different from the general purpose upper_bound, and the latter will take O(n) as specified in the c++ documentation: "On non-random-access iterators, the iterator advances produce themselves an additional linear complexity in N on average." Set iterators in c++ support the advance ++ or -- operations, and that's why it will take O(n) for the general upper bound in sets.

  • »
    »
    17 часов назад, # ^ |
      Проголосовать: нравится +1 Проголосовать: не нравится

    In addition, if you need to access the kth element, try using an order-statistic tree