Last Query Wins
We start with the last query and go backwards.\n\n\nWe track id of rows and columns we have seen - so we can ignore earlier queries that will be  overwritten.\n
We start with the last query and go backwards.\n\n\nWe track `id` of rows and columns we have seen - so we can ignore earlier queries that will be  overwritten.\n\nWe also track how many rows and columns are still not covered by any query. That way, each query adds:\n- `v * cols_cnt` for a row,\n- `v * rows_cnt` for a column.\n\n## Minimalizm Version\nIn this version, we abstract from rows and columns, and use `type` as an idex.\n\nSee a verbose version below (could be easier to understand).\n\n**C++**\n```cpp\nlong long matrixSumQueries(int n, vector<vector<int>>& qs) {\n    long long res = 0, cnt[2] = {n, n};\n    bool seen[2][10001] = {};\n    for (int i = qs.size() - 1; i >= 0; --i) {\n        int type = qs[i][0], id = qs[i][1], v = qs[i][2];\n        if (!seen[type][id]) {\n            seen[type][id] = true;\n            res += v * cnt[!type];\n            --cnt[type];\n        }\n    }\n    return res;\n}\n```\n\n## Verbose Version\n**C++**\n```cpp\nlong long matrixSumQueries(int n, vector<vector<int>>& qs) {\n    long long res = 0, cols_cnt = n, rows_cnt = n;\n    vector<bool> cols(n), rows(n);\n    for (int i = qs.size() - 1; i >= 0; --i) {\n        int type = qs[i][0], id = qs[i][1], v = qs[i][2];\n        if (type == 0) { // row\n            if (!rows[id]) {\n                rows[id] = true;\n                --rows_cnt;\n                res += v * cols_cnt;\n            }\n        }\n        else { // col\n            if (!cols[id]) {\n                cols[id] = true;\n                --cols_cnt;    \n                res += v * rows_cnt;\n            }\n        }\n    }\n    return res;\n}\n```
['C']