From 54795c85ded34b7f338a55a6726346783e62343d Mon Sep 17 00:00:00 2001 From: Reznov <78517110+alantudyk@users.noreply.github.com> Date: Wed, 27 Sep 2023 01:59:47 +0300 Subject: [PATCH 1/3] BIT O(n) init --- segmenttree/binary_indexed_tree.hpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/segmenttree/binary_indexed_tree.hpp b/segmenttree/binary_indexed_tree.hpp index 88ace04b..fca19855 100644 --- a/segmenttree/binary_indexed_tree.hpp +++ b/segmenttree/binary_indexed_tree.hpp @@ -8,6 +8,16 @@ template struct BIT { int n; std::vector data; BIT(int len = 0) : n(len), data(len) {} + BIT(const std::vector &v) : n(v.size()), data(v.size()) { + T p = 0; + for (int i = 0; i < n; i++) { + data[i] = p += v[i]; + } + for (int j = n - 1; j >= 0; j--) { + const int i = j & (j + 1); + if (i > 0) data[j] -= data[i - 1]; + } + } void reset() { std::fill(data.begin(), data.end(), T(0)); } void add(int pos, T v) { // a[pos] += v pos++; From 5997d20a9d00995f56de0d028fdc0663163683ba Mon Sep 17 00:00:00 2001 From: Reznov <78517110+alantudyk@users.noreply.github.com> Date: Wed, 27 Sep 2023 19:51:25 +0300 Subject: [PATCH 2/3] minus 2 unnecessary iterations --- segmenttree/binary_indexed_tree.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/segmenttree/binary_indexed_tree.hpp b/segmenttree/binary_indexed_tree.hpp index fca19855..61345bda 100644 --- a/segmenttree/binary_indexed_tree.hpp +++ b/segmenttree/binary_indexed_tree.hpp @@ -13,7 +13,7 @@ template struct BIT { for (int i = 0; i < n; i++) { data[i] = p += v[i]; } - for (int j = n - 1; j >= 0; j--) { + for (int j = n - 1; j > 1; j--) { const int i = j & (j + 1); if (i > 0) data[j] -= data[i - 1]; } From ef421ee468fe9e2e2990e91bf8ab75420001c51e Mon Sep 17 00:00:00 2001 From: Reznov <78517110+alantudyk@users.noreply.github.com> Date: Tue, 3 Oct 2023 00:12:52 +0300 Subject: [PATCH 3/3] spirit of modern C++ --- segmenttree/binary_indexed_tree.hpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/segmenttree/binary_indexed_tree.hpp b/segmenttree/binary_indexed_tree.hpp index 61345bda..0107b6e1 100644 --- a/segmenttree/binary_indexed_tree.hpp +++ b/segmenttree/binary_indexed_tree.hpp @@ -8,16 +8,17 @@ template struct BIT { int n; std::vector data; BIT(int len = 0) : n(len), data(len) {} - BIT(const std::vector &v) : n(v.size()), data(v.size()) { + BIT(const T *const a, const int len) : BIT(len) { T p = 0; for (int i = 0; i < n; i++) { - data[i] = p += v[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++; pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy