✅Start from the End✅ || 🔥O(q)🔥 || C ++ || Java || Python
Intuition\nStart from reverse since at the end we end up at the latest configuration of the matrix. Afterwards if there is case of overwriting then we would not
# Intuition\n**```Start from reverse since at the end we end up at the latest configuration of the matrix. Afterwards if there is case of overwriting then we would not consider it as we starting from latest to initial configuration```**\n\n# Approach\n**```Take two maps one for row , and one for the column. Take two variable which would keep the count of no. of columns and no. of rows```**\n**``` ```**\n\nAt any point while performing the row query add **``` val_at_the_row * (n - colums_already_painted) ```**\nAt any point while performing the col query add **``` val_at_the_col * (n - rows_already_painted) ```**\n\n\n# Complexity\n- Time complexity:\n**```Since it requires just to traverse the entire query only once the time complexity is: O(q) where q is the size of the query list```**\n\n- Space complexity:\n**```We are using here 2 map one for row and other for col so Space taken by a map can be at max N, so O(N) where N is the number of rows and columns```**\n\n# Code (C++)\n```\nclass Solution {\npublic:\n    long long matrixSumQueries(int n, vector<vector<int>>& queries) {\n        long long ans = 0, rsum = 0, csum = 0;\n        unordered_map<int, int> mpr, mpc;\n        reverse(queries.begin(), queries.end());\n        for (auto it : queries)\n        {\n            if (it[0] == 0)\n            {\n                // row\n                int r = it[1];\n                int val = it[2];\n                if (mpr.find(r) == mpr.end())\n                {\n                    rsum ++;\n                    mpr[r] = val;\n                    ans += val * (n - csum);\n                }\n            }\n            else\n            {\n                //col\n                int c = it[1];\n                int val = it[2];\n                if (mpc.find(c) == mpc.end())\n                {\n                    csum ++;\n                    mpc[c] = val;\n                    ans += val * (n - rsum);\n                }\n            }\n        }\n        return ans;\n    }\n};\n```\n# Code(Java)\n``` import java.util.ArrayList;\nimport java.util.HashMap;\nimport java.util.List;\nimport java.util.Map;\nimport java.util.Collections;\n\nclass Solution {\n    public long matrixSumQueries(int n, int[][] queries) {\n        long ans = 0, rsum = 0, csum = 0;\n        Map<Integer, Integer> mpr = new HashMap<>();\n        Map<Integer, Integer> mpc = new HashMap<>();\n        List<List<Integer>> queryList = new ArrayList<>();\n\n        for (int[] query : queries) {\n            List<Integer> list = new ArrayList<>();\n            for (int i = 0; i < query.length; i++) {\n                list.add(query[i]);\n            }\n            queryList.add(list);\n        }\n        \n        Collections.reverse(queryList);\n        \n        for (List<Integer> it : queryList) {\n            if (it.get(0) == 0) {\n                // row\n                int r = it.get(1);\n                int val = it.get(2);\n                if (!mpr.containsKey(r)) {\n                    rsum++;\n                    mpr.put(r, val);\n                    ans += val * (n - csum);\n                }\n            } else {\n                // col\n                int c = it.get(1);\n                int val = it.get(2);\n                if (!mpc.containsKey(c)) {\n                    csum++;\n                    mpc.put(c, val);\n                    ans += val * (n - rsum);\n                }\n            }\n        }\n        \n        return ans;\n    }\n}\n\n```\n# Code(Python)\n```\nclass Solution:\n    def matrixSumQueries(self, n, queries):\n        ans = 0\n        rsum = 0\n        csum = 0\n        mpr = {}\n        mpc = {}\n        queries.reverse()\n        \n        for it in queries:\n            if it[0] == 0:\n                # row\n                r = it[1]\n                val = it[2]\n                if r not in mpr:\n                    rsum += 1\n                    mpr[r] = val\n                    ans += val * (n - csum)\n            else:\n                # col\n                c = it[1]\n                val = it[2]\n                if c not in mpc:\n                    csum += 1\n                    mpc[c] = val\n                    ans += val * (n - rsum)\n        \n        return ans\n```
['Hash Table', 'Python', 'C++', 'Java', 'Python3']