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 可換群
7template<typename Abel=Abel::Sum<ll>>
9 using Type=typename Abel::Type;
10 FenwickTreeAbel()=default;
11
12 /// @brief サイズ n のFenwick Treeを構築する
14 this->n=n;
15 dat=vector<Type>(n,Abel::id());
16 }
17
18 /// @brief i 番目の要素に対し v[i] <- op(v[i], x) と更新する
19 /// @note O(log(N))
20 void add(int i, Type x) {
21 i++;
22 while(i<=n) {
23 dat[i-1]=Abel::op(dat[i-1],x);
24 i+=i&-i;
25 }
26 }
27
28 /// @brief 区間 [l, r) の群積を返す
29 /// @note O(log(N))
30 Type sum(int l, int r) {
31 return Abel::op(Abel::inv(sum(l)),sum(r));
32 }
33
34 /// @brief i 番目の要素を返す
35 /// @note O(log(N))
36 Type operator[](int i) { return sum(i,i+1); }
37
38 /// @brief 配列のサイズを返す
39 int size() { return n; }
40
41private:
42 int n;
43 vector<Type> dat;
44 Type sum(int r) {
45 Type ret=Abel::id();
46 while(r>0) {
47 ret=Abel::op(ret,dat[r-1]);
48 r-=r&-r;
49 }
50 return ret;
51 }
52};
Type sum(int l, int r)
区間 [l, r) の群積を返す
FenwickTreeAbel(int n)
サイズ n のFenwick Treeを構築する
int size()
配列のサイズを返す
void add(int i, Type x)
i 番目の要素に対し v[i] <- op(v[i], x) と更新する
Type operator[](int i)
i 番目の要素を返す
FenwickTreeAbel()=default