Easy, clean Python3🐍 Solution|| Beats 92%🔥
Intuition\n Describe your first thoughts on how to solve this problem. \nThis problem can be solved even with simulating the process but that approach is too sl
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThis problem can be solved even with simulating the process but that approach is too slow. So, what we can do is maintain sets of rows and columns changed and start with final queries (i.e., the queries whose changes don\'t get modified again)\n# Approach\n<!-- Describe your approach to solving the problem. -->\nWe start by iterating the queries in reverse order as the final query will never get overrridden and is final. We keep a count of number of rows and columns changed so far in order to keep track of how many cells are modified at each query.\nWe also keep track of all the changed rows and columns (using sets) so that once a row or column is modified, it isn\'t modified again (as we are traversing in reverse order, the later changes are actually ones which were supposed to happen earlier but were overridden by later queries)\nThe ans variable is updated with each query.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$O(n)$$ where n is the size od queries list\n\n# Code\n```\nclass Solution:\n    def matrixSumQueries(self, n: int, queries: List[List[int]]) -> int:\n        ans = 0 \n        rowchng, colchng = 0,0\n        row, col = set(), set()\n        for i in range(len(queries)-1,-1,-1):\n            typ, ind, val = queries[i]\n            if typ == 0:\n                if ind not in row:\n                    row.add(ind)\n                    ans += (n-colchng)*val\n                    rowchng += 1\n            else:\n                if ind not in col:\n                    ans += (n-rowchng)*val\n                    colchng += 1\n                    col.add(ind)\n        return ans\n```
['Array', 'Hash Table', 'Matrix', 'Python3']