Skip to content

Commit

Permalink
update binary_indexed_tree
Browse files Browse the repository at this point in the history
  • Loading branch information
idat50me committed Oct 24, 2024
1 parent e8ea88a commit 5c4a347
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
5 changes: 5 additions & 0 deletions tree/binary_indexed_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ template<typename T> struct BIT {
return sum(r) - sum(max(l - 1, 0));
}

inline T get(int idx) {
assert(0 < idx and idx <= N);
return sum(idx, idx);
}

void add(int idx, T val) {
assert(0 < idx && idx <= N);
while(idx <= N) {
Expand Down
8 changes: 5 additions & 3 deletions tree/docs/binary_indexed_tree.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ documentation_of: ../binary_indexed_tree.cpp
- `BIT(v)`:配列 `v` の要素で BIT を構築する.

## メンバ関数
- `sum(idx)``idx` 番目までの要素の総和を返す.
- `sum(l, r)``l` 番目から `r` 番目までの要素の総和を返す.
- `add(idx, val)``idx` 番目の要素に `val` を加える.
- `sum(idx)`:$[1, \mathrm{idx}]$ の要素の総和を返す.
- `sum(l, r)`:$[l, r]$ の要素の総和を返す.
- `get(idx)`:$\mathrm{idx}$ 番目の要素の値を返す.
- `add(idx, val)`:$\mathrm{idx}$ 番目の要素に `val` を加える.

`idx`, `l`, `r` は 1-indexed である.

Expand All @@ -23,4 +24,5 @@ documentation_of: ../binary_indexed_tree.cpp
- `BIT(v)`:$O(n)$
- `sum(idx)`:$O(\log n)$
- `sum(l, r)`:$O(\log n)$
- `get(idx)`:$O(\log n)$
- `add(idx, val)`:$O(\log n)$

0 comments on commit 5c4a347

Please sign in to comment.