Imaginary Units

View as PDF

Submit solution

Points: 10 (partial)
Time limit: 1.0s
Memory limit: 1G

Author:
Problem types

You are given an N \times N grid of complex numbers, all initially equal to i, the imaginary unit. A series of Q updates will be made to the grid, in one of the following formats:

  • R j - multiply every number in row j by i.
  • C j - multiply every number in column j by i.
  • I - increment every number in the grid by i.

After all Q updates, determine the sum of the numbers in the grid.

Constraints

1 \le N,Q \le 5 \times 10^5
1 \le j \le N

Subtask 1 [30%]

There are no increment (type I) updates.

Subtask 2 [70%]

No additional constraints.

Input Specification

The first line contains two space-separated integers, N and Q, the sidelength of the grid and the number of updates, respectively.

The next Q lines each contain an update in one of the following formats:

  • R j - multiply every number in row j by i.
  • C j - multiply every number in column j by i.
  • I - increment every number in the grid by i.

Output Specification

The sum of the numbers in the grid after all Q updates can be expressed in the form a + bi, where a and b are integers. Output one line containing two space-separated integers, a and b.

Note: These numbers may not fit within a 32-bit integer type.

Sample Input

5 4
R 3
I
C 1
R 4

Sample Output

-19 25

Explanation for Sample

Here is the state of the grid after each update:

Initially, every number in the grid is equal to i.


\begin{bmatrix}
i & i & i & i & i \\
i & i & i & i & i \\
i & i & i & i & i \\
i & i & i & i & i \\
i & i & i & i & i
\end{bmatrix}

The first update, R 3, multiplies every number in row 3 by i.


\begin{bmatrix}
\phantom{-}i & \phantom{-}i & \phantom{-}i & \phantom{-}i & \phantom{-}i \\
\phantom{-}i & \phantom{-}i & \phantom{-}i & \phantom{-}i & \phantom{-}i \\
-1 & -1 & -1 & -1 & -1 \\
\phantom{-}i & \phantom{-}i & \phantom{-}i & \phantom{-}i & \phantom{-}i \\
\phantom{-}i & \phantom{-}i & \phantom{-}i & \phantom{-}i & \phantom{-}i
\end{bmatrix}

The second update, I, increments every number in the grid by i.


\begin{bmatrix}
\phantom{-}2i & \phantom{-}2i & \phantom{-}2i & \phantom{-}2i & \phantom{-}2i \\
\phantom{-}2i & \phantom{-}2i & \phantom{-}2i & \phantom{-}2i & \phantom{-}2i \\
-1+i & -1+i & -1+i & -1+i & -1+i \\
\phantom{-}2i & \phantom{-}2i & \phantom{-}2i & \phantom{-}2i & \phantom{-}2i \\
\phantom{-}2i & \phantom{-}2i & \phantom{-}2i & \phantom{-}2i & \phantom{-}2i
\end{bmatrix}

The third update, C 1, multiplies every number in column 1 by i.


\begin{bmatrix}
-2 & \phantom{-}2i & \phantom{-}2i & \phantom{-}2i & \phantom{-}2i \\
-2 & \phantom{-}2i & \phantom{-}2i & \phantom{-}2i & \phantom{-}2i \\
-1-i & -1+i & -1+i & -1+i & -1+i \\
-2 & \phantom{-}2i & \phantom{-}2i & \phantom{-}2i & \phantom{-}2i \\
-2 & \phantom{-}2i & \phantom{-}2i & \phantom{-}2i & \phantom{-}2i
\end{bmatrix}

The fourth update, R 4, multiplies every number in row 4 by i.


\begin{bmatrix}
-2 & \phantom{-}2i & \phantom{-}2i & \phantom{-}2i & \phantom{-}2i \\
-2 & \phantom{-}2i & \phantom{-}2i & \phantom{-}2i & \phantom{-}2i \\
-1-i & -1+i & -1+i & -1+i & -1+i \\
-2i & -2 & -2 & -2 & -2 \\
-2 & \phantom{-}2i & \phantom{-}2i & \phantom{-}2i & \phantom{-}2i
\end{bmatrix}

Finally, the sum of all the numbers in the grid is -19 + 25i. Thus, a = -19 and b = 25, and the correct output is -19 25.


Comments

There are no comments at the moment.