Skip to content

Commit

Permalink
feat(ds/fenwick): O(n) building
Browse files Browse the repository at this point in the history
  • Loading branch information
Tiphereth-A committed Nov 14, 2024
1 parent a62b665 commit ef20fcf
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
8 changes: 8 additions & 0 deletions src/code/ds/fenwick.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ class fenwick {
public:
//! [1, sz)
CEXPE fenwick(u32 sz) : a(sz) { assert(sz > 1); }
//! [1, sz)
CEXP fenwick(spn<T> data) : fenwick((u32)data.size()) {
const u32 sz = (u32)data.size();
flt_ (u32, i, 1, sz) {
a[i] += data[i];
if (u32 j = i + bit::lowbit(i); j < sz) a[j] += a[i];
}
}

//! [pos, sz), pos > 0
CEXP void add(u32 pos, cT_(T) x) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@ int main() {
std::cin.tie(nullptr)->std::ios::sync_with_stdio(false);
u32 n, q;
std::cin >> n >> q;
tifa_libs::ds::fenwick<u64> f(n + 1);
u64 x;
fle_ (u32, i, 1, n) {
std::cin >> x;
f.add(i, x);
}
vecuu a(n + 1);
fle_ (u32, i, 1, n) std::cin >> a[i];
tifa_libs::ds::fenwick<u64> f(a);
fle_ (u32, i, 1, q) {
u32 op, l, r;
std::cin >> op >> l >> r;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@ int main() {
std::cin.tie(nullptr)->std::ios::sync_with_stdio(false);
u32 n, q;
std::cin >> n >> q;
tifa_libs::ds::fenwick<u64> f(n + 1);
u64 x;
fle_ (u32, i, 1, n) {
std::cin >> x;
f.add(i, x);
}
vecuu a(n + 1);
fle_ (u32, i, 1, n) std::cin >> a[i];
tifa_libs::ds::fenwick<u64> f(a);
fle_ (u32, i, 1, q) {
u32 l, r;
std::cin >> l >> r;
Expand Down

0 comments on commit ef20fcf

Please sign in to comment.