Rust | Ordered Set + Binary Search
Code\nrust\nuse std::collections::BTreeSet;\n\nimpl Solution {\n    pub fn closest_room(rooms: Vec<Vec<i32>>, queries: Vec<Vec<i32>>) -> Vec<i32> {\n        let
# Code\n```rust\nuse std::collections::BTreeSet;\n\nimpl Solution {\n    pub fn closest_room(rooms: Vec<Vec<i32>>, queries: Vec<Vec<i32>>) -> Vec<i32> {\n        let mut rooms = rooms.into_iter()\n            .map(|v| (v[1] as usize, v[0] as usize))\n            .collect::<Vec<_>>();\n        rooms.sort_unstable();\n\n        let mut queries = queries.into_iter()\n            .enumerate()\n            .map(|(i, v)| ((v[1] as usize, v[0] as usize), i))\n            .collect::<Vec<_>>();\n        queries.sort_unstable();\n        queries.reverse();\n\n        let mut ret = vec![None; queries.len()];\n\n        let mut set = BTreeSet::new();\n        let mut ptr_prev = rooms.len();\n        for ((threshold, target), i) in queries {\n            let ptr_cur = rooms.partition_point(|&(w, id)| w < threshold);\n            for ptr in ptr_cur..ptr_prev {\n                let (_, id) = rooms[ptr];\n                set.insert(id);\n            }\n            ptr_prev = ptr_cur;\n\n            let bound_l = set.range(..=target).rev().next().cloned();\n            let bound_r = set.range(target..).next().cloned();\n\n            if bound_l.is_none() || bound_r.is_none() {\n                ret[i] = bound_l.or(bound_r);\n                continue;\n            }\n\n            let (bound_l, bound_r) = (bound_l.unwrap(), bound_r.unwrap());\n            let _ret = if target - bound_l > bound_r - target {\n                bound_r\n            } else {\n                bound_l\n            };\n\n            ret[i] = Some(_ret);\n        }\n\n        ret.into_iter()\n            .map(|e| e.map(|val| val as i32).unwrap_or(-1))\n            .collect::<Vec<_>>()\n    }\n}\n```
['Binary Search', 'Ordered Set', 'Rust']