Monday, November 23, 2015

LeetCode [308] Range Sum Query 2D - Mutable

 308. Range Sum Query 2D - Mutable

Hard

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).

Range Sum Query 2D
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) -> 10

Note:

  1. The matrix is only modifiable by the update function.
  2. You may assume the number of calls to update and sumRegion function is distributed evenly.
  3. You may assume that row1 ≤ row2 and col1 ≤ col2.
//C++, Segment Tree
//C++: 784ms
class STNode{
public:
    int sum;
    int ss, se;
    STNode *left, *right;
    STNode(vector<int> &row, int s, int e){
        sum = 0;
        ss = s;
        se = e;
        left = NULL;
        right = NULL;
        if(s==e){
            sum = row[s];
        }else{
            int m = (s+e)/2;
            left = new STNode(row, s, m);
            right = new STNode(row, m+1, e);
            sum = left->sum+right->sum;
        }
    }
};

class NumMatrix {
    int nRows;
    vector<STNode *> roots;
    void updateST(STNode* root, int j, int val){
        if(!root || j<root->ss || j>root->se) return;
        else if(j==root->ss && j==root->se) root->sum = val;
        else{
            updateST(root->left, j, val);
            updateST(root->right, j, val);
            root->sum = (root->left?root->left->sum:0) + (root->right?root->right->sum:0);
        }
    }
    int sumRange(STNode* root, int i, int j){
        if(!root) return 0;
        else if(i>root->se || j<root->ss) return 0;
        else if(i<=root->ss && j>=root->se) return root->sum;
        else return sumRange(root->left, i, j)+sumRange(root->right, i, j);
    }
public:
    NumMatrix(vector<vector<int>> &matrix) {
        nRows = matrix.size();
        roots.resize(nRows);
        for(int i=0; i<nRows; ++i){
            roots[i] = new STNode(matrix[i], 0, (int)matrix[i].size()-1);
        }
    }

    void update(int row, int col, int val) {
        updateST(roots[row], col, val);
    }

    int sumRegion(int row1, int col1, int row2, int col2) {
        int ret = 0;
        for(int i=row1; i<=row2; ++i){
            ret += sumRange(roots[i], col1, col2);
        }
        return ret;
    }
};

//Java, Segment Tree
class SGTree{
    int sum, start, end;
    SGTree left, right;
    SGTree(int[] nums, int l, int r){
        start = l;
        end = r;
        if(start==end){
            sum = nums[l];
        }else{
            int m = (l+r)/2;
            left = new SGTree(nums, l, m);
            right = new SGTree(nums, m+1, r);
            sum = left.sum + right.sum;
        }
    }
};

class NumMatrix {
    int[] nums;
    SGTree root;
    int m, n;
    public NumMatrix(int[][] matrix) {
        m = matrix.length;
        if(m==0) return;
        n = matrix[0].length;
        if(n==0) return;
        nums = new int[m*n];
        for(int i=0; i<m; ++i){
            for(int j=0; j<n; ++j){
                nums[i*n+j] = matrix[i][j];
            }
        }
        root = new SGTree(nums, 0, nums.length-1);
    }

    void updateST(SGTree node, int i, int v){
        if(node==null || i<node.start || i>node.end) return;
        if(node.start==i && node.end==i) node.sum = v;
        else{
            updateST(node.left, i, v);
            updateST(node.right, i, v);
            node.sum = node.left.sum+node.right.sum;
        }
    }

    int sumST(SGTree node, int l, int r){
        if(node==null) return 0;
        if(r<node.start || l>node.end) return 0;
        if(l<=node.start && node.end<=r) return node.sum;
        int s = sumST(node.left, l, r) + sumST(node.right, l, r);
        return s;
    }
    
    public void update(int row, int col, int val) {
        updateST(root, row*n+col,val);
    }
    
    public int sumRegion(int row1, int col1, int row2, int col2) {
        int s = 0;
        for(int i=row1; i<=row2; ++i){
            s += sumST(root, i*n+col1, i*n+col2);
        }
        return s;
    }
};
/**
 * Your NumMatrix object will be instantiated and called as such:
 * NumMatrix obj = new NumMatrix(matrix);
 * obj.update(row,col,val);
 * int param_2 = obj.sumRegion(row1,col1,row2,col2);
 */

//Java, Binary Indexed Tree
class NumMatrix {

    int[][] nums;
    int[][] tree;
    int m, n;
    public NumMatrix(int[][] matrix) {
        m = matrix.length;
        if(m==0) return;
        n = matrix[0].length;
        tree = new int[m+1][n+1];
        nums = new int[m][n];
        for(int i=0; i<m; ++i){
            for(int j=0; j<n; ++j){
                update(i, j, matrix[i][j]);
            }
        }
    }
    
    public void update(int row, int col, int val) {
        int d = val-nums[row][col];
        nums[row][col] = val;
        for(int i=row+1; i<=m; i+=i&(-i)){
            for(int j=col+1; j<=n; j+=j&(-j)){
                tree[i][j] += d;
            }
        }
    }
    
    //row and col are corrordinates for tree
    public int sum(int row, int col){
        int s = 0;
        for(int i=row; i>0; i-=i&(-i)){
            for(int j=col; j>0; j-=j&(-j)){
                s += tree[i][j];
            }
        }
        return s;
    }

    public int sumRegion(int row1, int col1, int row2, int col2) {
        return sum(row2+1, col2+1)-sum(row2+1,col1)-sum(row1, col2+1)+sum(row1, col1);
    }
}

/**
 * Your NumMatrix object will be instantiated and called as such:
 * NumMatrix obj = new NumMatrix(matrix);
 * obj.update(row,col,val);
 * int param_2 = obj.sumRegion(row1,col1,row2,col2);
 */

No comments:

Post a Comment