How Tarjan answers LCA queries offline

Short answer: Tarjan’s offline LCA answers a batch of lowest-common-ancestor queries during one depth-first search, using union-find. When the search finishes a node, the node is united with its parent and the parent’s set is labeled with the parent. A query between the finished node and a node already…

How a treap uses random priorities

Short answer: A treap is a binary search tree on keys and a heap on independently random priorities. Each node stores a key and a priority. The in-order walk is the key order, and every parent has a better priority than its children. Insert and delete are rotations that…

How a Fenwick tree sums a prefix

Short answer: A Fenwick tree, or binary indexed tree, stores partial sums so that the prefix sum through index i, and a point update at i, both run in O(log n). The index trick is the least significant set bit: i & −i in two’s complement. That bit names…