This documentation is automatically generated by online-judge-tools/verification-helper
#include "data_structure/fenwick/fenwick.hpp"定义于头文件 fenwick.hpp
template <typename Mono>
struct Fenwick;
维护长度 $n$ 的序列 $a$,支持单点聚合与前缀聚合查询,各操作 $O(\log n)$。
| 参数 | 说明 |
|---|---|
Mono |
聚合运算类型,满足 Monoid 要求 |
| 成员类型 | 定义 |
|---|---|
X |
typename Mono::Type |
构造
| 成员函数 | 说明 |
|---|---|
Fenwick(int n) |
构造 $n$ 个 e() 的序列,$O(n)$ |
Fenwick(const vector<X>& init) |
以 init 构造,$O(n)$ |
修改
| 成员函数 | 说明 |
|---|---|
void add(int x, const X& v) |
$a_x \leftarrow \text{op}(a_x, v)$,$O(\log n)$ |
查询
| 成员函数 | 说明 |
|---|---|
X query(int x) |
返回 $\text{op}(a_0, \dots, a_{x-1})$,$O(\log n)$ |
X query(int l, int r) |
返回 $\text{op}(a_l, \dots, a_{r-1})$,$O(\log n)$ |
int kth(X k) |
返回最大的 $x$ 使 $\text{op}(a_0, \dots, a_{x-1}) \le k$,$O(\log n)$ |
query(l, r) 需 Mono::inv。kth 要求前缀聚合关于下标单调不减。
Fenwick<AddMono<i64>> fen(5);
fen.add(0, 3);
fen.add(2, 4);
i64 s = fen.query(3);
i64 t = fen.query(1, 4);
int p = fen.kth(4);
Fenwick<GcdMono<i64>> g(vector<i64>{6, 10, 15});
i64 d = g.query(3);
#pragma once
#include <common.hpp>
template <typename Mono>
struct Fenwick {
using X = typename Mono::Type;
int n;
vector<X> a;
Fenwick(int n_ = 0) : n(n_), a(n, Mono::e()) {}
Fenwick(const vector<X>& init) : n(init.size()), a(init) {
for (int i = 1; i <= n; i++) {
int j = i + (i & -i);
if (j <= n) {
a[j - 1] = Mono::op(a[j - 1], a[i - 1]);
}
}
}
void add(int x, const X& v) {
assert(0 <= x && x < n);
for (int i = x + 1; i <= n; i += i & -i) {
a[i - 1] = Mono::op(a[i - 1], v);
}
}
X query(int x) {
assert(0 <= x && x <= n);
X res = Mono::e();
for (int i = x; i > 0; i -= i & -i) {
res = Mono::op(a[i - 1], res);
}
return res;
}
X query(int l, int r) {
assert(l <= r);
return Mono::op(Mono::inv(query(l)), query(r));
}
// lower_bound
int kth(X k) {
int x = 0;
for (int i = 1 << __lg(n); i; i /= 2) {
if (x + i <= n && k >= a[x + i - 1]) {
x += i;
k -= a[x - 1];
}
}
return x;
}
};