Python Solution in O(n). With Easy explanation.
Greedy Approach\nThinking straight forward and modifying a matrix of nn based on query.\nIf q is then length of queries list then time complexity will be O(n * 
# Greedy Approach\nThinking straight forward and modifying a matrix of n*n based on query.\nIf q is then length of queries list then time complexity will be O(n * m).\n\n\n# Optimal Approach, Implementing queries in reverse.\nn = 3\nQuery = [[0,0,1],[1,2,2],[0,2,3],[1,0,4]]\n\nIt\'s obvious that the last query will override it\'s value to complete row (if type == 0) or column (if type == 1).\n\nSimilarly second last query will override it\'s value to complete row or column, except the elements updated by previous query.\n\nIf both last and second last query\'s type == 0 and index is not same  then  result += n*val for both queries. Same if type == 1.\n\nIf one query if of type == 1 and other is of type == 0. That mean for second last query definitely one element will be left as it\'s as it was updated in the previous query.\n\nWe can say that if current query type == 0 and there are c queries with type == 1 before the current index then the new elements added by the query will be val*(n-c).\n\nOne thing we should make sure that query of type == 0 was never implemented before current query.\n\nSo we will create,\n1. colVisited = set()\n2. rowVisited = set()\n3. Itterate the queries from n-1 to 0\n4. If type == 0 and index not in rowVisited: Then update rowVisited and add val*(n-len(colVisited)) in your result\n5.  If type == 1 and index not in colVisited: Then update colVisited and add val*(n-len(rowVisited)) in your result\n6. Return Result\n\n# Complexity\n- Time complexity:\nO(q), where q is the length of queries \n\n- Space complexity:\nO(n), where matrix dimension is n*n\n\n# Code\n```\nclass Solution:\n    def matrixSumQueries(self, n: int, queries: List[List[int]]) -> int:\n\n        colVisited = set()\n        rowVisited = set()\n        result = 0\n        for i in reversed(range(len(queries))):\n            t,index,val = queries[i]\n            if t == 0 and index not in rowVisited:\n                rowVisited.add(index)\n                result += val*(n-len(colVisited))\n            elif t == 1 and index not in colVisited:\n                colVisited.add(index)\n                result += val*(n-len(rowVisited))\n        return result\n        \n```
['Python3']