Kyopro Library
読み取り中…
検索中…
一致する文字列を見つけられません
fenwick_tree_abel.hpp
[詳解]
1
#
pragma
once
2
#
include
"../../kyopro_library/template.hpp"
3
#
include
"../../kyopro_library/others/abel.hpp"
4
5
/// @brief Fenwick Tree
6
/// @tparam Abel 可換群
7
template
<
typename
Abel=Abel::Sum<ll>>
8
9
struct
FenwickTree {
10
using
Type=
typename
Abel::Type;
11
12
FenwickTree
()=
default
;
13
14
/// @brief サイズ n のFenwick Treeを構築する
15
16
FenwickTree
(
int
n) {
17
this
->n=n;
18
dat.assign(n,Abel::id());
19
}
20
21
/// @brief i 番目の要素に対し v[i] <- op(v[i], x) と更新する
22
/// @note O(log(N))
23
24
void
add
(
int
i, Type x) {
25
i++;
26
while
(i<=n) {
27
dat[i-1]=Abel::op(dat[i-1],x);
28
i+=i&-i;
29
}
30
}
31
32
/// @brief 区間 [l, r) の群積を返す
33
/// @note O(log(N))
34
35
Type
sum
(
int
l,
int
r) {
36
return
Abel::op(Abel::inv(sum(l)),sum(r));
37
}
38
39
/// @brief i 番目の要素を返す
40
/// @note O(log(N))
41
Type
operator
[](
int
i) {
return
sum(i,i+1); }
42
43
/// @brief 配列のサイズを返す
44
int
size
() {
return
n; }
45
46
private
:
47
int
n;
48
vector<Type> dat;
49
Type sum(
int
r) {
50
Type ret=Abel::id();
51
while
(r>0) {
52
ret=Abel::op(ret,dat[r-1]);
53
r-=r&-r;
54
}
55
return
ret;
56
}
57
};
FenwickTree::FenwickTree
FenwickTree(int n)
サイズ n のFenwick Treeを構築する
Definition
fenwick_tree_abel.hpp:16
FenwickTree::operator[]
Type operator[](int i)
i 番目の要素を返す
Definition
fenwick_tree_abel.hpp:41
FenwickTree::FenwickTree
FenwickTree()=default
FenwickTree::sum
Type sum(int l, int r)
区間 [l, r) の群積を返す
Definition
fenwick_tree_abel.hpp:35
FenwickTree::add
void add(int i, Type x)
i 番目の要素に対し v[i] <- op(v[i], x) と更新する
Definition
fenwick_tree_abel.hpp:24
FenwickTree::size
int size()
配列のサイズを返す
Definition
fenwick_tree_abel.hpp:44
data_structure
fenwick_tree_abel.hpp
構築:
1.13.2