binary tree array index code example
Example: 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);
};
};