Official

E - Rotate and Flip Editorial by en_translator


Each operation is an affine transformation. For example, a symmetric transformation about the line \(x = p\) can be represented as follows:

\( \begin{pmatrix} -1 & 0 & 2p \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{pmatrix} \begin{pmatrix} x \\ y \\ 1 \end{pmatrix}= \begin{pmatrix} 2p-x \\ y \\ 1 \end{pmatrix} \)

Therefore, one can calculate the cumulative product of transformation matrices in order to answer each query in an \(O(1)\) time. Since the precalculation of cumulative products takes \(O(M)\) time, the total time complexity is \(O(N+M+Q)\).

More naively, it is sufficient to hold the information of “where (0,0), (1,0) and (1,1) goes after each operation,” in order to find the resulting coordinates of any point after some transformations in a \(O(1)\) time. By actually simulating the operations for those three points beforehand, the total time complexity will be \(O(N+M+Q)\).

posted:
last update: