-
Notifications
You must be signed in to change notification settings - Fork 170
/
Copy pathFenwick Tree.cpp
69 lines (56 loc) · 1.08 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include<bits/stdc++.h>
using namespace std;
vector<int> BIT;
void Update(int idx,int val)
{
idx++;
while(idx <= BIT.size())
{
BIT[idx] += val;
idx += idx & (-idx);
}
}
int Sum_range(int idx)
{
int sum = 0;
idx++;
while(idx > 0)
{
sum += BIT[idx];
idx -= idx & (-idx);
}
return sum;
}
void Fenwick_tree(vector<int> arr)
{
int n = arr.size();
BIT = vector<int>(n+1,0);
for(int i = 0;i<n;i++)
Update(i,arr[i]);
}
int main()
{
vector<int> arr{1,3,5};
Fenwick_tree(arr);
int l,r;
// for queries
// Query 1 : Range Sum from arr[0,2]
l = 0, r = 2;
if(l == 0)
cout<<Sum_range(r)<<endl;
else
cout<<Sum_range(r) - Sum_range(l-1)<<endl;
// Query 2 : Update value at index i to val
int idx = 1;
int val = 2;
int diff = val - arr[idx];
Update(1,diff);
arr[idx] = val;
// Query 3 : Range Sum from arr[0,2]
l = 0, r = 2;
if(l == 0)
cout<<Sum_range(r)<<endl;
else
cout<<Sum_range(r) - Sum_range(l-1)<<endl;
return 0;
}