This documentation is automatically generated by online-judge-tools/verification-helper
#include "src/dataStructure/fenwick_tree.hpp"
フェニック木 です。
集合と演算の組 $(S, \oplus)$ について
を満たす時、$x\ \in S$ からなる列 $A$ に対して以下のクエリに答えることができます。
コンストラクタ
zawa::fenwick_tree<structure> fen(std::size_t n)
structure::id
で初期化するstructure
typename T
: 型static constexpr T id
: 単位元static T add(const T& a, const t& b)
: 演算static T inverse(const T& a)
: 逆元を返すsrc/utility/fenwick_tree
を確認くださいzawa::fenwick_tree<structure> fen(const std::vector<structure::T>& A)
A
で初期化するメンバ関数
void add(int pos, const T& v)
T sum(int l, int r)
int lower_bound(T val)
sum(0, r) >= val
を満たす最小のr
を返します。#pragma once
#include <vector>
namespace zawa {
template <class structure>
class fenwick_tree {
private:
using T = typename structure::T;
std::vector<T> dat;
int pow_two;
inline int lsb(int x) {
return x & -x;
}
T sum(int r) {
T res = 0;
while (r > 0) {
res = structure::add(res, dat[r]);
r -= lsb(r);
}
return res;
}
public:
fenwick_tree(std::size_t n) : dat(n + 1, structure::id), pow_two(std::__lg(n) + 1) {}
fenwick_tree(const std::vector<T>& A) : dat(A.size() + 1, structure::id), pow_two(std::__lg(A.size()) + 1) {
for (int i = 0 ; i < (int)A.size() ; i++) {
add(i, A[i]);
}
}
T sum(int l, int r) {
return structure::add(sum(r), structure::inverse(sum(l)));
}
void add(int pos, const T& v) {
pos++;
while (pos < (int)dat.size()) {
dat[pos] = structure::add(dat[pos], v);
pos += lsb(pos);
}
}
int lower_bound(T val) {
int res = 0;
T now = structure::id;
for (int x = (1 << pow_two) ; x > 0 ; x >>= 1) {
if (res + x < (int)dat.size()) {
T nxt = structure::add(now, dat[res + x]);
if (nxt < val) {
res += x;
now = nxt;
}
}
}
return res;
}
};
} // namespace zawa
#line 2 "src/dataStructure/fenwick_tree.hpp"
#include <vector>
namespace zawa {
template <class structure>
class fenwick_tree {
private:
using T = typename structure::T;
std::vector<T> dat;
int pow_two;
inline int lsb(int x) {
return x & -x;
}
T sum(int r) {
T res = 0;
while (r > 0) {
res = structure::add(res, dat[r]);
r -= lsb(r);
}
return res;
}
public:
fenwick_tree(std::size_t n) : dat(n + 1, structure::id), pow_two(std::__lg(n) + 1) {}
fenwick_tree(const std::vector<T>& A) : dat(A.size() + 1, structure::id), pow_two(std::__lg(A.size()) + 1) {
for (int i = 0 ; i < (int)A.size() ; i++) {
add(i, A[i]);
}
}
T sum(int l, int r) {
return structure::add(sum(r), structure::inverse(sum(l)));
}
void add(int pos, const T& v) {
pos++;
while (pos < (int)dat.size()) {
dat[pos] = structure::add(dat[pos], v);
pos += lsb(pos);
}
}
int lower_bound(T val) {
int res = 0;
T now = structure::id;
for (int x = (1 << pow_two) ; x > 0 ; x >>= 1) {
if (res + x < (int)dat.size()) {
T nxt = structure::add(now, dat[res + x]);
if (nxt < val) {
res += x;
now = nxt;
}
}
}
return res;
}
};
} // namespace zawa