Range Sum Query 2D – Mutable
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2).
The above rectangle (with the red border) is defined by (row1, col1) = (2, 1) and (row2, col2) = (4, 3), which contains sum = 8.Example:
Given matrix = [ [3, 0, 1, 4, 2], [5, 6, 3, 2, 1], [1, 2, 0, 1, 5], [4, 1, 0, 1, 7], [1, 0, 3, 0, 5] ] sumRegion(2, 1, 4, 3) -> 8 update(3, 2, 2) sumRegion(2, 1, 4, 3) -> 10Note:
- The matrix is only modifiable by the update function.
- You may assume the number of calls to update and sumRegion function is distributed evenly.
- You may assume that row1 ≤ row2 and col1 ≤ col2.
2D segment tree
class SegtreeNode{ public: int x1, x2, y1, y2; SegtreeNode * ltChild, * rtChild, * lbChild, *rbChild; int val; SegtreeNode(int x1, int y1, int x2, int y2):x1(x1), x2(x2), y1(y1), y2(y2), ltChild(nullptr),lbChild(nullptr), rtChild(nullptr),rbChild(nullptr),val(0){} }; class NumMatrix { public: SegtreeNode * root; NumMatrix(vector<vector<int>> &matrix) { if(matrix.empty() || matrix[0].empty()) return ; root = build(matrix, 0, 0, matrix.size() - 1, matrix[0].size() - 1); } SegtreeNode * build(vector<vector<int>>& matrix, int x1, int y1, int x2, int y2){ if(x1 > x2 || y1 > y2) return nullptr; SegtreeNode * node = new SegtreeNode(x1, y1, x2, y2); if(x1 == x2 && y1 == y2){ node->val = matrix[x1][y1]; } else{ int xmid = (x1 + x2) / 2; int ymid = (y1 + y2) / 2; node->ltChild = build(matrix, x1, y1, xmid, ymid); node->rtChild = build(matrix, xmid + 1, y1, x2, ymid); node->lbChild = build(matrix, x1, ymid + 1, xmid, y2); node->rbChild = build(matrix, xmid + 1, ymid + 1, x2, y2); node->val = 0; node->val += node->ltChild? node->ltChild->val: 0; node->val += node->rtChild? node->rtChild->val: 0; node->val += node->lbChild? node->lbChild->val: 0; node->val += node->rbChild? node->rbChild->val: 0; } return node; } void update(int row, int col, int val) { _update(root, row, col, val); } void _update(SegtreeNode * p, int row, int col, int val){ if(p == nullptr) return ; if(p->x1 == p->x2 && p->y1 == p->y2){ p->val = val; } else{ int xmid = (p->x1 + p->x2) / 2; int ymid = (p->y1 + p->y2) / 2; if(row <= xmid){ if(col <= ymid){ _update(p->ltChild, row, col, val); }else{ _update(p->lbChild, row, col, val); } }else{ if(col <= ymid){ _update(p->rtChild, row, col, val); }else{ _update(p->rbChild, row, col, val); } } p->val = 0; p->val += p->ltChild? p->ltChild->val: 0; p->val += p->rtChild? p->rtChild->val: 0; p->val += p->lbChild? p->lbChild->val: 0; p->val += p->rbChild? p->rbChild->val: 0; } } int sumRegion(int row1, int col1, int row2, int col2) { return _sum(root, row1, col1, row2, col2); } int _sum(SegtreeNode * p, int x1, int y1, int x2, int y2){ if(p == nullptr) return 0; if(p->x1 >= x1 && p->x2 <= x2 && p->y1 >= y1 && p->y2 <= y2){ return p->val; }else if(p->x1 > x2 || p->x2 < x1 || p->y1 > y2 || p->y2 < y1){ return 0; }else{ int sum = 0; sum += _sum(p->ltChild, x1, y1, x2, y2); sum += _sum(p->lbChild, x1, y1, x2, y2); sum += _sum(p->rtChild, x1, y1, x2, y2); sum += _sum(p->rbChild, x1, y1, x2, y2); return sum; } } }; // Your NumMatrix object will be instantiated and called as such: // NumMatrix numMatrix(matrix); // numMatrix.sumRegion(0, 1, 2, 3); // numMatrix.update(1, 1, 10); // numMatrix.sumRegion(1, 2, 3, 4);
sum columns in a row. Dynamic programming.
class NumMatrix { public: vector<vector<int>> data; NumMatrix(vector<vector<int>> &matrix) { data = matrix; for(int i = 0;i < data.size(); i++){ for(int j = 1; j < data[0].size(); j++){ data[i][j] = data[i][j] + data[i][j - 1]; } } } void update(int row, int col, int val) { int offset = 0; if(col == 0){ offset = val - data[row][col]; }else{ offset = val - (data[row][col] - data[row][col - 1]); } for(int i = col; i < data[0].size(); i++){ data[row][i] += offset; } } int sumRegion(int row1, int col1, int row2, int col2) { int sum = 0; for(int i = row1; i <= row2; i++){ if(col1 == 0){ sum += data[i][col2]; }else{ sum += data[i][col2] - data[i][col1 - 1]; } } return sum; } }; // Your NumMatrix object will be instantiated and called as such: // NumMatrix numMatrix(matrix); // numMatrix.sumRegion(0, 1, 2, 3); // numMatrix.update(1, 1, 10); // numMatrix.sumRegion(1, 2, 3, 4);
666