This documentation is automatically generated by online-judge-tools/verification-helper
#include "src/data_structure/fenwick_tree.hpp"
長さ $N$ の配列に対し,
を $O(\log N)$ 時間で処理することができます.
FenwickTree<T> fw(int n)
制約
int / uint / ll / ull / mint
計算量
void fw.add(int p, T x)
a[p] += x
をします.
制約
計算量
T fw.sum(int l, int r)
a[l] + a[l + 1] + ... + a[r - 1]
を返します.
制約
計算量
T fw.get(int x)
a[x]
を返します.
制約
計算量
#pragma once
#include "../template/template.hpp"
template <typename T>
struct FenwickTree {
FenwickTree(const int N)
: n(N), data(N) {}
void add(int p, const T& x) {
assert(0 <= p and p < n);
++p;
while(p <= n) {
data[p - 1] += x;
p += p & -p;
}
}
T sum(const int l, const int r) const {
assert(0 <= l and l <= r and r <= n);
return sum(r) - sum(l);
}
T get(const int x) const {
assert(0 <= x and x < n);
return sum(x + 1) - sum(x);
}
private:
int n;
vector<T> data;
inline T sum(int r) const {
T s = 0;
while(r > 0) {
s += data[r - 1];
r -= r & -r;
}
return s;
}
};
#line 2 "src/template/template.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<long long, long long>;
#define rep(i, a, b) for(long long i = (a); i < (b); ++i)
#define rrep(i, a, b) for(long long i = (a); i >= (b); --i)
constexpr long long inf = 4e18;
struct SetupIO {
SetupIO() {
ios::sync_with_stdio(0);
cin.tie(0);
cout << fixed << setprecision(30);
}
} setup_io;
#line 3 "src/data_structure/fenwick_tree.hpp"
template <typename T>
struct FenwickTree {
FenwickTree(const int N)
: n(N), data(N) {}
void add(int p, const T& x) {
assert(0 <= p and p < n);
++p;
while(p <= n) {
data[p - 1] += x;
p += p & -p;
}
}
T sum(const int l, const int r) const {
assert(0 <= l and l <= r and r <= n);
return sum(r) - sum(l);
}
T get(const int x) const {
assert(0 <= x and x < n);
return sum(x + 1) - sum(x);
}
private:
int n;
vector<T> data;
inline T sum(int r) const {
T s = 0;
while(r > 0) {
s += data[r - 1];
r -= r & -r;
}
return s;
}
};