[python] intuition explained very easily
Intuition\n- point is, i\'th query overwrites everything before it\n- from last query to first one, decide contained cell\'s values, add it to total sum, and fr
# Intuition\n- point is, i\'th query *overwrites* everything before it\n- from last query to first one, decide contained cell\'s values, add it to total sum, and freeze them\n\n# Approach\n- while iterating on queries from last, let\'s say we are on j\'th column of value v\n- if j\'th column had already been decided and frozen, continue\n- else, out of n cells (of j\'th row), exclude "number of already frozen rows" and then add the rest to total sum\n- freeze j\'th column and proceed\n\n- we have to keep track of ..\n    - set of frozen row indices\n    - set of frozen col indices\n    - total sum\n\n# Complexity\n- Time complexity: O(Query_len)\n- Space complexity: O(n)\n\n# Code\n```python3 []\nclass Solution:\n    def matrixSumQueries(self, n: int, queries: List[List[int]]) -> int:\n        # point is, i\'th query *overwrites* everything before it\n        # from last query to first one, decide contained cell\'s values, add it to total sum, and freeze them\n\n        # let\'s say we are on j\'th column of value v\n            # if j\'th column had already been decided and frozen, continue\n            # else, out of n cells (of j\'th row), exclude "number of already frozen rows" and then add the rest to total sum\n            # freeze j\'th column and proceed\n\n        # we have to keep track of \n            # set of frozen row indices\n            # set of frozen col indices\n            # total sum\n\n        frozen_rows = set()\n        frozen_cols = set()\n        ans = 0\n\n        for type_, index, val in queries[::-1]:\n            if type_==0:\n                freeze_set = frozen_rows\n                num_already_frozen = len(frozen_cols)\n            elif type_==1:\n                freeze_set = frozen_cols\n                num_already_frozen = len(frozen_rows)\n            else:\n                raise NotImplementedError\n            \n            if index in freeze_set:\n                continue\n            \n            ans += (val * (n - num_already_frozen))\n            freeze_set.add(index)\n        \n        return ans\n            \n        \n\n```
['Python3']