Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fenwick_tree O(n) init
  • Loading branch information
alantudyk committed Sep 27, 2023
commit 0cbde952f3fda839b2e9eb4ceb33535c543cb34b
9 changes: 9 additions & 0 deletions atcoder/fenwicktree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ template <class T> struct fenwick_tree {
public:
fenwick_tree() : _n(0) {}
explicit fenwick_tree(int n) : _n(n), data(n) {}
explicit fenwick_tree(const std::vector<T> &v) : _n(v.size()), data(v.size()) {
U p = 0;
int i, j = -1;
while (++j < _n)
data[j] = p += U(v[j]);
while (--j >= 0)
if ((i = j & (j + 1)) > 0)
data[j] -= data[i - 1];
}

void add(int p, T x) {
assert(0 <= p && p < _n);
Expand Down