差分
このページの2つのバージョン間の差分を表示します。
両方とも前のリビジョン前のリビジョン | 次のリビジョン両方とも次のリビジョン | ||
programming_algorithm:data_structure:binary_indexed_tree [2019/11/19] – [累積和の二分探索] ikatakos | programming_algorithm:data_structure:binary_indexed_tree [2020/02/26] – [累積和の二分探索] ikatakos | ||
---|---|---|---|
行 141: | 行 141: | ||
* [[https:// | * [[https:// | ||
+ | |||
+ | (※以下、sum, | ||
<sxh python> | <sxh python> | ||
class Bit: | class Bit: | ||
- | def __init__(self, | + | def __init__(self, |
- | def sum(self, i): (略) | + | self.size = n |
- | def add(self, i, x): (略) | + | self.tree = [0] * (n + 1) |
+ | self.depth = n.bit_length() | ||
+ | |||
+ | def sum(self, i): | ||
+ | s = 0 | ||
+ | while i > 0: | ||
+ | s += self.tree[i] | ||
+ | i -= i & -i | ||
+ | return s | ||
+ | |||
+ | def add(self, i, x): | ||
+ | while i <= self.size: | ||
+ | self.tree[i] += x | ||
+ | i += i & -i | ||
def lower_bound(self, | def lower_bound(self, | ||
- | """ | + | """ |
sum_ = 0 | sum_ = 0 | ||
pos = 0 | pos = 0 | ||
行 158: | 行 173: | ||
pos += 1 << i | pos += 1 << i | ||
return pos + 1, sum_ | return pos + 1, sum_ | ||
+ | |||
</ | </ | ||