Easy to Understand || Prefix Sum || With Explanation || Not brute force || C++ Solution
hint \n1) try to solve using some pre computation, like some variation in prefix sum. \n\n        \n\n# Complexity\n- Time complexity: O(queries.size()n + nn)\n
# hint \n1) try to solve using some pre computation, like some variation in prefix sum. \n\n        \n\n# Complexity\n- Time complexity: $$O(queries.size()*n + n*n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n*n)$$ (for storing answer)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    vector<vector<int>> rangeAddQueries(int n, vector<vector<int>>& queries) {\n        \n\n/*\napproach-\n    let n = 3, queries = [[1,1,2,2],[0,0,1,1]]\n\n    make a ans array of size n*(n+1)\n    ans = 0 0 0 0\n          0 0 0 0\n          0 0 0 0\n    for every query :\n        make all row from x1 to x2 \n            ans[i][y1]++; ans[i][y2+1]--;\n    \n    after first iteration:\n    ans = 0 0 0 0\n          0 1 0 -1\n          0 1 0 -1\n    after second iteration:\n    ans = 1 0 -1 0\n          1 1 -1 -1\n          0 1 0  -1\n    why ans[i][y1]++ and ans[i][y2+1]-- ??\n        -> (i,y1) indicates that this perticular query contribute 1 from this index so we add 1 in ans.\n        -> (i,y2+1) indicates that this perticular query no longer contributes any 1 so we subtract 1 in ans.\n\n\n    now calculate prefix sum of every row;\n    ans = 1 1 0 0\n          1 2 1 0\n          0 1 1 0\n    delete the last column.\n    return ans;\n*/\n\n        vector<int>row1,row2;\n        vector<int>fir(n+1,0),sec(n+1,0);\n        vector<vector<int>>ans(n,vector<int>(n+1,0));\n        \n        for(auto x : queries){\n            int x1 = x[0], x2 = x[2];\n            int y1 = x[1], y2 = x[3];\n            \n            for(int i = x1; i<=x2; i++){\n                ans[i][y1] +=1;\n                ans[i][y2+1] -=1;\n            }\n        }\n        \n        for(int i = 0; i<n; i++){\n            for(int j = 0; j<n; j++){\n                if(j>0) ans[i][j] = ans[i][j]+ans[i][j-1];\n            }\n        }\n        for(int i = 0; i<n; i++) ans[i].pop_back();\n\n        return ans;\n    }\n};\n```
['Matrix', 'Prefix Sum', 'C++']