LeetCode "Range Sum Query 2D - Mutable"

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), dr(nullptr)
        {
            if(ix0 > ix1 || iy0 > iy1) return;

            if(ix0 == ix1 && iy0 == iy1)
            {
                sum = m[iy0][ix0];
                return;
            }

            int xmid = getMidX();
            int ymid = getMidY();

            ul = new Node(m, ix0, iy0, xmid, ymid);
            sum += ul->sum;
            if(ix1 > xmid)
            {
                ur = new Node(m, xmid + 1, iy0, ix1, ymid);
                sum += ur->sum;
            }
            if(iy1 > ymid)
            {
                dl = new Node(m, ix0, ymid + 1, xmid, iy1);
                sum += dl->sum;
            }
            if(iy1 > ymid && ix1 > xmid)
            {
                dr = new Node(m, xmid + 1, ymid + 1, ix1, iy1);
                sum += dr->sum;
            }
        }

        long long update(int rx, int ry, long long val)
        {
            if(rx == x0 && ry == y0 && x0 == x1 && y0 == y1)
            {
                long long d = val - sum;
                sum = val;
                return d;
            }

            int xmid = getMidX();
            int ymid = getMidY();

            long long d = 0;
            if(rx <= xmid && ry <= ymid)
            {
                d = ul->update(rx, ry, val);
            }
            else if(rx > xmid && ry <= ymid)
            {
                d = ur->update(rx, ry, val);
            }
            else if(rx <= xmid && ry > ymid)
            {
                d = dl->update(rx, ry, val);
            }
            else if(rx > xmid && ry > ymid)
            {
                d = dr->update(rx, ry, val);
            }
            sum += d;
            return d;
        }

        long long get(int rx0, int ry0, int rx1, int ry1)
        {
            if(rx0 == x0 && rx1 == x1 && ry0 == y0 && ry1 == y1)
            {
                return sum;
            }
            //
            int xmid = getMidX();
            int ymid = getMidY();

            long long d = 0;
            if(rx0 <= xmid && ry0 <= ymid)
            {
                d += ul->get(rx0, ry0, min(xmid, rx1), min(ymid, ry1));
            }
            if(rx1 > xmid && ry0 <= ymid)
            {
                d += ur->get(max(rx0, xmid + 1), ry0, rx1, min(ymid, ry1));
            }
            if(rx0 <= xmid && ry1 > ymid)
            {
                d += dl->get(rx0, max(ymid + 1, ry0), min(rx1, xmid), ry1);
            }
            if(rx1 > xmid && ry1 > ymid)
            {
                d += dr->get(max(rx0, xmid + 1), max(ry0, ymid + 1), rx1, ry1);
            }

            return d;
        }
    private:
        int getMidX(){ return x0 + (x1 - x0) / 2; }
        int getMidY(){ return y0 + (y1 - y0) / 2; }
    private:
        //    mem vars
        long long sum;
        int x0, x1;
        int y0, y1;

        Node *ul;
        Node *ur;
        Node *dl;
        Node *dr;
    };

    class NumMatrix
    {
        Node *pSeg;
    public:
        NumMatrix(vector<vector<int>> &matrix)
        {
            int h = matrix.size();
            if(!h) return;
            int w = matrix[0].size();

            pSeg = new Node(matrix, 0, 0, w - 1, h - 1);
        }

        void update(int row, int col, int val)
        {
            if(pSeg)
                pSeg->update(col, row, val);
        }

        int sumRegion(int row1, int col1, int row2, int col2)
        {
            if(pSeg)
                return pSeg->get(col1, row1, col2, row2);
            return 0;
        }
    };

时间: 2024-10-04 00:04:20

LeetCode "Range Sum Query 2D - Mutable"的相关文章

[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

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,

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.

[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

[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 - Mutable

原题链接在这里:https://leetcode.com/problems/range-sum-query-mutable/ 题目: Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. The update(i, val) function modifies nums by updating the element at index i to v

leetcode Range Sum Query - Immutable

题目连接 https://leetcode.com/problems/range-sum-query-immutable/ Range Sum Query - Immutable Description 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] su

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