1
0
Fork 0

Create BIT.rs

This commit is contained in:
Ariel 2024-01-09 17:17:27 +08:00 committed by GitHub
parent 6dcbcd59a7
commit e83c084524
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 23 additions and 0 deletions

23
trees/BIT.rs Normal file
View File

@ -0,0 +1,23 @@
struct BIT(usize, Vec<i64>);
impl BIT {
fn new(n: usize) -> Self {
Self(n, vec![0;n+1])
}
fn query(&self, mut i: usize) -> i64 {
let lowbit = |x: i64|x&-x;
let mut res = 0;
while i > 0 {
res += self.1[i];
i -= lowbit(i as i64) as usize;
}
res
}
fn insert(&mut self, mut i: usize) {
let lowbit = |x: i64| x & -x;
while i <= self.0 {
self.1[i] += 1;
i += lowbit(i as i64) as usize;
}
}
}