range_add_point_get.hpp¶
#include "noya/range_add_point_get.hpp"
#ifndef NOYA_RANGE_ADD_POINT_GET_HPP
#define NOYA_RANGE_ADD_POINT_GET_HPP 1
#include "noya/point_add_range_sum.hpp"
namespace noya {
template <class T, class C = noya::fenwick<T>> struct range_add_point_get {
int N;
C ST;
range_add_point_get(int _N) : ST(_N), N(_N) {}
/// @brief Add v to [l, r).
void range_add(int l, int r, T v) {
ST.add(l, v);
if (r + 1 < N) {
ST.add(r + 1, -v);
}
}
T point_get(int x) { return ST.prod(0, x + 1); }
};
} // namespace noya
#endif // NOYA_RANGE_ADD_POINT_GET_HPP
#include <cassert>
#include <cmath>
#include <numeric>
#include <type_traits>
#include <vector>
namespace atcoder {
namespace internal {
#ifndef _MSC_VER
template <class T>
using is_signed_int128 =
typename std::conditional<std::is_same<T, __int128_t>::value ||
std::is_same<T, __int128>::value,
std::true_type,
std::false_type>::type;
template <class T>
using is_unsigned_int128 =
typename std::conditional<std::is_same<T, __uint128_t>::value ||
std::is_same<T, unsigned __int128>::value,
std::true_type,
std::false_type>::type;
template <class T>
using make_unsigned_int128 =
typename std::conditional<std::is_same<T, __int128_t>::value,
__uint128_t,
unsigned __int128>;
template <class T>
using is_integral = typename std::conditional<std::is_integral<T>::value ||
is_signed_int128<T>::value ||
is_unsigned_int128<T>::value,
std::true_type,
std::false_type>::type;
template <class T>
using is_signed_int = typename std::conditional<(is_integral<T>::value &&
std::is_signed<T>::value) ||
is_signed_int128<T>::value,
std::true_type,
std::false_type>::type;
template <class T>
using is_unsigned_int =
typename std::conditional<(is_integral<T>::value &&
std::is_unsigned<T>::value) ||
is_unsigned_int128<T>::value,
std::true_type,
std::false_type>::type;
template <class T>
using to_unsigned = typename std::conditional<
is_signed_int128<T>::value,
make_unsigned_int128<T>,
typename std::conditional<std::is_signed<T>::value,
std::make_unsigned<T>,
std::common_type<T>>::type>::type;
#else
template <class T> using is_integral = typename std::is_integral<T>;
template <class T>
using is_signed_int =
typename std::conditional<is_integral<T>::value && std::is_signed<T>::value,
std::true_type,
std::false_type>::type;
template <class T>
using is_unsigned_int =
typename std::conditional<is_integral<T>::value &&
std::is_unsigned<T>::value,
std::true_type,
std::false_type>::type;
template <class T>
using to_unsigned = typename std::conditional<is_signed_int<T>::value,
std::make_unsigned<T>,
std::common_type<T>>::type;
#endif
template <class T>
using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;
template <class T>
using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;
template <class T> using to_unsigned_t = typename to_unsigned<T>::type;
} // namespace internal
} // namespace atcoder
namespace atcoder {
// Reference: https://en.wikipedia.org/wiki/Fenwick_tree
template <class T> struct fenwick_tree {
using U = internal::to_unsigned_t<T>;
public:
fenwick_tree() : _n(0) {}
explicit fenwick_tree(int n) : _n(n), data(n) {}
void add(int p, T x) {
assert(0 <= p && p < _n);
p++;
while (p <= _n) {
data[p - 1] += U(x);
p += p & -p;
}
}
T sum(int l, int r) {
assert(0 <= l && l <= r && r <= _n);
return sum(r) - sum(l);
}
private:
int _n;
std::vector<U> data;
U sum(int r) {
U s = 0;
while (r > 0) {
s += data[r - 1];
r -= r & -r;
}
return s;
}
};
} // namespace atcoder
namespace noya {
template <class T> struct block {
int V, sqrtV;
block() {}
block(const int &_V) {
if (_V > 0) {
build(_V);
}
}
std::vector<T> point, blo;
void build(const int &_V) {
V = _V;
sqrtV = sqrt(V);
point.assign(V, 0);
blo.assign(V / sqrtV + 1, 0);
}
void add(int x, T v) {
assert(0 <= x && x < V);
int bel = x / sqrtV;
blo[bel] += v;
point[x] += v;
}
T query(int x) const {
assert(0 <= x && x <= V);
T res = 0;
int bel = x / sqrtV;
for (int i = 0; i < bel; i++)
res += blo[i];
int start = bel * sqrtV;
int end = x;
for (int i = start; i < end; i++)
res += point[i];
return res;
}
/// @brief Sum of [l, r).
T prod(int l, int r) const {
assert(0 <= l && l <= r && r <= V);
return query(r) - query(l);
}
};
template <class T> struct fenwick : atcoder::fenwick_tree<T> {
using atcoder::fenwick_tree<T>::fenwick_tree;
using atcoder::fenwick_tree<T>::add;
T query(int x) { return this->sum(0, x); }
T prod(int l, int r) { return this->sum(l, r); }
};
} // namespace noya
namespace noya {
template <class T, class C = noya::fenwick<T>> struct range_add_point_get {
int N;
C ST;
range_add_point_get(int _N) : ST(_N), N(_N) {}
/// @brief Add v to [l, r).
void range_add(int l, int r, T v) {
ST.add(l, v);
if (r + 1 < N) {
ST.add(r + 1, -v);
}
}
T point_get(int x) { return ST.prod(0, x + 1); }
};
} // namespace noya