CSES - Quicklist

Your task is to implement an efficient list data structure of your own that supports the following operations:

  • Remove the first k elements from the list and insert them to the end of the list in the same order
  • Report the element at index i on the list

The time complexity of both operations should be O(1). You may assume that each element is an integer in the range 1 \dots 10^9.

In a file quicklist.py implement a class QuickList according to the following template.

class QuickList:
    def __init__(self, t):
        # TODO

    def move(self, k):
        # TODO

    def get(self, i):
        # TODO

if __name__ == "__main__":
    q = QuickList([1,2,3,4,5,6,7,8,9,10])
    print(q.get(4)) # 5
    q.move(3)
    print(q.get(4)) # 8
    q.move(3)
    print(q.get(4)) # 1
    q.move(10)
    print(q.get(4)) # 1
    q.move(7)
    q.move(5)
    print(q.get(0)) # 9

Explanation: The list in the example changes as follows:

  • [1,2,3,4,5,6,7,8,9,10]
  • move(3) \rightarrow [4,5,6,7,8,9,10,1,2,3]
  • move(3) \rightarrow [7,8,9,10,1,2,3,4,5,6]
  • move(10) \rightarrow [7,8,9,10,1,2,3,4,5,6]
  • move(7) \rightarrow [4,5,6,7,8,9,10,1,2,3]
  • move(5) \rightarrow [9,10,1,2,3,4,5,6,7,8]