Skip to content
  • Dashboard
  • Review
  • Go: plan & chat
  • Roadmap
  • Patterns & lessons49
  • Sheets
  • Problems1.5k
  • Ascend
  • Toolkit
  • Core CS + SQL
  • System Design
  • Machine Coding
  • Interview hub
  • Mock interview
  • Companies & OA
  • Leaderboard
  • Battles
  • College Cup
  • People

Keep your progress

Sync across devices with a free account.

Sign upSign in
Patterns & lessons
Sign inSign up
Patterns/Trees/Ordered Set / Balanced BST (TreeMap, multiset)

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.

Learn firstOrdered Set / Balanced BST (TreeMap, multiset)BST LogicLazy Propagation Segment Tree
0%
mastery
0/4 solved
Practise nextCut and Paste
IdeaPracticePitfalls

Recognise it

The signal

cut a substring and paste it elsewhere, many times

  1. 1Brute force

    Array splice or reverse: O(n) per operation.

  2. 2The observation

    Moving or reversing a contiguous block.

  3. 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

Next

Cut and Paste

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
Go deeperresources

Resources

  • Binary Tree explore cardLeetCode · course
  • Tree cheatsheetTech Interview Handbook · article
  • BST visualizerVisuAlgo · tool
  • Introduction to Tree AlgorithmsUSACO Guide · course
  • Lowest Common Ancestorcp-algorithms · article
  • NeetCode RoadmapNeetCode · sheet
TodayReviewPatternsProblems