#C5765. Sparse Matrix Operations

    ID: 49450 Type: Default 1000ms 256MiB

Sparse Matrix Operations

Sparse Matrix Operations

You are required to implement a sparse matrix data structure that supports the following operations:

  • set: Set the value at a given position.
  • get: Get the value at a given position.
  • transpose: Compute the transpose of the matrix. Note that if the matrix has dimensions \(r \times c\), its transpose is of dimension \(c \times r\).
  • add: Add another sparse matrix (of the same dimensions) to the current matrix.
  • print: Print all nonzero entries.

The matrix should internally store only the nonzero values. When a value is set to zero, any stored record for that position should be removed.

You will be given the initial dimensions of the matrix and a series of commands. Process the commands sequentially. For each get command, output the retrieved value on a separate line. For each print command, output the count of nonzero entries on a separate line, followed by each nonzero entry (row, column, and value) in ascending order of row and then column, each on a new line.

inputFormat

The first line contains two integers rows and cols \((1 \leq rows, cols \leq 10^3)\), representing the dimensions of the matrix.

The second line contains an integer Q, the number of commands. Each of the following Q lines contains a command in one of the following formats:

  • set row col value: Set the cell at \(\text{row}\) and \(\text{col}\) to value.
  • get row col: Output the value at \(\text{row}\) and \(\text{col}\).
  • transpose: Replace the current matrix with its transpose.
  • add k: Followed by k lines, each containing row col value, representing a sparse matrix to be added to the current matrix. The addition is performed element-wise.
  • print: Print the current matrix's nonzero entries. First output an integer n (the number of nonzero entries), then output n lines each in the format row col value in ascending order of row and then column.

All indices are 0-indexed.

outputFormat

For each get command, output the queried value on a separate line.

For each print command, first output an integer indicating the count of nonzero cells. If the count is greater than 0, then output that many lines, each containing three integers: row, column, and value, in ascending order (first by row, then by column).

## sample
5 5
6
set 2 3 10
get 2 3
transpose
get 3 2
add 1
2 3 5
get 2 3
10

10 5

</p>