Easy, Clean, Short C++ Solution | prefix sum
Approch\n Imagine each row as a separate array. Instead of updating the whole submatrix together, we can use prefix sum to update each row separately.\n For eac
**Approch**\n* Imagine each row as a separate array. Instead of updating the whole submatrix together, we can use prefix sum to update each row separately.\n* For each query, iterate over the rows i in the range [row1, row2] and add 1 to prefix sum mat[i][col1] (start point), and subtract 1 from mat[i][col2 + 1] (end point).\n* After doing this operation for all the queries, update each row separately with mat[i][j] = mat[i][j] + mat[i][j - 1].\n```\nclass Solution {\npublic:\n    vector<vector<int>> rangeAddQueries(int n, vector<vector<int>>& queries) {\n    vector<vector<int>> mat(n, vector<int>(n, 0));\n\n    for (auto q : queries) {\n        int row1 = q[0], col1 = q[1], row2 = q[2], col2 = q[3];\n        for (int i = row1; i <= row2; i++) {\n            mat[i][col1]++; // row start point\n            if (col2 + 1 < n) mat[i][col2 + 1]--; // row end point // to mark as end of submatrix\n                                                  //1 is substracted to offset previous submatrix values\n        }\n    }\n    for (int i = 0; i < n; i++) {\n        for (int j = 1; j < n; j++) {\n            mat[i][j] += mat[i][j - 1]; // copy from previous column \n        }\n    }\n    return mat;\n    }\n};\n```\nTC: **O(q*n + n^2)**\ncouldn\'t solve it in contest :\'( but after seeing hints it was easy :)
['C']