Created
July 20, 2020 16:26
-
-
Save Luzhiled/03b4f5ede7c406b9e277a8426ccc6da6 to your computer and use it in GitHub Desktop.
This file contains 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
// 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