diff --git a/FenwickTree.cpp b/FenwickTree.cpp new file mode 100644 index 0000000..a035497 --- /dev/null +++ b/FenwickTree.cpp @@ -0,0 +1,48 @@ +#include +#include + +template +class FenwickTree { + private: + int n; + std::vector bit; + public: + FenwickTree(int n) { + this -> n = n; + bit = std::vector(n+1); + } + + void update(int x, T delta) { + for(; x <= n; x += x & -x) { + bit[x] += delta; + } + } + + T query(int x) { + T res = 0; + for(; x > 0; x -= x & -x) { + res += bit[x]; + } + return res; + } + + T query_range(int l, int r) { + return query(r) - query(l-1); + } +}; + +int main() { + // creating FenwickTree object for index from 1 to 10 and type integers; + FenwickTree ft(10); + + //updates + ft.update(3, 5); + ft.update(4, 1); + ft.update(7, 2); + + //query in range + std::cout << ft.query_range(3, 7) <<'\n'; + std::cout << ft.query_range(4, 6) <<'\n'; + + return 0; +} diff --git a/Sorting Algos/CountSort.cpp b/Sorting Algos/CountSort.cpp deleted file mode 100644 index 364f8bb..0000000 --- a/Sorting Algos/CountSort.cpp +++ /dev/null @@ -1,62 +0,0 @@ -#include -using namespace std; -void countingSort(int *a,int n,int range) -{ - int *freq=new int[range]; - int *output=new int[n]; - memset(freq,0,sizeof(freq[0])*range); - /*cout<<"Range of elements entered "<=0;i--){ - freq[a[i]]--; - output[freq[a[i]]]=a[i]; - } - for(int i=0;i>s>>e; - n=abs(e-s+1); - int *a=new int[n]; - - for(int i=0;i>a[i]; - } - //cout<<"No of elements entered "< -#include -#define n 200 - +#include using namespace std; +void countingSort(int *a,int n,int range) +{ + int *freq=new int[range]; + int *output=new int[n]; + memset(freq,0,sizeof(freq[0])*range); + /*cout<<"Range of elements entered "<=0;i--){ - arr[i]=arr[i-1]; - } - - int outputarr[num]; - for(i=0;i=0;i--){ + freq[a[i]]--; + output[freq[a[i]]]=a[i]; + } + for(int i=0;i>s>>e; + n=abs(e-s+1); + int *a=new int[n]; -int main(){ - cout<<"Input format : Number of elements of array\n The elements\n"; - int k,i=0; - scanf("%d",&k); - int a[k]; - for(i=0;i>a[i]; - } - count_sort(a,k); - for(i=0;i>a[i]; + } + //cout<<"No of elements entered "<