fnupdate(&mutself, index: usize, v: i32) { letmut pos = index; self.val[pos] = v; while pos <= self.len { self.tree[pos] = self.val[pos]; letlow = Self::lowbit(pos); letmut i = 1; while i < low { self.tree[pos] = max(self.tree[pos], self.tree[pos - i]); i <<= 1; } pos += Self::lowbit(pos); } }
/// `query` 下标从`1`开始 pubfnquery(&self, mut l: i32, mut r: i32) ->i32 { letmut res = 0; l = max(1, l); while r >= l { res = max(res, self.val[r asusize]); r -= 1; while r - Self::lowbit(r asusize) asi32 >= l { res = max(res, self.tree[r asusize]); r -= Self::lowbit(r asusize) asi32; } } res } }