Create BIT.rs
This commit is contained in:
parent
6dcbcd59a7
commit
e83c084524
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue