[C++] Only last replacement matters!
\n// rows -> number of rows visited \n// cols->number of columns visited\n// row X cols intersection can change the values \n// for row : r columns can change r
```\n// rows -> number of rows visited \n// cols->number of columns visited\n// row X cols intersection can change the values \n// for row : r columns can change r times val of row , so just we count till now: how many number of col visited that affects the values in row (intially n values in that row)\n// same for column \n// visited array to maintain the index of rows/column visited to avoid the repeated replacement of same column /row\n// only last replacement of particular row/column matters! and give contribution to sum\nclass Solution {\npublic:\n    long long matrixSumQueries(int n, vector<vector<int>>& q) \n    {\n        long long ans=0;\n        int row=0;\n        int col=0;\n        vector<bool> visrow(n,0),viscol(n,0);\n        for(int i=q.size()-1;i>=0;i--)\n        {\n            int type=q[i][0];\n            int val=q[i][2];\n            if(type==0)\n            {\n                int rowind=q[i][1];\n                if(visrow[rowind]==0)\n                {\n                ans+=1LL*val*(n-col);\n                row++;\n                    visrow[rowind]=1;\n                }\n                \n            }\n            else\n            {\n                \n                int colind=q[i][1];\n                if(viscol[colind]==0)\n                {\n                    ans+=1LL*val*(n-row);\n                col++;\n                    viscol[colind]=1;\n                }\n                \n            }\n            \n        }\n        return ans;\n    }\n};\n```
['Hash Table', 'Greedy', 'C', 'Matrix']