Unrolled List Editor

medium linked-list 1000 ms · 256 MB

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 value x so that it becomes the p-th element of the sequence. Every element that was at position p or later shifts one position to the right. p may be one past the current end, in which case x is appended.
  • D p — delete the element currently at position p. Every element after it shifts one position to the left.
  • G p — report the value currently at position p. 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.

Example 1

Input

3
10 20 30
5
I 1 5
G 1
D 3
G 3
I 4 99

Output

5
30

Example 2

Input

0

4
I 1 7
I 1 3
I 2 5
G 2

Output

5

Hints · reveal one at a time if you're stuck

Hint 1

A plain array makes each insert or delete shift many elements, and a linked list makes each lookup walk many nodes. Think about a structure that holds small chunks of the sequence, so both kinds of work stay around the square root of the total size.

Hint 2

Store the sequence as a list of blocks, each holding a bounded number of values. To reach position p, walk the blocks while keeping a running count of how many elements came before, then work inside the one block that contains p. Remember that an insert may go one past the end.

Hint 3

Split a block into two once it grows past about twice the target size, so the number of blocks stays roughly the square root of the total. Each block then has a bounded number of elements, so every operation is O(sqrt(n + q)) and the 1-based indexing must be converted to offsets carefully.

report an issue Prefer your own editor? sooks prep 97 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.