Java | Easy solution | 15 lines
Complexity\n- Time complexity: O(m) where m = queries.length\n Add your time complexity here, e.g. O(n) \n\n- Space complexity: O(n)\n Add your space complexity
# Complexity\n- Time complexity: $$O(m)$$ where `m = queries.length`\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n  public long matrixSumQueries(int n, int[][] queries) {\n    var rows = new boolean[n];\n    var cols = new boolean[n];\n    int rowCnt = n, colCnt = n;\n    var sum = 0L;\n    \n    for (var i = queries.length - 1; i >=0 && rowCnt > 0 && colCnt > 0; i--) {\n      if (queries[i][0] == 0) {\n        if (rows[queries[i][1]]) continue;\n        \n        rows[queries[i][1]] = true;\n        rowCnt--;\n        sum += colCnt * queries[i][2];\n      } else {\n        if (cols[queries[i][1]]) continue;\n        \n        cols[queries[i][1]] = true;\n        colCnt--;\n        sum += rowCnt * queries[i][2];\n      }\n    }\n    return sum;\n  }\n}\n```
['Matrix', 'Java']