Created
October 7, 2014 03:13
-
-
Save tanitanin/dac5220fd6997b74fbc7 to your computer and use it in GitHub Desktop.
Binary Indexed Tree
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <vector> | |
template<class T> | |
class BIT { | |
public: | |
explicit BIT(size_t N=1) : _bit(N) {} | |
void add(size_t i, T val) { | |
while (i < _bit.size()){ | |
_bit[i] += val; | |
i += i & -i; | |
} | |
} | |
T sum(size_t i) { | |
T ret = 0; | |
while(i>0){ | |
ret += _bit[i]; | |
i &= (i - 1); | |
} | |
return ret; | |
} | |
private: | |
std::vector<T> _bit; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment