C++ Solution : Greedy || Brute Force + Optimized
Intuition\n Process the queries in reversed order because if you process from forward, you will have to update matrix each time..\n By processing in reverse , y
# Intuition\n* Process the queries in reversed order because if you process from forward, you will have to update matrix each time..\n* By processing in reverse , you make sure that no further changes can be made on that row or column.\n\n# Approach\n* Here iterate in reverse order & Keep track number of rows and number of cols that can be assigned a value, using row counter and column counter.\n* Add to answer $$(val * remainingRow)$$ when col operation is performed & add to answer $$(val * remainingCol)$$ when row operation is performed.\n* Decreament remaining $$row$$ when row operation is performed &\nreamining $$col$$ when col operation is performed\n* Note : The trick is to when ever we perform a $$row$$ operation then number of column remaining for next $$col$$ operation 1 less and same is the case for col operation.\n\n\n# Code\n\n\n## Brute Force - TLE\n* Time Complexity - $$O(N^2)$$\n* Space Complexity - $$O(N^2)$$\n```\nclass Solution {\npublic:\n    long long matrixSumQueries(int n, vector<vector<int>>& queries) {\n        if(queries.size() == 0){\n            return 0;\n        }\n        \n        vector<vector<int>> ans(n, vector<int>(n, 0));\n        \n        for(auto &q : queries){\n            int t = q[0];\n            int i = q[1];\n            int v = q[2];\n            \n            //col\n            if(t == 1){\n                for(int k = 0; k < n; k++){\n                    ans[k][i] = v;\n                }\n            }\n            //row\n            else{\n                for(int k = 0; k < n; k++){\n                    ans[i][k] = v;\n                }\n            }\n        }\n        \n        long long res = 0;\n        \n        for(int i = 0; i < n; i++){\n            for(int j = 0; j < n; j++){\n                res += ans[i][j];\n            }\n        }\n        \n        return res;\n    }\n};\n```\n\n## Optimized Approach\n* Time Complexity - $$O(N)$$\n* Space Complexity - $$O(N)$$\n```\nclass Solution {\npublic:\n    long long matrixSumQueries(int n, vector<vector<int>>& queries) {\n        \n        int m = queries.size();\n        unordered_set<int> rowMap;\n        unordered_set<int> colMap;\n        \n        long long ans = 0;\n        \n        long long rowCounter = 0;\n        long long colCounter = 0;\n        \n        \n        for(int i = m - 1; i >= 0; i--){\n            int t = queries[i][0];\n            int idx = queries[i][1];\n            int val = queries[i][2];\n            \n            if(t == 1){\n                if(colMap.count(idx)){\n                    continue;\n                }\n                else{\n                    colMap.insert(idx);\n                    ans += (n - rowCounter) * val;\n                    colCounter++;\n                }\n            }\n            else if(t == 0){\n                if(rowMap.count(idx)){\n                    continue;\n                }\n                else{\n                    rowMap.insert(idx);\n                    ans += (n - colCounter) * val;\n                    rowCounter++;\n                }\n            }\n        }\n        \n        return ans;\n    }\n};\n```
['Greedy', 'Matrix', 'Ordered Set', 'C++']