Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions include/.goutputstream-UUQE8X
Original file line number Diff line number Diff line change
@@ -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 <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
61 changes: 61 additions & 0 deletions include/fenwick_tree.h
Original file line number Diff line number Diff line change
@@ -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 <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
16 changes: 16 additions & 0 deletions src/fenwick_tree_demo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <cstdio>
#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;
}