Unrolled List Editor
You are editing a sequence of integers. It starts with n values, and then must process q
operations, one at a time, in the order given. Each operation is one of three kinds:
I p x— insert the valuexso that it becomes thep-th element of the sequence. Every element that was at positionpor later shifts one position to the right.pmay be one past the current end, in which casexis appended.D p— delete the element currently at positionp. Every element after it shifts one position to the left.G p— report the value currently at positionp. The sequence is unchanged.
Positions are 1-indexed and always refer to the sequence as it exists right before the operation is applied (i.e. after all earlier operations have already taken effect).
Up to 2·10^5 operations touch a sequence that can itself grow to length comparable to 2·10^5,
so rebuilding an array around every insertion or deletion, or walking a plain linked list one node
at a time to reach position p, is too slow. You need a structure that reaches any position and
splices in or out of it in roughly O(√n) time.
Input
The first line contains a single integer n (0 ≤ n ≤ 2·10^5), the number of elements the
sequence starts with.
The second line contains n integers a_1, a_2, ..., a_n (-10^9 ≤ a_i ≤ 10^9), the initial
sequence, in order. If n = 0 this line is empty (it may be absent).
The third line contains a single integer q (1 ≤ q ≤ 2·10^5), the number of operations.
Each of the next q lines describes one operation in one of the forms above, where x satisfies
-10^9 ≤ x ≤ 10^9. It is guaranteed that every p is valid for the sequence's size at the moment
the operation is processed: 1 ≤ p ≤ size + 1 for I, and 1 ≤ p ≤ size for D and G, where
size is the number of elements just before that operation.
Output
For every G operation, print the reported value on its own line, in the order the operations
appear in the input.
Notes
In the first example the sequence starts as 10 20 30. I 1 5 inserts 5 at the front, giving
5 10 20 30, so the first G 1 prints 5. D 3 removes the element then at position 3 (20),
giving 5 10 30, so G 3 prints 30. The final I 4 99 appends 99, giving 5 10 30 99.
In the second example the sequence starts empty. The three inserts build it up one element at a
time into 3 5 7, and G 2 prints the middle value, 5.