O(queries) approach, beats 99.52%.  Simple  and elegant.
Intuition\n Describe your first thoughts on how to solve this problem. \nWe don\'t need to find what the matrix might look like, just finding the final sum woul
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe don\'t need to find what the matrix might look like, just finding the final sum would suffice. Start from the last query, because we are overwritting so only last changes contributes to the final result.\n# Approach\n<!-- Describe your approach to solving the problem. -->\nInitialize two vectors for rows and columns with zero. It will help us to determine whether to make changes in that particular row/col or not. Also take two variables row_count and col_count which will help us to easily find how many rows and cols are still ain\'t overwritten. Only calculate for those rows which haven\'t been modified yet, which can be easily find by visited array for rows. \n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n O(queries) \n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(n)\n\n# Code\n```\nclass Solution {\npublic:\n    long long matrixSumQueries(int n, vector<vector<int>>& queries) {\n\n      long long sum=0;\n      //Visited array for rows and cols\n      vector<int> row(n,0);\n      vector<int> col(n,0);\n      //This will help us to find the number of rows/cols which has to modified for each query.\n      int row_count=n;\n      int col_count=n;\n      for(int i=queries.size()-1;i>=0;i--)\n      {\n       int type=queries[i][0];\n       int index=queries[i][1];\n       int value=queries[i][2];\n       if(type==1&&col[index]==0)\n       {\n        col[index]=1;\n        col_count--;\n        if(col_count<0) col_count=0;\n        sum+=row_count*value;\n       }\n       else if(type==0 && row[index]==0)\n       {\n        row[index]=1;\n        row_count--;\n        if(row_count<0) row_count=0;\n        sum+=col_count*value;\n       }\n      }\n      return sum;\n    }\n};\n```
['C++']