O(N) - Sum of Matrix After Queries Optimized Code
Intuition\n Describe your first thoughts on how to solve this problem. \nWe will iterate the queries from last so that we will only change a row/col only once. 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe will iterate the queries from last so that we will only change a row/col only once. and we will keep a counter of visited rows and cols to add the (number of unvisited rows/cols * value) to answer at that particular row or col.\n\n\n# Code\n```\nclass Solution {\n    public long matrixSumQueries(int n, int[][] queries) {\n        long ans=0;\n        \n        // VISITED ARRAY\n        int[] vrow =new int[n];\n        int[] vcol =new int[n];\n        \n        // COUNT OF VISITED ROWS AND COLUMNS\n        int c=0;\n        int r=0;\n        \n        for(int i=queries.length-1;i>-1;i--){\n            \n            if(queries[i][0]==0 && vrow[queries[i][1]]==0){\n                int val = queries[i][2];\n                vrow[queries[i][1]]=1;\n                r++;\n                ans+=(n-c)*val;\n                \n            }else if(queries[i][0]==1 && vcol[queries[i][1]]==0){\n                int val = queries[i][2];\n                vcol[queries[i][1]]=1;\n                c++;\n                ans+=(n-r)*val;\n            }\n        }\n        return ans;\n        \n    }\n}\n```
['Java']