cpplib

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub edge2992/cpplib

:heavy_check_mark: BIT (Binary Indexed Tree)
(structure/bit.hpp)

概要

実装のヒント

参考

Verified with

Code

#pragma once
#include <vector>


template <typename T>
struct BIT {
    int N;
    std::vector<T> bit;
    BIT(size_t sz) : N(sz + 2), bit(N, 0) {}

    void add(int i, T x){
        for(int idx = i; idx <= N; idx += idx & -idx){
            bit[idx] += x;
        }
    }

    T sum(int i){
        T ret = 0;
        for(int idx = i; idx > 0; idx -= idx & -idx){
            ret += bit[idx];
        }
        return ret;
    }
};
#line 2 "structure/bit.hpp"
#include <vector>


template <typename T>
struct BIT {
    int N;
    std::vector<T> bit;
    BIT(size_t sz) : N(sz + 2), bit(N, 0) {}

    void add(int i, T x){
        for(int idx = i; idx <= N; idx += idx & -idx){
            bit[idx] += x;
        }
    }

    T sum(int i){
        T ret = 0;
        for(int idx = i; idx > 0; idx -= idx & -idx){
            ret += bit[idx];
        }
        return ret;
    }
};
Back to top page