Skip to content

Commit 0359b8d

Browse files
committed
Merge pull request xtaci#39 from Gabriel123Duarte/master
Add Fenwick Tree algorithm - Gabriel123Duarte
2 parents cd47f96 + 99defa0 commit 0359b8d

File tree

3 files changed

+138
-0
lines changed

3 files changed

+138
-0
lines changed

include/.goutputstream-UUQE8X

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*******************************************************************************
2+
* Fenwick Tree
3+
*
4+
* Data structure providing prefix sums and modify the table in O(log n) - n is the size o the table.
5+
*
6+
* In this algorithm we use two functions:
7+
* - RSQ - This function calculates the range sum query in O(log n)
8+
* - Update - This function adjusts the values in the given range in O(log n)
9+
*
10+
* https://en.wikipedia.org/wiki/Fenwick_tree
11+
*
12+
* @author Gabriel Duarte ([email protected])
13+
* @github Gabriel123Duarte
14+
*
15+
******************************************************************************/
16+
17+
#ifndef __FENWICK_H__
18+
#define __FENWICK_H__
19+
20+
#include <vector>
21+
22+
#define LSONE(x) (x & (-x))
23+
24+
class Fenwick
25+
{
26+
private:
27+
// Vector representing the table
28+
std::vector<int> fen;
29+
public:
30+
Fenwick() {}
31+
32+
// We don't use the index 0, because it is the base case
33+
Fenwick(int n)
34+
{
35+
fen.assign(n + 1, 0);
36+
}
37+
38+
// Calculate the
39+
int rsq(int a)
40+
{
41+
int ans = 0;
42+
for(; a; a -= LSONE(a))
43+
ans += fen[a];
44+
return ans;
45+
}
46+
47+
// RSQ a..b
48+
inline int rsq(int a, int b)
49+
{
50+
return rsq(b) - (a == 1 ? 0 : rsq(a - 1));
51+
}
52+
53+
// Update the value of the k-th element by x
54+
void update(int k, int x)
55+
{
56+
for(; k < (int)fen.size(); k += LSONE(k))
57+
fen[k] += x;
58+
}
59+
};
60+
61+
#endif

include/fenwick_tree.h

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*******************************************************************************
2+
* Fenwick Tree
3+
*
4+
* Data structure providing prefix sums and modify the table in O(log n) - n is the size o the table.
5+
*
6+
* In this algorithm we use two functions:
7+
* - RSQ - This function calculates the range sum query in O(log n)
8+
* - Update - This function adjusts the values in the given range in O(log n)
9+
*
10+
* https://en.wikipedia.org/wiki/Fenwick_tree
11+
*
12+
* @author Gabriel Duarte ([email protected])
13+
* @github Gabriel123Duarte
14+
*
15+
******************************************************************************/
16+
17+
#ifndef __FENWICK_H__
18+
#define __FENWICK_H__
19+
20+
#include <vector>
21+
22+
#define LSONE(x) (x & (-x))
23+
24+
class Fenwick
25+
{
26+
private:
27+
// Vector representing the table
28+
std::vector<int> fen;
29+
public:
30+
Fenwick() {}
31+
32+
// We don't use the index 0, because it is the base case
33+
Fenwick(int n)
34+
{
35+
fen.assign(n + 1, 0);
36+
}
37+
38+
// Calculate the
39+
int rsq(int a)
40+
{
41+
int ans = 0;
42+
for(; a; a -= LSONE(a))
43+
ans += fen[a];
44+
return ans;
45+
}
46+
47+
// RSQ a..b
48+
inline int rsq(int a, int b)
49+
{
50+
return rsq(b) - (a == 1 ? 0 : rsq(a - 1));
51+
}
52+
53+
// Update the value of the k-th element by x
54+
void update(int k, int x)
55+
{
56+
for(; k < (int)fen.size(); k += LSONE(k))
57+
fen[k] += x;
58+
}
59+
};
60+
61+
#endif

src/fenwick_tree_demo.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <cstdio>
2+
#include "fenwick_tree.h"
3+
4+
int main()
5+
{
6+
Fenwick ft(5);
7+
8+
ft.update(2, 1);
9+
ft.update(4, 10);
10+
11+
printf("%d\n", ft.rsq(1));
12+
13+
ft.update(1, 5);
14+
printf("%d\n", ft.rsq(1));
15+
return 0;
16+
}

0 commit comments

Comments
 (0)