#P2809. Folding Paper Simulation
Folding Paper Simulation
Folding Paper Simulation
Hzwer bought a magical paper strip that initially consists of cells, each containing an integer . There is a boundary between every two adjacent cells. Hzwer can perform two types of operations on the paper:
-
Fold ((F)): Choose any boundary between the -th and -th cell (with (1 \le k < L), where (L) is the current number of cells). Then fold the paper to the left. In this folding operation, the right part (cells (k+1) to (L)) is flipped (i.e. its order is reversed) and laid on top of the left part as follows:
- Let the left part be (A[1 \ldots k]) and the right part be (A[k+1 \ldots L]). Let (R = L - k) and let (P) be the reversed right part.
- If (R \ge k) then define (\text{extra} = R - k). The final paper is formed by taking the first (\text{extra}) elements of (P) (which do not overlap any cell), followed by overlapping each cell of the left part with a corresponding cell from (P) as: for (i=1) to (k), the new cell becomes (A[i] + P[\text{extra} + i]).
- Otherwise, if (R < k), then the first (k - R) cells (i.e. (A[1 \ldots k-R])) remain unchanged, and for (i=1) to (R) the overlapping part is formed by summing (A[k-R+i] + P[i]).
-
Flip ((R)): Flip the entire paper to the left (i.e. reverse the order of the cells). That is, after the operation, the number in the (i)-th cell becomes the number in the (L - i + 1)-th cell.
You are given the initial paper and a sequence of operations. Your task is to simulate the operations in order and output the final configuration of the paper.
Note: The final configuration should be printed as a sequence of integers separated by a single space.
inputFormat
The input consists of multiple lines:
- The first line contains an integer (N) ((2 \le N \le 10^5)) representing the initial number of cells.
- The second line contains (N) integers (N_1, N_2, \ldots, N_N), where (|N_i| \le 10^9).
- The third line contains an integer (M) ((1 \le M \le 10^5)) representing the number of operations.
- Each of the following (M) lines contains an operation in one of the following formats:
- "F k" indicates a fold operation along the boundary between the (k)-th and ((k+1))-th cell. It is guaranteed that (1 \le k < L) where (L) is the current number of cells at that moment.
- "R" indicates a flip operation.
outputFormat
Output a single line containing the final configuration of the paper. The cells should be printed in order, separated by a single space.
sample
5
1 2 3 4 5
1
F 3
1 7 7