forked from IEEESFIT1/OpenOctober
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fenwick_tree.cpp
55 lines (44 loc) · 1 KB
/
Fenwick_tree.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
#include <iostream>
using namespace std;
int getSum(int Fenwick[], int index)
{
int sum = 0;
index = index + 1;
while (index>0)
{
sum += Fenwick[index];
index -= index & (-index);
}
return sum;
}
void updateBIT(int Fenwick[], int n, int index, int val)
{
index = index + 1;
while (index <= n)
{
Fenwick[index] += val;
index += index & (-index);
}
}
int *constructBITree(int arr[], int n)
{
int *Fenwick = new int[n+1];
for (int i=1; i<=n; i++)
Fenwick[i] = 0;
for (int i=0; i<n; i++)
updateBIT(Fenwick, n, i, arr[i]);
return Fenwick;
}
int main()
{
int freq[] = {2, 1, 1, 3, 2, 3, 4, 5, 6, 7, 8, 9};
int n = sizeof(freq)/sizeof(freq[0]);
int *Fenwick = constructBITree(freq, n);
cout << "Sum of elements in arr[0..5] is "
<< getSum(Fenwick, 5);
freq[3] += 6;
updateBIT(Fenwick, n, 3, 6);
cout << "\nSum of elements in arr[0..5] after update is "
<< getSum(Fenwick, 5);
return 0;
}