-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathFenwickTree.cpp
72 lines (48 loc) · 1.14 KB
/
FenwickTree.cpp
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <vector>
using namespace std;
//for calculating the sum from 0 to index
int Sum(vector<int> &FTree, int index)
{
int sum = 0;
index = index + 1;
while (index>0)
{
sum += FTree[index];
index -= index & (-index); //To go to next node of tree
}
return sum;
}
//To update the value at index
void Update(vector<int> &FTree, int n, int index, int val)
{
index+=1;
while(index<=n)
{
FTree[index]+=val;
index += index & (-index); //To move to its parent
}
}
//To construct Fenwick tree
vector<int> constructFT(vector<int> a,int n)
{
vector<int> FTree(n+1,0);
for (int i = 0; i < n; ++i)
{
Update(FTree,n,i,a[i]); // Using update to construct it
}
return FTree;
}
int main()
{
vector<int> a={1,4,6,5,3,2,5,9,8};
int n=a.size();
vector<int> FTree = constructFT(a,n);
cout << "Sum of elements(Before Update) of first 4 elements is "<< Sum(FTree, 5)<<endl;
a[3] += 10;
Update(FTree, n, 3, 10);
cout << "Sum of elements(After Update) of first 4 elements is "<< Sum(FTree, 5)<<endl;
return 0;
}
//g++ -std=c++11 FenwickTree.cpp
//./a.out