Reverse Iteration | Easy to Understand | C++
Intuition\n Describe your first thoughts on how to solve this problem. \nSince we are overwriting the queries its easy to manage them in reverse order.\n# Appro
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nSince we are overwriting the queries its easy to manage them in reverse order.\n# Approach\n<!-- Describe your approach to solving the problem. -->\nIterate the queries in reverse fashion and keep track of the row and column which are used or for which a query is already encountered before and also keep a track of at each step how many rows and cols are modified. We need to do this because (lets say we have already made a column as any value say 4, if we encounter query to perform on a row we know that one of its column is already set we cant modify this so we just add in our ans the values of the remaing columns).\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(NlogN) -> iterating through the queries for once and insertion in set takes logn\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(N) -> space due to use of set\n# Code\n```\nclass Solution {\npublic:\n    long long matrixSumQueries(int n, vector<vector<int>>& q) {\n        \n        long long ans = 0;\n        \n        int col = 0;\n        int row = 0;\n        \n        set<pair<int, int>> st;\n        \n        for(int i = q.size() - 1; i >= 0; i--){\n            \n            long long mulRow = row;\n            long long mulCol = col;\n\n            int type = q[i][0];\n            int ind = q[i][1];\n            int val = q[i][2];\n            \n            if(st.count({type, ind}) == 0){\n                ans += (n * val) - ((type == 0 ? mulRow : mulCol) * val);\n                if(type == 1) row++;\n                else col++;\n            }\n            \n            st.insert({type, ind});\n            \n        }\n        \n        return ans;\n        \n    }\n};\n```
['Math', 'Matrix', 'C++']