Official

C - Rotation Editorial by en_translator


For simplicity, we will use zero-based index for \(S\).

The problem statement asks to “remove the last character of \(S\) and prepend it,” but if this operations is naively performed \(x\) times, it will cost an \(\mathrm{O}(NQ)\) time complexity, which does not fit in the execution time limit under the Constraints.

Here, maintain an integer \(P\) that counts how many times the operation “remove the last character of \(S\) and prepend it” was done. Initially, \(P=0\).

For a query of type 1 x, increase \(P\) by \(x\).

Consider a query of type 2 x.

Let \(X\) be \(S\) before repeatedly performing the operation “remove the last character of \(S\) and prepend it” \(x\) times, and \(Y\) be that after performing it. Then, it holds that \(X_{i-x \bmod N}=Y_i\).

Therefore, it is sufficient to output \(S_{x-P \bmod N}\).

Thus, the problem has been solved in a total of \(\mathrm{O}(N+Q)\) time, which is fast enough.

posted:
last update: