SHARE
TWEET

Fenwick Construction Algorithm

a guest Jun 26th, 2015 82 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define n 100
  2. #include <iostream>
  3. #include <math.h>
  4.  
  5. int tree[n];
  6. int arr[n] = { /*...values...*/ };
  7.  
  8. int main(int argc, char *argv[]) {
  9.         for(int i=1; i<=n; i++) {
  10.                 int tree_val = arr[i];
  11.                 int incrementer = (i&(-i))>>1;
  12.                 for(int index = (i-(incrementer<<1))+incrementer; incrementer>0; incrementer>>=1, index+=incrementer) {
  13.                         tree_val+=tree[index];
  14.                 }
  15.                 tree[i] = tree_val;
  16.         }
  17. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top