Answers for "binary index tree c++"

C++
1

binary index tree c++

class bit{
public:
    int n;
    vector<int> tree;

    bit(){};
    bit(int _n){
        n=_n;
        tree.resize(n+1);
    };

    void update(int idx,int val){
        while(idx<=n){
            tree[idx]+=val;
            idx+=idx&(-idx);
        }
    };

    int read(int idx){
        int res=0;
        while(idx>0){
            res+=tree[idx];
            idx-=idx&(-idx);
        }
        return res;
    };

    int sum(int L,int R){
        return read(R)-read(L-1);
    };
};
Posted by: Guest on April-30-2021

Browse Popular Code Answers by Language