差分
このページの2つのバージョン間の差分を表示します。
両方とも前のリビジョン前のリビジョン次のリビジョン | 前のリビジョン次のリビジョン両方とも次のリビジョン | ||
programming_algorithm:data_structure:binary_indexed_tree [2019/11/08] – [累積和のlower bound] ikatakos | programming_algorithm:data_structure:binary_indexed_tree [2019/11/19] – [累積和の二分探索] ikatakos | ||
---|---|---|---|
行 51: | 行 51: | ||
注意点として、配列のindex等と異なり、添え字は1から始まる。そうしないと効率的に上下の要素を特定できないため。 | 注意点として、配列のindex等と異なり、添え字は1から始まる。そうしないと効率的に上下の要素を特定できないため。 | ||
- | | + | ⇤←←←←←←8 |
- | 4 | + | |
- | 2 6 | + | ⇤2 ⇤6 ⇤10 |
- | 1 3 5 7 9 ... | + | 1 3 5 7 9 |
行 150: | 行 150: | ||
def lower_bound(self, | def lower_bound(self, | ||
""" | """ | ||
- | | + | |
pos = 0 | pos = 0 | ||
for i in range(self.depth, | for i in range(self.depth, | ||
k = pos + (1 << i) | k = pos + (1 << i) | ||
- | if k <= self.size and sum + self.tree[k] < x: | + | if k <= self.size and sum_ + self.tree[k] < x: |
- | | + | |
pos += 1 << i | pos += 1 << i | ||
- | return pos + 1, sum | + | return pos + 1, sum_ |
</ | </ | ||