Today I learned about the piece table data structure.
Today I learned about the skip list data structure.
Grab a piece of paper and try to fold your way out of this problem.