Advertisement
istinishat

Binary Indexed Tree(Fenwick)

Nov 2nd, 2016
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. #define MAX 100004
  5.  
  6. int fen[MAX],n;
  7.  
  8. //pos is 1 based
  9. void update(int pos,int val){
  10.     for(int i=pos;i<=n;i+= i& -i)
  11.         fen[i]+=val;
  12. }
  13.  
  14. int sum(int pos){
  15.     int ans=0;
  16.     for(int i=pos;i;i-= (i & -i))
  17.         ans+=fen[i];
  18.     return ans;
  19. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement