Reverse and Hashset
Intuition\n Describe your first thoughts on how to solve this problem. \nIgnore the rows or columns that get set later and calculate the result by remembering h
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nIgnore the rows or columns that get set later and calculate the result by remembering how many rows or cols have been set.\n# Approach\n<!-- Describe your approach to solving the problem. -->\nFirst you reverse the queries because the later queries will overwrite the former ones.\nYou keep in mind which rows or columns have already been set and ignore those.\nYou remember how many rows or columns have been set, so that you know how many numbers will be overwritten by the next query.\nFor a query on a row you calculate this:\nresult += query_value * (n - cols_set);\nFor a query on a column you calculate this:\nresult += query_value * (n - rows_set);\n\n# Complexity\n- Time complexity: N is the amount of the queries O(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: N is the size of the matrix O(N)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nlong long matrixSumQueries(int n, vector<vector<int>>& queries)\n{\n\treverse(queries.begin(), queries.end());\n\tvector<bool> queried_rows(n, false);\n\tvector<bool> queried_cols(n, false);\n\tint rows_set = 0;\n\tint cols_set = 0;\n\tlong long result = 0;\n\tfor (const vector<int>& query : queries) {\n\t\tconst int query_type = query[0];\n\t\tconst int query_index = query[1];\n\t\tconst int query_value = query[2];\n\t\tif (query_type == 0) {\n\t\t\tif (queried_rows[query_index] == false) {\n\t\t\t\t++rows_set;\n\t\t\t\tresult += query_value * (n - cols_set);\n\t\t\t\tqueried_rows[query_index] = true;\n\t\t\t}\n\t\t}\n\t\tif (query_type == 1) {\n\t\t\tif (queried_cols[query_index] == false) {\n\t\t\t\t++cols_set;\n\t\t\t\tresult += query_value * (n - rows_set);\n\t\t\t\tqueried_cols[query_index] = true;\n\t\t\t}\n\t\t}\n\t}\n\treturn result;\n}\n```
['C++']