-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBITree.cpp
More file actions
53 lines (50 loc) · 1.32 KB
/
BITree.cpp
File metadata and controls
53 lines (50 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <vector>
using namespace std;
// Binary Index Tree
class BinaryIndexTree{
private:
vector<int> BIT;
public:
// Returns sum of arr[0..index]. This function assumes
// that the array is preprocessed and partial sums of
// array elements are stored in BIT[].
int getSum(int index){
int sum = 0;
index = index+1;
while(index > 0){
sum += BIT[index];
index = index - (index & -index);
}
return sum;
}
// Updates a node in Binary Index Tree (BIT) at given index
// in BIT. The given value 'val' is added to BIT[i] and
// all of its ancestors in tree.
void updateBIT(int index, int value){
// index in BIT[] is 1 more than the index in arr[]
index = index + 1;
int n = BIT.size();
while(index < n){
BIT[index] += value;
index = index + (index & -index); // ancestors, 1, 2, 4, 8, 16 ...
}
}
// Constructs and returns a Binary Indexed Tree for given array of size n.
BinaryIndexTree(vector<int> arr){
BIT.assign(arr.size()+1, 0);
for(int i = 0; i < arr.size(); i++){
updateBIT(i, arr[i]);
}
}
};
int main(void){
vector<int> arr = {2, 1, 1, 3, 2, 3, 4, 5, 6, 7, 8, 9};
BinaryIndexTree BITree(arr);
for(int i = 0; i <= arr.size(); i++){
cout<< BITree.getSum(i) <<" ";
}
cout<<endl;
cout << "Sum of elements in arr[0..5] is " << BITree.getSum(5)<<endl;
return 0;
}