Simple and Easy explanation
Intuition\nThe intuition behind the solution is to efficiently handle matrix sum queries by optimizing the processing of row and column operations. By iterating
# Intuition\nThe intuition behind the solution is to efficiently handle matrix sum queries by optimizing the processing of row and column operations. By iterating through the queries in reverse order and keeping track of processed rows and columns using sets, we can avoid redundant calculations and improve overall efficiency. \n\n# Approach\nThe approach involves iterating through the matrix sum queries in reverse order. For each query, we extract the type (row or column operation), index, and value. If it\'s a row operation (type 0) and the row hasn\'t been processed before, we add the specified value to all elements in the row and update the sum. Similarly, for column operations (type 1), we perform the corresponding operations on columns. We use sets \'row\' and \'col\' to keep track of processed rows and columns, and variables \'ro\' and \'co\' to represent the remaining rows and columns. \n\n# Complexity\n- Time complexity:\nThe time complexity of the solution is O(q), where q is the number of matrix sum queries. Each query is processed once, and the iteration is performed in reverse order.\n\n- Space complexity:\nThe space complexity is O(r + c), where r is the number of rows and c is the number of columns. This accounts for the space used by the sets \'row\' and \'col\', which store processed rows and columns.\n\n# Code\n```\nclass Solution\n{\npublic:\n    long long matrixSumQueries(int n, vector<vector<int>> &q)\n    {\n        unordered_set<int> row, col;\n        long long sum = 0;\n        int ro = n, co = n;\n        for (int i = q.size() - 1; i >= 0; --i)\n        {\n            int type = q[i][0];\n            int index = q[i][1];\n            int val = q[i][2];\n            if (type == 0 and row.find(index) == row.end())\n            {\n                sum += (co * val);\n                row.insert(index);\n                ro--;\n            }\n            else if (type == 1 and col.find(index) == col.end())\n            {\n                sum += (ro * val);\n                col.insert(index);\n                co--;\n            }\n        }\n        return sum;\n    }\n};\n```
['C++']