[C++] Single Pass from Reverse, Hash Tables
Complexity\n- Time complexity: O(q)\n- Space complexity: O(n)\n\n# Code\n\nclass Solution {\npublic:\n    long long matrixSumQueries(int n, vector<vector<int>>&
# Complexity\n- Time complexity: $$O(q)$$\n- Space complexity: $$O(n)$$\n\n# Code\n```\nclass Solution {\npublic:\n    long long matrixSumQueries(int n, vector<vector<int>>& queries) {\n        long long sum = 0;\n        unordered_set<int> rows, cols;\n        for (auto it = queries.rbegin(); it != queries.rend(); it++) {\n            auto& query = *it;\n            if (query[0] == 0) {\n                if (rows.find(query[1]) == rows.end()) {\n                    rows.insert(query[1]);\n                    sum += query[2] * (n - cols.size());\n                }\n\n            } else {\n                if (cols.find(query[1]) == cols.end()) {\n                    cols.insert(query[1]);\n                    sum += query[2] * (n - rows.size());\n                }\n            }\n        }\n        return sum;\n    }\n};\n```
['Array', 'Hash Table', 'C++']