2536. 子矩阵元素加1

2536. 子矩阵元素加1

class Solution {
public:
    vector<vector<int>> rangeAddQueries(int n, vector<vector<int>>& queries) {
        vector<vector<int>> dif(n + 5,vector<int>(n + 5,0));
        for(int i = 0;i < queries.size();i ++)
        {
            int x1 = queries[i][0] + 1,y1 = queries[i][1] + 1,x2 = queries[i][2] + 1,y2 = queries[i][3] + 1;
            dif[x1][y1] ++;
            dif[x2 + 1][y2 + 1] ++;
            dif[x2 + 1][y1] --;
            dif[x1][y2 + 1] --;
        }
        vector<vector<int>> ans(n);
        for(int i = 1;i <= n;i ++)
        {
            for(int j = 1;j <= n;j ++)
            {
                dif[i][j] += dif[i - 1][j] + dif[i][j - 1] - dif[i - 1][j - 1];
                ans[i - 1].push_back(dif[i][j]);
            }
        }


        return ans;
    }
};
posted @ 2024-11-21 11:46  chhh31  阅读(1)  评论(0编辑  收藏  举报