Skip to content

Commit c0cb4b3

Browse files
committed
Added fenwick tree with variations
1 parent c1c694e commit c0cb4b3

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
class BIT {
5+
public:
6+
int n;
7+
vector<int> ft;
8+
BIT(int n) {
9+
this->n = n;
10+
this->ft.assign(n+1, 0);
11+
}
12+
13+
void update(int ind, int val) {
14+
for(;ind < ft.size(); ind+=(ind&-ind))
15+
this->ft[ind] += val;
16+
}
17+
18+
int getSum(int ind) {
19+
int sum = 0;
20+
for(;ind>0; ind-=(ind&-ind))
21+
sum += this->ft[ind];
22+
return sum;
23+
}
24+
25+
int query(int l, int r) {
26+
return getSum(r) - getSum(l-1);
27+
}
28+
};
29+
30+
int main() {
31+
BIT b(100);
32+
return 0;
33+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
class BIT {
5+
public:
6+
int n;
7+
vector<int> ft;
8+
BIT(int n) {
9+
this->n = n;
10+
this->ft.assign(n+1, 0);
11+
}
12+
13+
void updateUtil(int ind, int val) {
14+
for(;ind < ft.size(); ind+=(ind&-ind))
15+
this->ft[ind] += val;
16+
}
17+
18+
void update(int l, int r, int val) {
19+
updateUtil(l, val);
20+
updateUtil(r+1, -val);
21+
}
22+
23+
int query(int ind) {
24+
int sum = 0;
25+
for(;ind>0; ind-=(ind&-ind))
26+
sum += this->ft[ind];
27+
return sum;
28+
}
29+
30+
};
31+
32+
int main() {
33+
BIT b(100);
34+
return 0;
35+
}

0 commit comments

Comments
 (0)