-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathfenwick_tree.h
More file actions
61 lines (52 loc) · 1.28 KB
/
fenwick_tree.h
File metadata and controls
61 lines (52 loc) · 1.28 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
51
52
53
54
55
56
57
58
59
60
61
/*******************************************************************************
* Fenwick Tree
*
* Data structure providing prefix sums and modify the table in O(log n) - n is the size o the table.
*
* In this algorithm we use two functions:
* - RSQ - This function calculates the range sum query in O(log n)
* - Update - This function adjusts the values in the given range in O(log n)
*
* https://en.wikipedia.org/wiki/Fenwick_tree
*
* @author Gabriel Duarte (gabriellagoa10@yahoo.com.br)
* @github Gabriel123Duarte
*
******************************************************************************/
#ifndef ALGO_FENWICK_H__
#define ALGO_FENWICK_H__
#include <vector>
#define LSONE(x) (x & (-x))
class Fenwick
{
private:
// Vector representing the table
std::vector<int> fen;
public:
Fenwick() {}
// We don't use the index 0, because it is the base case
Fenwick(int n)
{
fen.assign(n + 1, 0);
}
// Calculate the
int rsq(int a)
{
int ans = 0;
for(; a; a -= LSONE(a))
ans += fen[a];
return ans;
}
// RSQ a..b
inline int rsq(int a, int b)
{
return rsq(b) - (a == 1 ? 0 : rsq(a - 1));
}
// Update the value of the k-th element by x
void update(int k, int x)
{
for(; k < (int)fen.size(); k += LSONE(k))
fen[k] += x;
}
};
#endif