BIT
| 投稿者 | urunea |
|---|---|
| 言語 | PyPy3 |
| 更新日時 (JST) | 2026-10-24 22:42:21 |
| サイズ | 943 Byte |
| 参照URL | |
日時はサーバーが記録したものです。更新すると更新日時が変わるので、不都合がある場合は新しく投稿してください。
import bisect
class BIT:
# 0-idx
# (i, j)は両端点含む。
def __init__(self, n):
self.n = n
self.a = [0] * (n + 1)
def add(self, i, x):
i += 1
while i <= self.n:
self.a[i] += x
i += i & (-i)
def sum_sub(self, i):
i += 1
s = 0
while i > 0:
s += self.a[i]
i -= i & (-i)
return s
def sum(self, i, j):
return self.sum_sub(j) - self.sum_sub(i - 1)
# a[0]+a[1]+...+a[i] >= x となる最小のiを返す (a[k] >= 0が前提)
def lower_bound(self, x):
if x <= 0:
return 0
i = 0
r = 1
while r < self.n:
r <<= 1
length = r
while length > 0:
if i + length <= self.n and self.a[i + length] < x:
x -= self.a[i + length]
i += length
length >>= 1
return i