Java reverse loop
\n# Approach\n Describe your approach to solving the problem. \nYou will see the set of rows just keep overwriting previous resuls. \nYou just need to find out 
\n# Approach\n<!-- Describe your approach to solving the problem. -->\nYou will see the set of rows just keep overwriting previous resuls. \nYou just need to find out the last operation of each cols and rows.\n\nKeep in mind:\nWhen you check cols, the vals in cols will be overwritten by later query of rows, and vice versa. see comment like "// the value in rows.size() cells is already added."\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nTC: O(N)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nSC: O(N)\n# Code\n```\npublic long matrixSumQueries(int n, int[][] qs) {\n    Set<Integer> rows = new HashSet<>(), cols = new HashSet<>();\n    long res = 0;\n    for (int i = qs.length - 1; i >= 0; i--) {\n        int[] q = qs[i];\n        if (q[0] == 0 ) { // rows\n            // the value in cols.size() cells is already added.\n            if (rows.add(q[1])) res += q[2] * (n - cols.size());\n        } else if (cols.add(q[1])) { // cols\n            // the value in rows.size() cells is already added.\n            res += q[2] * (n - rows.size());\n        }\n        if (rows.size() == n && cols.size() == n) break;\n    }\n    return res;\n}\n```
['Java']