微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

线段树——区间求和

#include <bits/stdc++.h>
using namespace std;
//以下是线段树的模板,让区间查询修改的时间复杂度到O(lgn);
class XianDuanTree{
private:
    vector<int> arr;
    vector<int> tree;
public:
    XianDuanTree(vector<int> &arr){
        this->arr = arr;
    }
    //建树,根据vector<int> arr;构建区间查询树vector<int>tree
    //start,end限定区间范围,node_index是这个结点在tree的索引;
    void build_tree(vector<int> &arr,vector<int> &tree,int start,int end,int node_index ){
        //只有一个值了,即叶子结点
        if(start == end){
            tree[node_index] = arr[start];
            return;
        }
        int left_index = 2*node_index+1;
        int right_index = 2*node_index+2;
        int mid = start+(end-start)/2;
        //递归;
        build_tree(arr,tree,start,mid,left_index);
        build_tree(arr,tree,mid+1,end,right_index);
        //后序位置
        tree[node_index] = tree[left_index]+tree[right_index];
    }
    vector<int> ues_build(){
        int n = arr.size();
        tree.resize(2*n,0);
        build_tree(arr,tree,0,n-1,0);
        return tree;
    }
    //查询区间和
    int query(vector<int> & arr,vector<int> & tree,int start,int end,int left,int  right,int node_index){
        if(left > end ||  right  < start) return 0;
        else if(left<=start && end <= right) return tree[node_index];
        else{
            int mid = start+(end-start)/2;
            int left_node = 2*node_index+1;
            int right_node = 2*node_index+2;
            int left_sum = query(arr,tree,start,mid,left,right,left_node);
            int right_sum = query(arr,tree,mid+1,end,left,right,right_node);
            int sum = left_sum+right_sum;
            return sum;
        }
    }
    int ues_query(int left,int right){
        return query(arr,tree,0,arr.size()-1,left,right,0);
    }
    //更新单点更新,arr数组
    //upadte_index为需要修改的arr中的数值下标,把值改成val
    void update(vector<int> & arr,vector<int> & tree,int start,int end,int node_index,int update_index,int val){
        if(start == end) {
            arr[start] = val;
            tree[node_index] = arr[start];
        } else{
            int left_node = 2*node_index+1;
            int right_node = 2*(node_index+1);
            int mid = start+(end-start)/2;
            if(update_index <= mid){
                update(arr,tree,start,mid,left_node,update_index,val);
            }else{
                update(arr,tree,mid+1,end,right_node,update_index,val);
            }
            tree[node_index] = tree[left_node]+tree[right_node];
        }
    }
    void use_update(int index,int val){
        update(arr,tree,0,arr.size()-1,0,index,val);
    }


};
int main() {
    vector<int> arr={93,90,50,50,1};
    XianDuanTree * xian = new XianDuanTree(arr);
    vector<int> tree = xian->ues_build();
    xian->use_update(4,2);
    for (int a:tree) {
        cout << a << " " <<endl;
    }
    int sum = xian->ues_query(2,4);
    return 0;
}

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐