From 5269330e748237c2a7992e95e3655bb74421b2fb Mon Sep 17 00:00:00 2001 From: Gabriel Duarte Date: Sun, 8 Nov 2015 23:19:15 -0200 Subject: [PATCH 1/2] Add Fenwick Tree algorithm - Gabriel123Duarte --- include/fenwick_tree.h | 44 +++++++++++++++++++++++++++++++++++++++ src/fenwick_tree_demo.cpp | 16 ++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 include/fenwick_tree.h create mode 100644 src/fenwick_tree_demo.cpp diff --git a/include/fenwick_tree.h b/include/fenwick_tree.h new file mode 100644 index 00000000..ba3f581c --- /dev/null +++ b/include/fenwick_tree.h @@ -0,0 +1,44 @@ +#ifndef __FENWICK_H__ +#define __FENWICK_H__ + +#include + +#define LSONE(x) (x & (-x)) + +class Fenwick +{ + private: + std::vector fen; + public: + Fenwick() {} + + // We don't use the index 0 + Fenwick(int n) + { + fen.assign(n + 1, 0); + } + + // RSQ 1..a + 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 \ No newline at end of file diff --git a/src/fenwick_tree_demo.cpp b/src/fenwick_tree_demo.cpp new file mode 100644 index 00000000..d5e294de --- /dev/null +++ b/src/fenwick_tree_demo.cpp @@ -0,0 +1,16 @@ +#include +#include "fenwick_tree.h" + +int main() +{ + Fenwick ft(5); + + ft.update(2, 1); + ft.update(4, 10); + + printf("%d\n", ft.rsq(1)); + + ft.update(1, 5); + printf("%d\n", ft.rsq(1)); + return 0; +} \ No newline at end of file From 99defa0c2139648cc551509bf583457137598da5 Mon Sep 17 00:00:00 2001 From: Gabriel Duarte Date: Mon, 9 Nov 2015 08:53:33 -0200 Subject: [PATCH 2/2] Added some comments in the header file --- include/.goutputstream-UUQE8X | 61 +++++++++++++++++++++++++++++++++++ include/fenwick_tree.h | 23 +++++++++++-- 2 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 include/.goutputstream-UUQE8X diff --git a/include/.goutputstream-UUQE8X b/include/.goutputstream-UUQE8X new file mode 100644 index 00000000..e92d1ab5 --- /dev/null +++ b/include/.goutputstream-UUQE8X @@ -0,0 +1,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 __FENWICK_H__ +#define __FENWICK_H__ + +#include + +#define LSONE(x) (x & (-x)) + +class Fenwick +{ + private: + // Vector representing the table + std::vector 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 diff --git a/include/fenwick_tree.h b/include/fenwick_tree.h index ba3f581c..e92d1ab5 100644 --- a/include/fenwick_tree.h +++ b/include/fenwick_tree.h @@ -1,3 +1,19 @@ +/******************************************************************************* + * 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 __FENWICK_H__ #define __FENWICK_H__ @@ -8,17 +24,18 @@ class Fenwick { private: + // Vector representing the table std::vector fen; public: Fenwick() {} - // We don't use the index 0 + // We don't use the index 0, because it is the base case Fenwick(int n) { fen.assign(n + 1, 0); } - // RSQ 1..a + // Calculate the int rsq(int a) { int ans = 0; @@ -41,4 +58,4 @@ class Fenwick } }; -#endif \ No newline at end of file +#endif