Skip to content

Added new data structure Fenwick Tree #272

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 28, 2021
Merged
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
53 changes: 53 additions & 0 deletions Data_Structure/src/Trees/Fenwick_tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*Let, f be some reversible function and A be an array of integers of length N.
Fenwick tree is a data structure which:
1.calculates the value of function f in the given range [l,r] (i.e. f(Al,Al+1,…,Ar)) in O(logn) time;
2.updates the value of an element of A in O(logn) time;
3.requires O(N) memory, or in other words, exactly the same memory required for A;
4.is easy to use and code, especially, in the case of multidimensional arrays.
Note:-
Fenwick tree is also called Binary Indexed Tree, or just BIT .
Application:-
calculating the sum of a range (i.e. f(A1,A2,…,Ak)=A1+A2+⋯+Ak). */
#include <bits/stdc++.h>
using namespace std;


int FNT[1000000] = {0}; //fenwick tree array
int query(int i)
{
int ans = 0;
while (i > 0)
{
ans += FNT[i];
i = i - (i & (-i));
}
return ans;
}
void Build_update(int i, int inc, int N)
{
while (i <= N)
{
FNT[i] += inc;
i += (i & (-i));
}
}
int main()
{

int n;
cin >> n;
int a[n];
for (int i = 1; i <= n; i++)
{
cin >> a[i];
Build_update(i, a[i], n);
}
int q, l, r;
cin >> q;
while (q--)
{
cin >> l >> r;
cout << (query(r) - query(l - 1)) << endl;
}
return 0;
}