diff --git a/segmenttree/binary_indexed_tree.hpp b/segmenttree/binary_indexed_tree.hpp index 88ace04b..0107b6e1 100644 --- a/segmenttree/binary_indexed_tree.hpp +++ b/segmenttree/binary_indexed_tree.hpp @@ -8,6 +8,17 @@ template struct BIT { int n; std::vector data; BIT(int len = 0) : n(len), data(len) {} + BIT(const T *const a, const int len) : BIT(len) { + T p = 0; + for (int i = 0; i < n; i++) { + data[i] = p += a[i]; + } + for (int j = n - 1; j > 1; j--) { + const int i = j & (j + 1); + if (i > 0) data[j] -= data[i - 1]; + } + } + BIT(const std::vector &v) : BIT(v.data(), int(v.size())) {} void reset() { std::fill(data.begin(), data.end(), T(0)); } void add(int pos, T v) { // a[pos] += v pos++;