#ifndef BINARYINDEXEDTREE_HPP
#define BINARYINDEXEDTREE_HPP
#include <stdint.h>
#include <vector>
namespace {
class BinaryIndexedTree
{
public:
template <typename T>
void init(const T& sieve)
{
size_ = sieve.size() / 2;
tree_.resize(size_);
for (int64_t i = 0; i < size_; i++)
{
tree_[i] = sieve[i * 2];
int64_t k = (i + 1) & ~i;
for (int64_t j = i; k >>= 1; j &= j - 1)
tree_[i] += tree_[j - 1];
}
}
void update(int64_t pos)
{
pos >>= 1;
do {
tree_[pos]--;
pos |= pos + 1;
}
while (pos < size_);
}
int64_t count(int64_t low, int64_t high) const
{
int64_t pos = (high - low) >> 1;
int64_t sum = tree_[pos++];
while ((pos &= pos - 1) != 0)
sum += tree_[pos - 1];
return sum;
}
private:
std::vector<int32_t> tree_;
int64_t size_ = 0;
};
}
#endif