其他分享
首页 > 其他分享> > 307. 区域和检索-数组不可变

307. 区域和检索-数组不可变

作者:互联网

class NumArray {

    SegmentTree segmentTree;

    public NumArray(int[] nums) {

        segmentTree = new SegmentTree(nums);
    }

    public void update(int index, int val) {

        segmentTree.update(index, val);
    }

    public int sumRange(int left, int right) {

        return segmentTree.sumRange(left, right);
    }
}

/**
 * 使用线段树对区间求和,更新区间值
 */
 class SegmentTree {

    int[] data;
    int[] tree;

    public SegmentTree(int[] arr){

        data = arr;
        tree = new int[data.length * 4];

        buildSegmentTree(0, 0, data.length - 1);
    }

    public void buildSegmentTree(int treeIndex, int left, int right){

        if (left == right){

            tree[treeIndex] = data[left];
            return;
        }

        int mid = left + (right - left) / 2;

        buildSegmentTree(leftChild(treeIndex), left, mid);
        buildSegmentTree(rightChild(treeIndex), mid + 1, right);

        tree[treeIndex] = tree[leftChild(treeIndex)] + tree[rightChild(treeIndex)];
    }

    public void update(int index, int val){

        data[index] = val;
        update(0, 0, data.length - 1, index, val);
    }

    private void update(int treeIndex, int left, int right, int index, int val){

        if (left == right){

            tree[treeIndex] = val;
            return;
        }

        int mid = left + (right - left)/ 2;

        if (index >= mid + 1){
            update(rightChild(treeIndex), mid + 1, right, index, val);
        }
        else {
            update(leftChild(treeIndex), left, mid, index, val);
        }

        tree[treeIndex] = tree[leftChild(treeIndex)] + tree[rightChild(treeIndex)];
    }

    public int sumRange(int left, int right){

        return sumRange(0, 0, data.length - 1, left, right);
    }

    private int sumRange(int treeIndex, int l, int r, int left, int right){

        if (l == left && r == right){

            return tree[treeIndex];
        }

        int mid = l + (r - l) / 2;

        if (left >= mid + 1){
            return sumRange(rightChild(treeIndex), mid + 1, r, left, right);
        }
        else if (right <= mid){
            return sumRange(leftChild(treeIndex), l, mid, left, right);
        }
        else {

            int resLeft = sumRange(leftChild(treeIndex), l, mid, left, mid);
            int resRight = sumRange(rightChild(treeIndex), mid + 1, r, mid + 1, right);

            return resLeft + resRight;
        }
    }

    public int leftChild(int index){

        return 2 * index + 1;
    }

    public int rightChild(int index){

        return 2 * index + 2;
    }
 }

https://leetcode-cn.com/problems/range-sum-query-mutable/

标签:检索,right,treeIndex,int,tree,mid,307,数组,left
来源: https://www.cnblogs.com/taoyuann/p/15533440.html