2025-08-06 23:55:37 +08:00
|
|
|
struct SegmentTree {
|
|
|
|
tree: Vec<i32>,
|
|
|
|
arr: Vec<i32>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SegmentTree {
|
|
|
|
pub fn new(baskets: Vec<i32>) -> Self {
|
|
|
|
let n = baskets.len();
|
|
|
|
let size = 2 << (32 - (n as u32 - 1).leading_zeros());
|
|
|
|
let mut seg = SegmentTree {
|
|
|
|
tree: vec![0; size as usize],
|
|
|
|
arr: baskets.clone(),
|
|
|
|
};
|
|
|
|
seg.build(1, 0, n - 1);
|
|
|
|
seg
|
|
|
|
}
|
|
|
|
|
|
|
|
fn push_up(&mut self, o: usize) {
|
|
|
|
self.tree[o] = self.tree[o * 2].max(self.tree[o * 2 + 1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn build(&mut self, o: usize, l: usize, r: usize) {
|
|
|
|
if l == r {
|
|
|
|
self.tree[o] = self.arr[l];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let m = (l + r) / 2;
|
|
|
|
self.build(o * 2, l, m);
|
|
|
|
self.build(o * 2 + 1, m + 1, r);
|
|
|
|
self.push_up(o);
|
|
|
|
}
|
2025-08-02 11:43:45 +08:00
|
|
|
|
2025-08-06 23:55:37 +08:00
|
|
|
pub fn find_update(&mut self, o: usize, l: usize, r: usize, x: i32) -> Option<usize> {
|
|
|
|
if self.tree[o] < x {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
if l == r {
|
|
|
|
self.tree[o] = -1;
|
|
|
|
return Some(l);
|
|
|
|
}
|
|
|
|
let m = (l + r) / 2;
|
|
|
|
let mut i = self.find_update(o * 2, l, m, x);
|
|
|
|
if i.is_none() {
|
|
|
|
i = self.find_update(o * 2 + 1, m + 1, r, x);
|
|
|
|
}
|
|
|
|
self.push_up(o);
|
|
|
|
i
|
|
|
|
}
|
|
|
|
}
|
2025-07-10 14:13:01 +08:00
|
|
|
struct Solution;
|
|
|
|
impl Solution {
|
2025-08-06 23:55:37 +08:00
|
|
|
pub fn num_of_unplaced_fruits( fruits: Vec<i32>, baskets: Vec<i32>) -> i32 {
|
|
|
|
let n = baskets.len();
|
|
|
|
let mut tree = SegmentTree::new(baskets);
|
2025-08-04 16:26:50 +08:00
|
|
|
let mut ans = 0;
|
2025-08-06 23:55:37 +08:00
|
|
|
|
|
|
|
for fruit in fruits.iter() {
|
|
|
|
if tree.find_update(1,0,n-1,*fruit).is_none() {
|
|
|
|
ans += 1;
|
2025-08-01 10:39:53 +08:00
|
|
|
}
|
2025-07-30 10:59:42 +08:00
|
|
|
}
|
2025-08-06 23:55:37 +08:00
|
|
|
ans
|
2025-07-10 14:13:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2025-08-06 23:55:37 +08:00
|
|
|
let sl = Solution::num_of_unplaced_fruits(vec![4,2,5],vec![3,5,4]);
|
2025-07-29 11:20:14 +08:00
|
|
|
println!("{:?}", sl);
|
2025-07-10 14:13:01 +08:00
|
|
|
}
|