-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBIT.cpp
More file actions
50 lines (41 loc) · 1.06 KB
/
BIT.cpp
File metadata and controls
50 lines (41 loc) · 1.06 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
// Implementation of Fenwick Tree(Binary Indexed Tree)
//
// Running Time Complexity
// Build : O(N)
// Point Update : O(log N)
// Range Query : O(log N)
#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define int long long
#define pii pair<int, int>
const int N = 100005;
int A[N];
int tree[N]; // Fenwick tree takes O(N) memory
int n; // No of elements
void point_update(int ind, int val) { // Update A[ind] = A[ind] + val
while(ind <= n) {
tree[ind] += val;
ind += (ind & -ind);
}
}
int range_query(int ind) { // Sum of [1, ind]
int ans = 0;
while(ind > 0) {
ans += tree[ind];
ind -= (ind & -ind);
}
return ans;
}
int32_t main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
memset(tree, 0, sizeof(tree));
int i, n, x;
cout << "Enter the array lenght:";
cin >> n;
for(i = 1 ; i <= n ; i++) {
cin >> A[i];
point_update(i, A[i]);
}
cout << "Sum of the range [3, 14]: " << (range_query(14) - range_query(2)) << endl;
}