How does a treap help to update this ordered queue?

回眸只為那壹抹淺笑 提交于 2019-12-21 20:10:16
问题 I'm having trouble understanding this solution to a problem on HackerRank. Please see the solution code below, apparently by Kimiyuki Onaka. The problem is: given a list of unique numbers, and m queries of the type, " move the current ith to jth elements (l,r) to the beginning ", return the final arrangement of the numbers. Onaka suggests that a treap data structure (one that maintains both priority and binary search) can help solve it in O(m log n) . Since I'm not versed in C++, I've tried

Why is insertion into my tree faster on sorted input than random input?

旧城冷巷雨未停 提交于 2019-12-04 08:11:15
问题 Now I've always heard binary search trees are faster to build from randomly selected data than ordered data, simply because ordered data requires explicit rebalancing to keep the tree height at a minimum. Recently I implemented an immutable treap, a special kind of binary search tree which uses randomization to keep itself relatively balanced. In contrast to what I expected, I found I can consistently build a treap about 2x faster and generally better balanced from ordered data than unordered

Treap with implicit keys

自作多情 提交于 2019-11-27 14:57:22
问题 There's a data structure called treap: that's a randomized binary search tree, which is also a heap on randomly generated so-called "priorities". There's a variation of this structure, where keys are implicit, they aren't stored in the tree, but we consider the ordered index of the node in the tree as this node's key. We need to store size of subtree in each node instead of key. This technique enables us to think about treap like some kind of array, which supports lots of operation in O(log N