Java || C++ || JavaScript || Prefix Matrix || Beats 100% || O(m*n) time || Concise and Clean code
Intuition\nMaintaining a prefix matrix to store increments and decrements of size [n+1][n+1].\n\n\n# Complexity\n- Time complexity: O((mn) + (nn)) \u2243 O(m*n)
# Intuition\nMaintaining a prefix matrix to store increments and decrements of size [n+1][n+1].\n\n\n# Complexity\n- Time complexity: O((m*n) + (n*n)) \u2243 O(m*n) ,m=queries.length\n\n- Space complexity: O(n^2)\n\n# Code\n``` java []\nclass Solution {\n    public int[][] rangeAddQueries(int n, int[][] queries) {\n        int[][] prefix = new int[n+1][n+1];\n        int[][] a = new int[n][n];\n        for(int[] x:queries)\n        {\n            int r1 = x[0], r2 = x[2], c1 = x[1], c2 = x[3];\n            for(int i=r1;i<=r2;i++){\n                prefix[i][c1]++;\n                prefix[i][c2+1]--;\n            }\n        }\n        \n        for(int i=0;i<n;i++)\n        {\n            int sum=0;\n            for(int j=0;j<n;j++)\n            {\n                sum+=prefix[i][j];\n                a[i][j]+=sum;\n            }\n        }\n        \n        return a;\n    }\n}\n```\n``` C++ []\nclass Solution {\npublic:\n    vector<vector<int>> rangeAddQueries(int n, vector<vector<int>>& queries) {\n        vector<vector<int>> prefix(n+1, vector<int>(n+1));\n        vector<vector<int>> a(n, vector<int>(n));\n        for(auto x:queries)\n        {\n            int r1 = x[0], r2 = x[2], c1 = x[1], c2 = x[3];\n            for(int i=r1;i<=r2;i++){\n                prefix[i][c1]++;\n                prefix[i][c2+1]--;\n            }\n        }\n\n        for(int i=0;i<n;i++)\n        {\n            int sum=0;\n            for(int j=0;j<n;j++)\n            {\n                sum+=prefix[i][j];\n                a[i][j]+=sum;\n            }\n        }\n        \n        return a;\n    }\n};\n```\n``` javascript []\nvar rangeAddQueries = function(n, queries) {\n    let prefix = new Array(n+1).fill(0).map(() => new Array(n+1).fill(0));\n    let a = new Array(n).fill(0).map(() => new Array(n).fill(0));\n    for (let x of queries) {\n        let r1 = x[0], c1 = x[1], r2 = x[2], c2 = x[3];\n        for (let j = r1; j <= r2; j++) {\n            prefix[j][c1]++;\n            prefix[j][c2 + 1]--;\n        }\n    }\n\n    for (let i = 0; i < n; i++) {\n        let sum = 0;\n        for (let j = 0; j < n; j++) {\n            sum += prefix[i][j];\n            a[i][j] += sum;\n        }\n    }\n    return a;\n};\n```\n
['Prefix Sum', 'C++', 'Java', 'JavaScript']