Traverse Queries in Reverse || C++
Complexity\n- Time complexity :  O(Q)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity :  O(N)\n Add your space complexity here, e.g. O(n) \n\n
# Complexity\n- Time complexity :  $$O(Q)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity :  $$O(N)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    long long matrixSumQueries(int n, vector<vector<int>>& queries) {\n        int q = queries.size();\n        long long ans = 0;\n        unordered_set<int> visRow;\n        unordered_set<int> visCol;\n        for(int i=q-1 ; i>=0 ; i--) {\n            int type = queries[i][0];\n            int val = queries[i][2];\n            if(queries[i][0] == 1) {\n                int col = queries[i][1];\n                if(visCol.count(col)) continue;\n                \n                ans += 1ll*(n-visRow.size())*val;\n                visCol.insert(col);\n            }\n            \n            else {\n                int row = queries[i][1];\n                if(visRow.count(row)) continue;\n                \n                ans += 1ll*(n-visCol.size())*val;\n                visRow.insert(row);\n            }\n        }\n        return ans;\n    }\n};\n```
['C++']