mmrz's library

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


Project maintained by mm-rz Hosted on GitHub Pages — Theme by mattgraham

:heavy_check_mark: BIT(Binary Indexed Tree)
(data_structure/binary_indexed_tree.hpp)

BIT(Binary Indexed Tree)

使い方

Verified with

Code

#pragma once

#include<cassert>
#include<vector>

template<typename T>struct binary_indexed_tree {
	int n;
	std::vector<T> BIT;
	binary_indexed_tree(int n_) : n(n_ + 1), BIT(n, 0) {}

	void add(int i, T x){
		assert(1 <= i && i <= n);
		for(int idx = i;idx < n;idx += (idx & -idx)){
			BIT[idx] += x;
		}
	}
	
	T sum(int i) {
		assert(1 <= i && i <= n);
		T ret = 0;
		for(int idx = i;idx > 0;idx -= (idx & -idx)){
			ret += BIT[idx];
		}
		return ret;
	}
};
#line 2 "data_structure/binary_indexed_tree.hpp"

#include<cassert>
#include<vector>

template<typename T>struct binary_indexed_tree {
	int n;
	std::vector<T> BIT;
	binary_indexed_tree(int n_) : n(n_ + 1), BIT(n, 0) {}

	void add(int i, T x){
		assert(1 <= i && i <= n);
		for(int idx = i;idx < n;idx += (idx & -idx)){
			BIT[idx] += x;
		}
	}
	
	T sum(int i) {
		assert(1 <= i && i <= n);
		T ret = 0;
		for(int idx = i;idx > 0;idx -= (idx & -idx)){
			ret += BIT[idx];
		}
		return ret;
	}
};
Back to top page