✅ Dedicated to beginners [ Image explanation -- C++, Java, Python ]
IMPORTANT\uD83D\uDCDD  : If you want to master the concept of sliding window and want to solve all slding window (20+) problems just using single template you m
**IMPORTANT\uD83D\uDCDD**  : If you want to master the concept of sliding window and want to solve all slding window (20+) problems just using single template you may check out my youtube channel by clicking my profile icon and you will find the link of channel. Best of Luck \u270C\uFE0F\n**It took a lot of efforts to prepare a image explanation , please upvote this post so that other can find it easily.**\nThankyou,\n\n![image](https://assets.leetcode.com/users/images/c9c5c980-4dad-42cb-b2f8-766fd1d7196d_1685897211.6367457.png)\n![image](https://assets.leetcode.com/users/images/d15f48f8-d72e-408c-99ba-6ac2cee67287_1685897057.0139484.png)\n![image](https://assets.leetcode.com/users/images/28656679-747a-4c8d-8f43-44e7f1c6e17e_1685898753.1859484.png)\n![image](https://assets.leetcode.com/users/images/e7b90e9f-c855-4a54-a096-9eed05f9f501_1685899477.3602993.png)\n![image](https://assets.leetcode.com/users/images/760b9c3e-1848-490f-91f2-80fac33523f5_1685898930.5690446.png)\n![image](https://assets.leetcode.com/users/images/e86d9d46-b43d-4c13-a6df-b049d39c0d92_1685899716.3258834.png)\n\n\n\n\n\n**1. C++**\n```\nclass Solution {\npublic:\n    long long matrixSumQueries(int n, vector<vector<int>>& queries) {\n         long sum = 0;\n         int row = n, col = n;\n        \n         unordered_set<int>Rowvis;       // storage of index for type==0\n         unordered_set<int>Colvis;        // storage of index for type==1\n\n    for (int i = queries.size() - 1; i >= 0; i--) {\n        \n        int type=queries[i][0];\n        int idx=queries[i][1];\n        int value=queries[i][2];\n        \n        if (type==0) {\n            if (Rowvis.find(idx)!=Rowvis.end()) {            // because this index is already calculated\n                continue;\n            } \n            else {                     \n               \n                sum += col* value;     // update its value if not filled this index already (updated col value will handle it)\n                Rowvis.insert(idx);    // mark this index as updated for type==0                          \n                row --;               // decrement row value because this row value is now permanent we can not update it further\n              \n            }\n        } else {\n            if (Colvis.find(idx)!=Colvis.end()) {        // because this index is already calculated\n                continue;\n            }\n            else {\n               \n                sum += row * value;   // update its value if not filled this index already (updated row value will handle it)\n                Colvis.insert(idx);   // mark this index as updated for type==1\n                col --;             // decrement col value because this col value is now permanent we can not update it \n            }\n        }\n    }\n\n    return sum;\n\n    }\n};\n```\n \n **More Optimized C++ code:**\n```\nlong long matrixSumQueries(int n, vector<vector<int>>& queries) {\n        \n        vector<int>visrow(n),viscol(n);\n        \n        reverse(queries.begin(), queries.end());\n        \n        int row=n, col= n;\n        long long sum= 0;\n        \n        for(int i=0; i<queries.size(); i++){\n            \n            int type= queries[i][0];\n            int idx= queries[i][1];\n            \n            if(type == 0 and visrow[idx] == 0){        \n               \n                int value= queries[i][2];\n                \n                sum+= row * value;            \n                visrow[idx] = 1;              // this row is changed so dont visit it again\n                col--;                        // now 1 more col is reserved you cant fill this\n            }\n            \n            else if(type ==1 and viscol[idx] == 0){             \n               \n                int value= queries[i][2];\n                \n                sum+= col * value;\n                viscol[idx]= 1;            // this col is changed so dont visit it again\n                row--;                     // now 1 more row is reserved you cant fill this\n            } \n        }\n        \n        return sum;\n    }\n```\n\n**2. PYTHON**\n\n```\nclass Solution:\n    def matrixSumQueries(self, n, queries):\n        sum_val = 0\n        row = n\n        col = n\n        \n        row_vis = set()    # storage of index for type==0\n        col_vis = set()    # storage of index for type==1\n        \n        for i in range(len(queries) - 1, -1, -1):\n            query_type = queries[i][0]\n            idx = queries[i][1]\n            value = queries[i][2]\n            \n            if query_type == 0:\n                if idx in row_vis:    # because this index is already calculated\n                    continue\n                else:\n                    sum_val += col * value    # update its value if not filled this index already (updated col value will handle it)\n                    row_vis.add(idx)    # mark this index as updated for type==0\n                    row -= 1    # decrement row value because this row value is now permanent we cannot update it further\n            else:\n                if idx in col_vis:    # because this index is already calculated\n                    continue\n                else:\n                    sum_val += row * value    # update its value if not filled this index already (updated row value will handle it)\n                    col_vis.add(idx)    # mark this index as updated for type==1\n                    col -= 1    # decrement col value because this col value is now permanent we cannot update it further\n        \n        return sum_val\n\n```\n\n**3. JAVA**\n\n```\nimport java.util.HashSet;\nimport java.util.Set;\nimport java.util.List;\n\nclass Solution {\n    public long matrixSumQueries(int n, List<List<Integer>> queries) {\n        long sumVal = 0;\n        int row = n;\n        int col = n;\n\n        Set<Integer> rowVis = new HashSet<>();    // storage of index for type==0\n        Set<Integer> colVis = new HashSet<>();    // storage of index for type==1\n\n        for (int i = queries.size() - 1; i >= 0; i--) {\n            int type = queries.get(i).get(0);\n            int idx = queries.get(i).get(1);\n            int value = queries.get(i).get(2);\n\n            if (type == 0) {\n                if (rowVis.contains(idx)) {    // because this index is already calculated\n                    continue;\n                } else {\n                    sumVal += col * value;    // update its value if not filled this index already (updated col value will handle it)\n                    rowVis.add(idx);    // mark this index as updated for type==0\n                    row--;    // decrement row value because this row value is now permanent we cannot update it further\n                }\n            } else {\n                if (colVis.contains(idx)) {    // because this index is already calculated\n                    continue;\n                } else {\n                    sumVal += row * value;    // update its value if not filled this index already (updated row value will handle it)\n                    colVis.add(idx);    // mark this index as updated for type==1\n                    col--;    // decrement col value because this col value is now permanent we cannot update it further\n                }\n            }\n        }\n\n        return sumVal;\n    }\n}\n\n```\n
['C']