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

Автор Windycaution, история, 3 года назад, По-английски

Hello, I have recently got haunted by telling the size of memory pool of persistent segment tree, getting WA / RE / MLE results on many problems. Therefore, I'd appreciate it if someone could give me a implementation of persistent segtree applying std::vector to manage the memory (in order to avoid getting WA / RE / MLE by telling wrongly the size of arrays), which I could not find one anywhere on the Internet.

If the code could be laconic as well, it would be better.

Thank you!

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

»
3 года назад, # |
  Проголосовать: нравится +6 Проголосовать: не нравится
»
3 года назад, # |
  Проголосовать: нравится +8 Проголосовать: не нравится
»
3 года назад, # |
Rev. 5   Проголосовать: нравится +1 Проголосовать: не нравится

mine; I choose not to templatize and prefer just to modify the PST for the problem, for example spoj dquery, kth smallest in range

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

    I still stick to tempatizing & formulating every data structure within a mathematical model, but still thank you for advising.

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

I would highly recommend using std::deque instead of std::vector because of two reasons:

  1. Inserting and deleting elements from a deque at beginning or end will not invalidate pointers. This means your code could still use pointers everywhere. So by using std::deque your implementation could look almost identical to the usual implementation of a persistent segment tree .

  2. std::deque has lower memory overhead than std::vector. This is probably not super important, but it is something.

Another alternative you could look into is using a bump allocator.