308. Range Sum Query 2D - Mutable

/*
 * 308. Range Sum Query 2D - Mutable
 * 2016-7-3 by Mingyang
 * 继续丧心病狂的Segment Tree
 */
class NumMatrix {

    int[][] tree;
    int[][] nums;
    int m;
    int n;

    public NumMatrix(int[][] matrix) {
        if (matrix.length == 0 || matrix[0].length == 0) return;
        m = matrix.length;
        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) {
        if (m == 0 || n == 0) return;
        int delta = 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] += delta;
            }
        }
    }

    public int sumRegion(int row1, int col1, int row2, int col2) {
        if (m == 0 || n == 0) return 0;
        return sum(row2+1, col2+1) + sum(row1, col1) - sum(row1, col2+1) - sum(row2+1, col1);
    }

    public int sum(int row, int col) {
        int sum = 0;
        for (int i = row; i > 0; i -= i & (-i)) {
            for (int j = col; j > 0; j -= j & (-j)) {
                sum += tree[i][j];
            }
        }
        return sum;
    }
}
// time should be O(log(m) * log(n))
时间: 2024-10-13 13:13:28

308. Range Sum Query 2D - Mutable的相关文章

Range Sum Query 2D - Mutable &amp; Immutable

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,

[LeetCode] 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, co

LeetCode &quot;Range Sum Query 2D - Mutable&quot;

2D Segment Tree -> Quad Tree. All in O(log4N) class Node // 2D Segment Tree { public: Node(vector<vector<int>> &m, int ix0, int iy0, int ix1, int iy1) : sum(0), x0(ix0), x1(ix1), y0(iy0), y1(iy1), ul(nullptr), ur(nullptr), dl(nullptr),

[LeetCode] Range Sum Query - Immutable &amp; Range Sum Query 2D - Immutable

Range Sum Query - Immutable Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. Example: Given nums = [-2, 0, 3, -5, 2, -1] sumRange(0, 2) -> 1 sumRange(2, 5) -> -1 sumRange(0, 5) -> -3 Note: You

304. Range Sum Query 2D - Immutable

/* * 304. Range Sum Query 2D - Immutable * 2016-7-3 by Mingyang * 这个题目自己做了一下同样的dp思路,但是out of boundry,为什么呢? * 因为自己老老实实的把dp的维度跟matrix的维度设成一样的了 * 那我们就把dp的维度再多设一个这样就很好地表示了 * dp[i][j]仅仅表示到i-1,j-1这个点(inclusive)的和的大小 */ class NumMatrix { private int[][] df;

[LeetCode] Range Sum Query 2D - Immutable

Very similar to Range Sum Query - Immutable, but we now need to compute a 2d accunulated-sum. In fact, if you work in computer vision, you may know a name for such an array --- Integral Image. To solve this problem, Stefan has already posted a very e

Range Sum Query 2D - Immutable

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, co

304. Range Sum Query 2D - Immutable java solutions

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, co

leetcode笔记:Range Sum Query 2D - Immutable

一. 题目描述 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 (