✅C++ | ✅Marking Boundaries | ✅Efficient Approach
Intuition\nBrute-force approach for solving this problem is not working in the given time constraints. So we need to reduce nested loops.\n\n# Approach\nWe mark
# Intuition\nBrute-force approach for solving this problem is not working in the given time constraints. So we need to reduce nested loops.\n\n# Approach\nWe mark boundary of start point of adding 1 and ending point of adding 1 for each row. By using given steps:\n\ni) Initialize nxn matrix with 0.\n\nii) For marking start point of the boundary, we add +1 for each row given in the query.\n \niii) For marking end point of the boundary, we add -1 at the next column for each row. But if we are at last column of the row, we mark first column of the next row as -1 and if we are also at last row of matrix then we skip adding -1.\n\niv) At last we create required matrix by taking count as 0 and update it as par the values marked in the matrix\'s cell and assign count to that cell. \n\n# Complexity\n- Time complexity:\nO(nxn) + O(queries x rows)\n\n- Space complexity:\nO(1)\n\n# Code\n```\nclass Solution \n{\npublic:\n    vector<vector<int>> rangeAddQueries(int n, vector<vector<int>>& queries) \n    {\n        vector<vector<int>> mat(n, vector<int> (n, 0));\n        int r1, c1, r2, c2;\n        for(auto &it:queries)\n        {\n            r1=it[0], c1=it[1], r2=it[2], c2=it[3];\n            for(int i=r1; i<=r2; i++) //marking each row of the query\n            {\n                mat[i][c1] += 1; //according to the query, we need to start adding 1 from (i, c1) cell\n                if((c2+1)>=n) //for stop adding one, we need to check whether we are at last cell of the row or not\n                    if(i+1<n) //if we are at last cell, we mark first cell of next row as stop adding 1\n                        mat[i+1][0] -= 1;\n                    else\n                        continue;\n                        \n                \n                else //if we are not at last cell, we mark next cell of the row at stop adding 1.\n                    mat[i][c2+1] -= 1;      \n                \n            }    \n        }\n        \n        int cnt=0;\n        for(int i=0; i<n; i++)\n        {\n            for(int j=0; j<n; j++)\n            {\n                cnt += mat[i][j];\n                mat[i][j] = cnt;\n            }\n        }\n        return mat;\n    }\n};\n```
['Matrix', 'C++']