-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfenwickTree.cpp
64 lines (53 loc) · 1.24 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
//Fenwick Tree is a linear array which contains logical values to find the cumulative sums
//Array elements are inserted using fenwick logic
#include <bits/stdc++.h>
#include <assert.h>
#define lli long long int
#define mod 1000000007
using namespace std;
void updateBit(int BIT[], int x, int size, int index)
{
while(index <= size){
BIT[index] += x;
index += (index & -index);
}
}
int getSum(int index, int BIT[])
{
int sum = 0;
while(index > 0){
sum += BIT[index];
index -= (index & -index);
}
return sum;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cout<<"Enter Size of array\n";
cin>>n;
int arr[n];
for (int i = 0; i < n; ++i) cin>>arr[i];
int BIT[n+1];
for (int i = 0; i <= n; ++i){
BIT[i] = 0;
}
for (int i = 0; i < n; ++i){
updateBit(BIT, arr[i], n,i+1);
}
for (int i = 0; i <= n; ++i) cout<<BIT[i]<<" ";
cout<<endl;
cout<<"Enter the index to find the sum from 0 to index\n";
int index;
cin>>index;
updateBit(BIT, 9, n, 3 + 1); //addding 9 to index 3
cout<<"Sum of the elements from 0 to "<<index<<" is "<<getSum(index+1, BIT)<<endl;
return 0;
assert(false);
}
//Input
//11
//3 2 -1 6 5 4 -3 3 7 2 3