[C++] O(N) solution without using matrix
High level intuition:\n1. If you traverse the queries in reverse order, once a spot is occupied, it cannot be overtaken by an earlier query.\n2. Do not use matr
High level intuition:\n1. If you traverse the queries in reverse order, once a spot is occupied, it cannot be overtaken by an earlier query.\n2. Do not use matrices since O(n * n) in this case will exceed the time limit (10^8).\n3. For each element in the matrix, there is only one row and one column that can mutate it.\n4. When you fill up a row, all columns will have lost the opportunity to fill up one element. Vice versa for filling up columns.\n5. Therefore, You can keep a total count of number of elements that can still be filled for all columns (and for all rows).\n6. Use a visited array for rows and columns to ensure no earlier query can override the values in that row/column.\n\n```\nclass Solution {\npublic:\n    long long matrixSumQueries(int n, vector<vector<int>>& queries) {\n        long long ans = 0;\n        vector<int> row(n, 1), col(n, 1);\n        int rowc = n, colc = n;\n        reverse(queries.begin(), queries.end());\n        for (auto query : queries) {\n            auto type = query[0], index = query[1], val = query[2];\n            if (type == 0 && row[index]) {\n                ans += rowc * val;\n                colc -= 1;\n                row[index] = 0;\n            } else if (type == 1 && col[index]) {\n                ans += colc * val;\n                rowc -= 1;\n                col[index] = 0;\n            }\n        }\n        \n        return ans;\n    }\n};\n```
['Matrix']