-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFenwickTree.cpp
More file actions
executable file
·38 lines (34 loc) · 880 Bytes
/
FenwickTree.cpp
File metadata and controls
executable file
·38 lines (34 loc) · 880 Bytes
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
#include <bits/stdc++.h>
using namespace std;
class FenwickTree{
private:
int pivot;
int max_num;
map<int,int> bits;
int total = 0;
public:
int lowbit(int x) {
return x & -x;
}
FenwickTree(int minn, int maxx){
this->pivot = minn*-1 + 1;
this->max_num = 2*maxx + this->pivot;
}
void update(int num, int count){
this->total += count;
int i = num + this->pivot;
while (i <= this->max_num){
this->bits[i] += count;
i += lowbit(i);
}
}
int getCount(int num){
int i = num + this->pivot;
int res = 0;
while (i > 0){
res += this->bits[i];
i -= lowbit(i);
}
return res;
}
}