JavaScript ⤬ Time O(q) ⤬ Space O(n) ⤬ 3 solutions ⤬ 8 lines of code in the shortest one
Approach\n1. Iterate from the end and Keep track number of rows and number of cols that can be assigned a value.\n2. Add to answer val * row rest when col opera
# Approach\n1. Iterate from the end and Keep track number of rows and number of cols that can be assigned a value.\n2. Add to answer val * row rest when col operation is performed and add to answer val * col rest when row operation is performed.\n3. Decrement row rest when row operation is performed and col rest when col operation is performed.\n\n# With Arrays\n```javascript\nvar matrixSumQueries = function(n, queries) {\n  const seen = [\n    new Uint8Array(n),\n    new Uint8Array(n),\n  ];\n  const rest = [n, n];\n  let ans = 0;\n\n  for (let i = queries.length-1; i >= 0; i--) {\n    const [type, index, val] = queries[i];\n    if (seen[type][index] === 0) {\n      seen[type][index] = 1;\n      ans += rest[1-type] * val;\n      rest[type]--;\n    }\n  }\n  \n  return ans;\n};\n```\n# Or shorter with Sets\n```\nvar matrixSumQueries = function(n, queries) {\n  const seen = [new Set(), new Set()];\n  let ans = 0;\n\n  for (let i = queries.length-1; i >= 0; i--) {\n    const [type, index, val] = queries[i];\n    if (!seen[type].has(index)) {\n      seen[type].add(index);\n      ans += (n-seen[1-type].size) * val;\n    }\n  }\n  \n  return ans;\n};\n\n```\n# Or the shortest with Sets and ReduceRight\n```\nvar matrixSumQueries = function(n, queries) {\n  const seen = [new Set(), new Set()];\n  return queries.reduceRight((sum, [type, index, val]) => \n    !seen[type].has(index)\n      ? (seen[type].add(index), sum + (n-seen[1-type].size) * val)\n      : sum\n  , 0);\n};\n```
['JavaScript']