This documentation is automatically generated by online-judge-tools/verification-helper
#pragma once
#include <cassert>
#include <vector>
#include "../utility/ceil_log2.cpp"
template <class G> class FenwickTree {
using T = typename G::Type;
int logn;
std::vector<T> data;
public:
explicit FenwickTree(const int size = 0) {
logn = ceil_log2(size + 1) - 1;
data = std::vector<T>(size + 1, G::identity());
}
int size() const { return data.size() - 1; }
void add(int i, const T& x) {
assert(0 <= i and i < size());
i += 1;
while (i <= size()) {
data[i] = G::operation(data[i], x);
i += i & -i;
}
}
T fold() const { return fold(0, size()); }
T fold(int l, int r) const {
assert(0 <= l and l <= r and r <= size());
T ret = G::identity();
while (l < r) {
ret = G::operation(ret, data[r]);
r -= r & -r;
}
while (r < l) {
ret = G::operation(ret, G::inverse(data[l]));
l -= l & -l;
}
return ret;
}
template <class F> int max_right(const F& f) const {
assert(f(G::identity()));
int i = 0;
T sum = G::identity();
for (int k = (1 << logn); k > 0; k >>= 1) {
if (i + k <= size() && f(G::operation(sum, data[i + k]))) {
sum = G::operation(sum, data[i += k]);
}
}
return i;
}
};
#line 2 "container/fenwick_tree.cpp"
#include <cassert>
#include <vector>
#line 2 "internal/enable_avx2.cpp"
#ifdef ENABLE_AVX2
#define TARGET_AVX2 __attribute__((target("avx2")))
#else
#define TARGET_AVX2
#endif
#line 2 "utility/int_alias.cpp"
#include <cstdint>
using i32 = std::int32_t;
using u32 = std::uint32_t;
using i64 = std::int64_t;
using u64 = std::uint64_t;
using i128 = __int128_t;
using u128 = __uint128_t;
#line 4 "utility/countl_zero.cpp"
TARGET_AVX2 constexpr int countl_zero(u64 x) {
#ifdef __GNUC__
return x == 0 ? 64 : __builtin_clzll(x);
#else
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
x |= x >> 32;
return 64 - countr_zero(~x);
#endif
}
#line 4 "utility/bit_width.cpp"
TARGET_AVX2 constexpr int bit_width(const u64 x) { return 64 - countl_zero(x); }
#line 5 "utility/ceil_log2.cpp"
TARGET_AVX2 constexpr int ceil_log2(const u64 x) {
#ifdef __GNUC__
return x == 0 ? 0 : bit_width(x - 1);
#else
int e = 0;
while (((u64)1 << e) < x) ++e;
return e;
#endif
}
#line 5 "container/fenwick_tree.cpp"
template <class G> class FenwickTree {
using T = typename G::Type;
int logn;
std::vector<T> data;
public:
explicit FenwickTree(const int size = 0) {
logn = ceil_log2(size + 1) - 1;
data = std::vector<T>(size + 1, G::identity());
}
int size() const { return data.size() - 1; }
void add(int i, const T& x) {
assert(0 <= i and i < size());
i += 1;
while (i <= size()) {
data[i] = G::operation(data[i], x);
i += i & -i;
}
}
T fold() const { return fold(0, size()); }
T fold(int l, int r) const {
assert(0 <= l and l <= r and r <= size());
T ret = G::identity();
while (l < r) {
ret = G::operation(ret, data[r]);
r -= r & -r;
}
while (r < l) {
ret = G::operation(ret, G::inverse(data[l]));
l -= l & -l;
}
return ret;
}
template <class F> int max_right(const F& f) const {
assert(f(G::identity()));
int i = 0;
T sum = G::identity();
for (int k = (1 << logn); k > 0; k >>= 1) {
if (i + k <= size() && f(G::operation(sum, data[i + k]))) {
sum = G::operation(sum, data[i += k]);
}
}
return i;
}
};