Only Logic | Explanation ✅
Intuition\n Describe your first thoughts on how to solve this problem. \n1) The brute force requires every cell to be traversed so it will cause TLE (n^2).\n2) 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n1) The brute force requires every cell to be traversed so it will cause TLE (n^2).\n2) Since the order of queries also matters, so we can\'t simply store the change and result them all later.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nSuppose if a cell is made under two or more updations, then the final value will surely update the previous and it will be the answer. So if we approach from backwards (in the queries array) and then check if the current cell is already updated then it means we don\'t need to sum the current value.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n    O(queries.length())\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n    O(n)\n\n# Code\n```\nclass Solution {\npublic:\n    long long matrixSumQueries(int n, vector<vector<int>>& queries) {\n        \n        vector<int> rowV(n, -1), colV(n, -1);\n        int rowC, colC;\n        \n        //It is done only to make the backward iteration simpler.\n        reverse(queries.begin(), queries.end());\n        \n        long long sum=0;\n        for(auto q : queries) {\n            if((q[0] == 0 && rowV[q[1]] != -1) || (q[0] == 1 && colV[q[1]] != -1)) {\n                continue;        \n            }\n            else {\n                if(q[0] == 0) {\n                    sum += (n-colC)*q[2];\n                    rowV[q[1]] = q[2];\n                    rowC++;\n                }\n                else {\n                    sum += (n-rowC)*q[2];\n                    colV[q[1]] = q[2];\n                    colC++;\n                }\n                \n            }\n        }\n        \n        return sum;\n    }\n};\n```
['C++']