Skip to content

Instantly share code, notes, and snippets.

@Luzhiled
Created July 20, 2020 16:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Luzhiled/03b4f5ede7c406b9e277a8426ccc6da6 to your computer and use it in GitHub Desktop.
Save Luzhiled/03b4f5ede7c406b9e277a8426ccc6da6 to your computer and use it in GitHub Desktop.
// fenwick tree {{{
template < typename data_type >
class fenwick_tree {
std::size_t size_;
std::vector< data_type > data_;
public:
fenwick_tree(size_t size) : size_(size), data_(size + 1) {}
data_type get_sum(int k) {
data_type sum = 0;
for (; k; k -= k & -k) sum += data_[k];
return sum;
}
void add(int k, data_type x) {
for (; k <= size_; k += k & -k) data_[k] += x;
}
};
// }}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment