Greedy | C++ | Simple
Code\n\nclass Solution {\npublic:\n    long long matrixSumQueries(int n, vector<vector<int>>& q) {\n        \n        vector<long long>col(n),row(n);\n        \
# Code\n```\nclass Solution {\npublic:\n    long long matrixSumQueries(int n, vector<vector<int>>& q) {\n        \n        vector<long long>col(n),row(n);\n        \n        long long ans=0, ro=0,co=0;\n        \n        for(int i=q.size()-1;i>=0;i--){\n            \n            int type = q[i][0];\n            int index = q[i][1];\n            int val = q[i][2];\n            \n            if(type==0){\n                if(row[index]){\n                    q[i][0]=2;\n                }\n                else {\n                    row[index] = 1;\n                }\n            }\n            else{\n                if(col[index]){\n                    q[i][0]=2;\n                }\n                else {\n                    col[index] = 1;  \n                }              \n            }\n            \n        }\n        for(int i=0;i<q.size();i++){\n            \n            int type = q[i][0];\n            int index = q[i][1];\n            int val = q[i][2];\n            \n            if(type==0){   \n                \n                ro+=val;\n                ans+= val*n;\n                ans-=co; \n                \n            }\n            if(type==1){\n                \n                co+=val;\n                ans+=val*n;\n                ans-=ro;\n                \n            }\n        }\n        return ans;\n    }\n};\n```
['Greedy', 'C++']