From ef20fcf26d383d5e28e9cb437a104ba15327eba1 Mon Sep 17 00:00:00 2001 From: Tifa <62847935+Tiphereth-A@users.noreply.github.com> Date: Thu, 14 Nov 2024 17:43:48 +0800 Subject: [PATCH] feat(ds/fenwick): O(n) building --- src/code/ds/fenwick.hpp | 8 ++++++++ .../point_add_range_sum.fenwick.test.cpp | 9 +++------ .../static_range_sum.fenwick.test.cpp | 9 +++------ 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/code/ds/fenwick.hpp b/src/code/ds/fenwick.hpp index 424f6dab8..3c1138d68 100644 --- a/src/code/ds/fenwick.hpp +++ b/src/code/ds/fenwick.hpp @@ -12,6 +12,14 @@ class fenwick { public: //! [1, sz) CEXPE fenwick(u32 sz) : a(sz) { assert(sz > 1); } + //! [1, sz) + CEXP fenwick(spn 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) { diff --git a/src/test_cpverifier/library-checker-datastructure/point_add_range_sum.fenwick.test.cpp b/src/test_cpverifier/library-checker-datastructure/point_add_range_sum.fenwick.test.cpp index ed78951af..0fc50b1d1 100644 --- a/src/test_cpverifier/library-checker-datastructure/point_add_range_sum.fenwick.test.cpp +++ b/src/test_cpverifier/library-checker-datastructure/point_add_range_sum.fenwick.test.cpp @@ -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 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 f(a); fle_ (u32, i, 1, q) { u32 op, l, r; std::cin >> op >> l >> r; diff --git a/src/test_cpverifier/library-checker-datastructure/static_range_sum.fenwick.test.cpp b/src/test_cpverifier/library-checker-datastructure/static_range_sum.fenwick.test.cpp index cb1301f5b..74f56ad36 100644 --- a/src/test_cpverifier/library-checker-datastructure/static_range_sum.fenwick.test.cpp +++ b/src/test_cpverifier/library-checker-datastructure/static_range_sum.fenwick.test.cpp @@ -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 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 f(a); fle_ (u32, i, 1, q) { u32 l, r; std::cin >> l >> r;