dfenwick tree code c++
C++
// C++ code to demonstrate operations of Binary Index Tree
#include <iostream>
using namespace std;
/* n --> No. of elements present in input array.
BITree[0..n] --> Array that represents Binary Indexed Tree.
arr[0..n-1] --> Input array for which prefix sum is evaluated. */
// Returns sum of arr[0..index]. This function assumes
// that the array is preprocessed and partial sums of
// array elements are stored in BITree[].
int getSum(int BITree[], int index)
{
int sum = 0; // Iniialize result
// index in BITree[] is 1 more than the index in arr[]
index = index + 1;
// Traverse ancestors of BITree[index]
while (index>0)
{
// Add current element of BITree to sum
sum += BITree[index];
// Move index to parent node in getSum View
index -= index & (-index);
}
return sum;
}
// Updates a node in Binary Index Tree (BITree) at given index
// in BITree. The given value 'val' is added to BITree[i] and
// all of its ancestors in tree.
void updateBIT(int BITree[], int n, int index, int val)
{
// index in BITree[] is 1 more than the index in arr[]
index = index + 1;
// Traverse all ancestors and add 'val'
while (index <= n)
{
// Add 'val' to current node of BI Tree
BITree[index] += val;
// Update index to that of parent in update View
index += index & (-index);
}
}
// Constructs and returns a Binary Indexed Tree for given
// array of size n.
int *constructBITree(int arr[], int n)
{
// Create and initialize BITree[] as 0
int *BITree = new int[n+1];
for (int i=1; i<=n; i++)
BITree[i] = 0;
// Store the actual values in BITree[] using update()
for (int i=0; i<n; i++)
updateBIT(BITree, n, i, arr[i]);
// Uncomment below lines to see contents of BITree[]
//for (int i=1; i<=n; i++)
// cout << BITree[i] << " ";
return BITree;
}
// Driver program to test above functions
int main()
{
int freq[] = {2, 1, 1, 3, 2, 3, 4, 5, 6, 7, 8, 9};
int n = sizeof(freq)/sizeof(freq[0]);
int *BITree = constructBITree(freq, n);
cout << "Sum of elements in arr[0..5] is "
<< getSum(BITree, 5);
// Let use test the update operation
freq[3] += 6;
updateBIT(BITree, n, 3, 6); //Update BIT for above change in arr[]
cout << "\nSum of elements in arr[0..5] after update is "
<< getSum(BITree, 5);
return 0;
}
Also in C++:
- Title
- dijkstra in c++
- Category
- C++
- Title
- c++ garbage collection
- Category
- C++
- Title
- c++ get ascii value of char
- Category
- C++
- Title
- pass vector by reference c++
- Category
- C++
- Title
- substr c++
- Category
- C++
- Title
- c++ cout int
- Category
- C++
- Title
- struct c++
- Category
- C++
- Title
- c++ char print width
- Category
- C++
- Title
- checking an int in c++
- Category
- C++
- Title
- c++ short if
- Category
- C++
- Title
- c++ cli convert string to string^
- Category
- C++
- Title
- deque c++
- Category
- C++
- Title
- how to iterate trough a vector in c++
- Category
- C++
- Title
- string to number in c++
- Category
- C++
- Title
- leveling system c++
- Category
- C++
- Title
- how to switch to another branch in git
- Category
- C++
- Title
- appending a double to a string c++
- Category
- C++
- Title
- opencv compile c++
- Category
- C++
- Title
- c++ how to add something at the start of a vector
- Category
- C++
- Title
- insertion c++
- Category
- C++
- Title
- power c++
- Category
- C++
- Title
- queue stl c++
- Category
- C++
- Title
- 1d fixed length arrays c++
- Category
- C++
- Title
- bitset c++
- Category
- C++
- Title
- set in c++
- Category
- C++
- Title
- gfg right view of tree
- Category
- C++
- Title
- c++ random numbers
- Category
- C++
- Title
- range based for loop c++
- Category
- C++
- Title
- make an x using asterisk c++
- Category
- C++
- Title
- what does the modularity mean in c++
- Category
- C++
- Title
- subarray sum in c++
- Category
- C++
- Title
- formal parameter c++
- Category
- C++
- Title
- stoi c++
- Category
- C++
- Title
- c++ modulo make it give only positive numbers
- Category
- C++
- Title
- Runtime Error: Runtime ErrorFloating-point exception (SIGFPE
- Category
- C++
- Title
- how to return a vector c++
- Category
- C++
- Title
- loop through array c++
- Category
- C++
- Title
- c++ code to print hello world
- Category
- C++
- Title
- roscpp publish int32
- Category
- C++
- Title
- lambda c++
- Category
- C++
- Title
- how are graphics in games made
- Category
- C++
- Title
- how to show c++ binary files in sublime text
- Category
- C++
- Title
- how to dynamically allocate an array c++
- Category
- C++
- Title
- c++ how to return an empty vector
- Category
- C++
- Title
- how to get size of 2d vector in c++
- Category
- C++
- Title
- visual studio 2019 c++ tutorial project
- Category
- C++
- Title
- ue4 c++ how to open a blueprint widget
- Category
- C++
- Title
- length of string in c++
- Category
- C++
- Title
- c++ iterate over vector of pointers
- Category
- C++
- Title
- print type cpp
- Category
- C++
- Title
- how to take input in C++ in coding
- Category
- C++
- Title
- c++ throw exception
- Category
- C++
- Title
- c++ movment
- Category
- C++
- Title
- c++ printf char as hex
- Category
- C++
- Title
- find in string c++
- Category
- C++
- Title
- inserting an element in an set c++
- Category
- C++
- Title
- range of long long in c++
- Category
- C++
- Title
- c++ method name
- Category
- C++
- Title
- pass by reference c++
- Category
- C++
- Title
- find_if c++
- Category
- C++
- Title
- create a 2d array c++
- Category
- C++
- Title
- residuo en lenguaje c
- Category
- C++
- Title
- c++ sql
- Category
- C++
- Title
- knapsack
- Category
- C++
- Title
- delete memory c++
- Category
- C++
- Title
- how to check the datatype of a variable in c++
- Category
- C++
- Title
- set precision in c++
- Category
- C++
- Title
- maximum subarray sum in c++
- Category
- C++
- Title
- c++ try
- Category
- C++
- Title
- delete 2d dynamic array c++
- Category
- C++
- Title
- add a timer c++
- Category
- C++
- Title
- first prime numbers
- Category
- C++
- Title
- c++ compiler for sublime text
- Category
- C++
- Title
- how to allocate on heap in c++
- Category
- C++
- Title
- how to add a number after each number in an array with a for loop in C++
- Category
- C++
- Title
- c++ variable argument
- Category
- C++
- Title
- stl sort in c++
- Category
- C++
- Title
- how to iterate over unordered_map c++
- Category
- C++
- Title
- never gonna give you up
- Category
- C++
- Title
- string to vector c++
- Category
- C++
- Title
- C++ remove element from set
- Category
- C++
- Title
- cpp nan value
- Category
- C++
- Title
- what is a header in c++
- Category
- C++
- Title
- how to cout in c++
- Category
- C++
- Title
- substitution failure is not an error
- Category
- C++
- Title
- how to get os name in c++
- Category
- C++
- Title
- how to know the correct class of objects cpp
- Category
- C++
- Title
- pairs in c++
- Category
- C++
- Title
- c++ give options
- Category
- C++
- Title
- c++ sort
- Category
- C++
- Title
- c++ class method example
- Category
- C++
- Title
- % operator in c++
- Category
- C++
- Title
- arduino for command
- Category
- C++
- Title
- c++ switch case statement
- Category
- C++
- Title
- create a dictionary cpp
- Category
- C++
- Title
- map.erase in c++
- Category
- C++
- Title
- qt graphics scene map cursor position
- Category
- C++
- Title
- floor() in c++
- Category
- C++
- Title
- initialize 2d array c++
- Category
- C++
- Title
- eigenvalue of matrix c++ using Eigen
- Category
- C++