Data Structures19 sections · 729 units
Open in Course

Problem - Cut and Paste

Implicit treap in action

Given a string of nn characters and qq operations: move the substring from position ll to rr to the end. Constraints: n,q2105n, q \leq 2 \cdot 10^5. Regular arrays would require O(n)O(n) per operation. With implicit treaps, each operation is O(logn)O(\log n):

1.1. Split to extract [l,r][l, r]

2.2. Merge the extracted piece at the end Before reading the solution, think about how you'd implement this with three splits and two merges.