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×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

1N,Q5×105
1jN

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

Copy
5 4
R 3
I
C 1
R 4

Sample Output

Copy
-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.

[iiiiiiiiiiiiiiiiiiiiiiiii]

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

[iiiiiiiiii11111iiiiiiiiii]

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

[2i2i2i2i2i2i2i2i2i2i1+i1+i1+i1+i1+i2i2i2i2i2i2i2i2i2i2i]

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

[22i2i2i2i22i2i2i2i1i1+i1+i1+i1+i22i2i2i2i22i2i2i2i]

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

[22i2i2i2i22i2i2i2i1i1+i1+i1+i1+i2i222222i2i2i2i]

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.