打卡记录
统计区间中的整数数目(动态开点线段树)
链接
class CountIntervals:
__slots__ = 'left', 'right', 'l', 'r', 'cnt'
def __init__(self, l=1, r=10 ** 9):
self.left = self.right = None
self.l, self.r, self.cnt = l, r, 0
def add(self, l: int, r: int) -> None:
if self.cnt == self.r - self.l + 1: return # self 已被完整覆盖,无需执行任何操作
if l <= self.l and self.r <= r: # self 已被区间 [l,r] 完整覆盖,不再继续递归
self.cnt = self.r - self.l + 1
return
mid = (self.l + self.r) // 2
if self.left is None: self.left = CountIntervals(self.l, mid) # 动态开点
if self.right is None: self.right = CountIntervals(mid + 1, self.r) # 动态开点
if l <= mid: self.left.add(l, r)
if mid < r: self.right.add(l, r)
self.cnt = self.left.cnt + self.right.cnt
def count(self) -> int:
return self.cnt