Easy to understand solution, with detailed explanations.
Intuition\n\nThe problem involves calculating the sum of elements in a matrix after applying a series of queries. Each query either updates an entire row or an 
## Intuition\n\nThe problem involves calculating the sum of elements in a matrix after applying a series of queries. Each query either updates an entire row or an entire column by adding a specified value to every element in that row or column. The challenge is to find an efficient way to compute the final sum without actually performing all the updates, which would be computationally expensive, especially for large matrices.\n\n## Approach\n\nThe solution leverages the observation that each row and each column only need to be updated once to reflect the final sum, regardless of how many queries target them. This is because the sum of a row or column after multiple updates can be calculated directly if we know the total value added to it. The solution employs two sets, `seenRows` and `seenColumns`, to track which rows and columns have been updated. It iterates through the queries in reverse order, ensuring that for each row or column, only the last (or in this case, the first in reverse order) update is applied. This is based on the insight that only the last update matters for the final sum calculation, as earlier updates are overwritten.\n\n1. Initialize `seenRows` and `seenColumns` sets to keep track of which rows and columns have been updated.\n2. Iterate through the queries in reverse order.\n3. For each query, check the type (`0` for row, `1` for column) and whether the row or column has been seen.\n   - If the query is for an unseen row, add the value times `(n - seenColumns.size)` to the result. This accounts for the value added to each element in the row, excluding any columns that have been updated.\n   - Similarly, for an unseen column, add the value times `(n - seenRows.size)` to the result.\n4. Add the index of the updated row or column to the respective set to mark it as seen.\n5. Return the accumulated result as the final sum after all queries.\n\n## Complexity\n\n- **Time Complexity:** O(q), where `q` is the number of queries. The algorithm iterates through the list of queries once.\n- **Space Complexity:** O(n), where `n` is the size of the matrix. This space is used to store the `seenRows` and `seenColumns` sets. In the worst case, each row and each column will be stored once.\n\n# Code\n```\nfunction matrixSumQueries(n: number, queries: number[][]): number {\n    const seenRows = new Set<number>();\n    const seenColumns = new Set<number>();\n    let result = 0;\n    for (let i = queries.length - 1; i >= 0; i--) {\n        const [type, index, val] = queries[i];\n        if (type === 0 && !seenRows.has(index)) {\n            result += (n - seenColumns.size) * val;\n            seenRows.add(index);\n        } else if (type === 1 && !seenColumns.has(index)) {\n            result += (n - seenRows.size) * val;\n            seenColumns.add(index);\n        }\n    }\n\n    return result;\n}\n```
['TypeScript']