Technique · CP 2000
Treap / Implicit Treap
A randomised BST with split and merge in O(log n). With implicit keys (positions from subtree sizes) it acts as an array that supports cut-and-paste, lazy reversal and range sums.
0%
mastery
0/4 solved
0/4 solved
Recognise it
The signal
cut a substring and paste it elsewhere, many times
1Brute force
Array splice or reverse: O(n) per operation.
2The observation
Moving or reversing a contiguous block.
3Why it works
BST order on keys (or implicit positions), heap order on random priorities. Lazy reverse flags are pushed down before descending.
Reach for it when
- +reverse subarrays and query sums
- +insert/delete at arbitrary positions with order statistics
Not when
- ×only point updates and range queries (a segment tree is simpler)
- ×lookup/erase only (std::set / TreeMap)
Practice
0/4 solved
- Cut and PasteCSESMedium
- Substring ReversalsCSESHard
- Reversals and SumsCSESHard
- Reversal SortingCSESHard
Pitfalls
Misconceptions
- !forgetting to push lazy flags before split/merge
- !not recomputing sizes after structural changes