Area Sweep
As a refresher, a line sweep is a technique when, instead of adding value to each element, we:\n- Add value at the start of the segment,\n- Subtract that value 
As a refresher, a line sweep is a technique when, instead of adding value to each element, we:\n- Add value at the start of the segment,\n- Subtract that value after the end of the segment. \n\nAfter processing all queries, we use a prefix sum to aggregate all queries in a single "sweep".\n\nBased on the problem contraints, a line sweep solution is accepted.\n\n2D area sweep is more complicated, but signifficantly faster: O(m + n * n) vs. O(m * n + n * n)).\n\nIt\'s useful to place these sulutions side-by-side to highlight the difference.\n\n## 1D Sweep\n\n![image](https://assets.leetcode.com/users/images/539a8cbc-8fdd-4485-bece-26e473c1d54b_1676248344.1043003.png)\n\n**C++**\n```cpp\nvector<vector<int>> rangeAddQueries(int n, vector<vector<int>>& queries) {\n    vector<vector<int>> res(n, vector<int>(n));\n    for (auto q : queries)\n        for (int i = q[0]; i <= q[2]; ++i) {\n            res[i][q[1]] += 1;\n            if (q[3] + 1 < n)\n                res[i][q[3] + 1] -= 1;\n        }\n    for (int i = 0; i < n; ++i)\n        partial_sum(begin(res[i]), end(res[i]), begin(res[i]));\n    return res;\n}\n```\n\n## 2D Sweep\nFor the area sweep, we:\n- Add value to `[r1][c1]`.\n- Subtract that value from `[r1][c2 + 1]`,\n- Subtract that value from `[r2 + 1][c1]`,\n- Add that value again to `[r2 + 1][c2 + 1]`.\n\nNote that we do prefix sum independently for each dimension, so that we do not double-count.\n\n![image](https://assets.leetcode.com/users/images/2a2516ed-54bd-4f7f-9296-9ccea788c761_1676248400.5119631.png)\n\n**C++**\n```cpp\nvector<vector<int>> rangeAddQueries(int n, vector<vector<int>>& queries) {\n    vector<vector<int>> res(n, vector<int>(n));\n    for (auto q : queries) {\n            int r1 = q[0], c1 = q[1], r2 = q[2], c2 = q[3];\n            ++res[r1][c1];\n            if (r2 + 1 < n)\n                --res[r2 + 1][c1];\n            if (c2 + 1 < n)\n                --res[r1][c2 + 1];\n            if (max(r2, c2) + 1 < n)\n                ++res[r2 + 1][c2 + 1];\n    }\n    for (int i = 0; i < n; ++i)\n        partial_sum(begin(res[i]), end(res[i]), begin(res[i]));  \n    for (int i = 1; i < n; ++i)\n        for (int j = 0; j < n; ++j)\n            res[i][j] += res[i - 1][j];         \n    return res;\n}\n```
['C']