Strict Test cases 😶.3 solutions. O(n) Explanation
Intuition:\nGiven the input matrix and a set of queries, we need to calculate the sum of values in the matrix based on the queries. The queries specify whether 
Intuition:\nGiven the input matrix and a set of queries, we need to calculate the sum of values in the matrix based on the queries. The queries specify whether to calculate row sums or column sums, along with the indices and values to be added or subtracted. To efficiently calculate the sum, we can process the queries in reverse order and keep track of the processed rows and columns.\n\nApproach:\n\nInitialize the cumulative sum variable sumi to 0.\nCreate sets row and col to keep track of processed rows and columns, respectively.\nIterate through the queries in reverse order using a for loop.\nFor each query, check the value of a (0 for row sum, 1 for column sum).\nIf a is 0 (row sum):\nCheck if the row index b is already in the row set.\nIf not, add the row index to the row set and update sumi by adding c * (n - len(col)), where c is the value to be added or subtracted, and (n - len(col)) represents the number of unprocessed columns.\nIf a is 1 (column sum):\nCheck if the column index b is already in the col set.\nIf not, add the column index to the col set and update sumi by adding c * (n - len(row)), where (n - len(row)) represents the number of unprocessed rows.\nReturn the final value of sumi as the result.\nComplexity:\n\nTime complexity: The algorithm iterates through the queries once in reverse order, resulting in a time complexity of O(q), where q is the number of queries.\nSpace complexity: The algorithm uses additional space to store the row and col sets, which can have at most n elements (assuming an n x n matrix). Therefore, the space complexity is O(n).\n\n# Code\nACCEPTED\n```\nfrom typing import List\n\nclass Solution:\n    def matrixSumQueries(self, n: int, queries: List[List[int]]) -> int:\n        sumi = 0\n        row = set()\n        col = set()\n\n        for a, b, c in reversed(queries):\n            if a == 0:\n                if b not in row:\n                    row.add(b)\n                    sumi += c * (n - len(col))\n            else:\n                if b not in col:\n                    col.add(b)\n                    sumi += c * (n - len(row))\n        \n        return sumi\n\n\n\n```\n\n![image.png](https://assets.leetcode.com/users/images/96e80b2b-bf6f-4773-9493-38541adeafe0_1685851459.9388924.png)\n```\n\nclass Solution:\n    def matrixSumQueries(self, n: int, queries: List[List[int]]) -> int:\n        sumi=0\n        row=[0]*n\n        col=[0]*n\n        \n        for i in range(len(queries)-1,-1,-1):\n            a,b,c=queries[i]\n            if a==0:\n                if row[b]!=1:\n                    row[b]=1\n                    sumi+=c*(n-sum(col))\n                \n            else:\n                if col[b]!=1:\n                    col[b]=1\n                    sumi+=c*(n-sum(row))\n        return sumi\n```\n![image.png](https://assets.leetcode.com/users/images/206dfc2c-7828-4a35-b2ba-56bcec1519d4_1685851567.7818458.png)\n\n```\nclass Solution:\n    def matrixSumQueries(self, n: int, queries: List[List[int]]) -> int:\n        d=defaultdict(int)\n        \n        for a,b,c in queries[::-1]:\n            if a==0:\n                i=0\n                while 0<=i<n:\n                    if (b,i) not in d:\n                        d[(b,i)]=c\n                    i+=1\n            else:\n                i=0\n                while 0<=i<n:\n                    if (i,b) not in d:\n                        d[(i,b)]=c\n                    i+=1\n        # print(d)\n        return sum(d.values())\n```
['Python', 'Python3']