[Java/C++/Python] Reverse Queries
Why people down vote this thread without even a word or reason?\n# Please upvote this, thanks.\n\n# Intuition\n"overwriting any previous values."\nSo we don\'t 
# **Why people down vote this thread without even a word or reason?**\n# **Please upvote this, thanks.**\n\n# **Intuition**\n"overwriting any previous values."\nSo we don\'t know whether the current value will be overrote by later query.\nWe can iterate `queries` reversely,\nso we can make sure once the value filled,\nit won\'t be changed anymore.\n<br>\n\n# **Explanation**\n`col[i]` the values in the row with index `i`.\n`row[i]` the values in the col with index `i`.\n\n\nIterate `queries` reversely,\nif `col[i]` or `row[i]` already set,\nignore it.\notherwise set the value.\n\nIf `row[i]` is set,\nwe didn\'t set `n` values in matrix,\nsince there length of `col` have already been set,\nwe only fill in `n - col.size()` cell in `i`th row,\nso the total value is incresed by `v * (n - col.size())`.\nIf `col[i]` is set, similar case.\n\nFinally we return result.\n<br>\n\n# **Complexity**\nTime `O(queries)`\nSpace `O(n)`\n<br>\n\n**Java**\n```java\n    public long matrixSumQueries(int n, int[][] queries) {\n        Map<Integer, Integer> col = new HashMap<>(), row = new HashMap<>();\n        long res = 0;\n        for (int j = queries.length - 1; j >= 0; --j) {\n            int t = queries[j][0], i = queries[j][1], v = queries[j][2];\n            if (t == 0 && !col.containsKey(i)) {\n                col.put(i, v);\n                res += 1L * v * (n - row.size());\n            }\n            if (t == 1 && !row.containsKey(i)) {\n                row.put(i, v);\n                res += 1L * v * (n - col.size());\n            }\n        }\n        return res;\n    }\n```\n\n**C++**\n```cpp\n    long long matrixSumQueries(int n, vector<vector<int>>& queries) {\n        unordered_map<int, int> col, row;\n        long long res = 0;\n        for (int j = queries.size() - 1; j >= 0; --j) {\n            int t = queries[j][0], i = queries[j][1], v = queries[j][2];\n            if (t == 0 && !col.count(i)) {\n                col[i] = v;\n                res += v * (n - row.size());\n            }\n            if (t == 1 && !row.count(i)) {\n                row[i] = v;\n                res += v * (n - col.size());\n            }\n        }\n        return res;\n    }\n```\n\n**Python**\n```py\n    def matrixSumQueries(self, n: int, queries: List[List[int]]) -> int:\n        col, row = {}, {}\n        res = 0\n        for t,i,v in queries[::-1]:\n            if t == 0 and i not in row:\n                row[i] = v\n                res += v * (n - len(col))\n            if t == 1 and i not in col:\n                col[i] = v\n                res += v * (n - len(row))\n        return res\n```\n
['C', 'Python', 'Java']