✅C++ || Simple and Easy to Understand🔥 Time: O(q) & Space: O(N)🔥
Intuition\nStart from reverse since at the end we end up at the latest configuration of the matrix. Afterwards if there is case of overwriting then we would not
# Intuition\nStart from reverse since at the end we end up at the latest configuration of the matrix. Afterwards if there is case of overwriting then we would not consider it as we starting from latest to initial configuration\n\n# Approach\nTake two unordered set one for row , and one for the column. Take two variable which would keep the count of number of rows and columns.\n\nAt any point while performing the row query add `val_at_the_row * (number of colums available)` and decrease the number of row by 1.\nAt any point while performing the col query add `val_at_the_col * (number of rows available)` and decrease the number of column by 1.\n\n# Complexity\n- Time complexity:\n$$O(q)$$ where q is the size of the query list\n\n- Space complexity:\n$$O(N)$$ where N is the number of rows and columns to store in unordered set\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        int nrow = n, ncol = n;\n        long long int sum = 0;\n        for (int i = q.size() - 1; i >= 0; i--)\n        {\n            int type = q[i][0];\n            int indx = q[i][1];\n            int val = q[i][2];\n            if (type == 0 && row.find(indx) == row.end())\n            {\n                sum += (ncol * val);\n                row.insert(indx);\n                nrow--;\n            }\n            else if (type == 1 && col.find(indx) == col.end())\n            {\n                sum += (nrow * val);\n                col.insert(indx);\n                ncol--;\n            }\n        }\n        return sum;\n    }\n};\n```\n![LEETCODE.png](https://assets.leetcode.com/users/images/86abe9b8-5bc8-4506-892c-6e61c18fe2c5_1685861372.3619711.png)\n
['Ordered Set', 'C++']