O(n) solution : very easy to understand
Intuition\n Describe your first thoughts on how to solve this problem. \nJust think this question as if you are trying to color a square paper with different co
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nJust think this question as if you are trying to color a square paper with different color paint.\nIn each stroke you color some part of the paper.\nAfter you are done with painting the only the last paint would be visible.\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nUsing the above intution we can say that the row or colum which was last changed will be persisted while other will be lost.\nSo, for each query we need to find how many cells will be remain unchanged after the finaly query.\nTo achive this we need to maintain two boolean array represting which row or col has been used. Apart from that we need to keep track of how many row or col has been used.\n\ntc -> no. of cols used\ntr-> no. of rows used\nFor each query : q\n    Type 1 : val(i) * (n-tc)\n    Type 2  : val(i) * (n-tr)\n\n# Complexity\n- Time complexity: O( queries.size() )\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->  \n\n- Space complexity: O(n) -> to store the bool array\n<!-- Add your space complexity here, e.g. $$O(n)$$ --> \n\n\n# Code\n```\nclass Solution {\npublic:\n    long long matrixSumQueries(int n, vector<vector<int>>& queries) {\n        reverse(queries.begin(),queries.end());\n        \n        vector<bool> rcheck(n,false),ccheck(n,false);\n        long long ans = 0, tr = 0, tc = 0;\n        for( auto &q : queries ){\n            int tq = q[0];\n            if( tq==0 ){\n                int rn = q[1];\n                int val = q[2];\n                \n                if( rcheck[rn] )\n                    continue;\n                \n                rcheck[rn] = true;\n                tr++;\n                ans += val * (n-tc);\n            }\n            else {\n                int cn = q[1];\n                int val = q[2];\n                \n                if( ccheck[cn] )\n                    continue;\n                \n                ccheck[cn] = true;\n                tc++;\n                ans += val * (n-tr);\n            }\n        }\n        \n        return ans;\n    }\n};\n```
['Hash Table', 'Matrix', 'C++']