#K84262. Spreadsheet Evaluation

    ID: 36381 Type: Default 1000ms 256MiB

Spreadsheet Evaluation

Spreadsheet Evaluation

You are given a spreadsheet with cells identified by their row and column numbers. The spreadsheet supports two types of operations:

  • SET r c value: Sets the cell at row r and column c to the specified value. The value can be an integer or a formula. A formula is a string that begins with '=' and may include references to other cells, using the format \(R\text{row}C\text{col}\), and basic arithmetic operators \(+, -, *, /\).
  • GET r c: Retrieves the evaluated integer value of the cell at row r and column c.

For formulas, replace every cell reference \(RrCc\) with the evaluated integer value of that cell, then evaluate the resulting arithmetic expression. You may assume that formulas do not contain parentheses and that multiplication and division have higher precedence than addition and subtraction. Note that division is integer division.

Your task is to implement the spreadsheet so that it processes the commands from standard input and outputs the results for each GET command to standard output.

inputFormat

The first line contains two integers \(r\) and \(c\), representing the number of rows and columns of the spreadsheet.

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 r c value: Set the cell at row \(r\) and column \(c\) with the given value. The value is either an integer or a formula starting with '=' (with no spaces).
  • GET r c: Get the evaluated integer value of the cell at row \(r\) and column \(c\).

All input is read from standard input (stdin).

outputFormat

For each GET command, output a single line containing the evaluated integer value of the specified cell. All output is printed to standard output (stdout).

## sample
3 3
18
SET 1 1 5
SET 1 2 3
SET 1 3 =R1C1+R1C2
SET 2 1 =R1C3
SET 2 2 2
SET 2 3 =R2C1*R2C2
SET 3 1 =R2C3-4
SET 3 2 =R3C1/2
SET 3 3 4
GET 1 1
GET 1 2
GET 1 3
GET 2 1
GET 2 2
GET 2 3
GET 3 1
GET 3 2
GET 3 3
5

3 8 8 2 16 12 6 4

</p>