Rust/C++ BFS + Ordered Set
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(N)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(N)\n# Code\n```\n//Rust\nuse std::collections::BTreeSet;\nuse std::collections::HashSet;\nuse std::collections::VecDeque;\n\nimpl Solution {\n    pub fn min_reverse_operations(n: i32, p: i32, banned: Vec<i32>, k: i32) -> Vec<i32> {\n        let banned = banned.into_iter().collect::<HashSet<_>>();\n        let mut s = vec![BTreeSet::<i32>::new(); 2];\n        for i in 0 .. n {\n            if i != p && banned.contains(&i) == false { s[i as usize % 2].insert(i); }\n        }\n\n        let mut ret = vec![-1; n as usize];\n        let mut q = VecDeque::from([(p as usize, 0)]);\n\n        while let Some((i, cnt)) = q.pop_front() {\n            ret[i] = cnt;\n            let mut idx = p as usize % 2;\n            if k % 2 == 0 && (i as i32 - p) % 2 == 0 { idx = 1 - idx; };\n            let len = (k - 1) / 2; \n            let (d1, d2) = (len.min(i as i32), len.min(n - 1 - i as i32));\n            \n            let temp = i as i32 + (k - 1) - 2 * d1;\n            while let Some(j) = s[idx].range(temp ..).next() {\n                let j = *j;\n                if ((k - 1) - (j - i as i32)) / 2 + j >= n { break }\n                if j >= i as i32 + k { break }\n                ret[j as usize] = cnt + 1;\n                q.push_back((j as usize, cnt + 1));\n                s[idx].remove(&j);\n            }\n            \n            let temp = i as i32 - (k - 1) + 2 * d2;\n            while let Some(j) = s[idx].range(..= temp).next_back() {\n                let j = *j;\n                if ((k - 1) - (i as i32 - j)) / 2 > j { break }\n                if (i as i32 >= j + k) { break }\n                ret[j as usize] = cnt + 1;\n                q.push_back((j as usize, cnt + 1));\n                s[idx].remove(&j);\n            }\n        }\n\n        ret\n    }\n}\n```\n\n~~~\n//C++\nclass Solution {\npublic:\n    vector<int> minReverseOperations(int n, int p, vector<int>& banned, int k) {\n        vector<set<int>> s(2);\n        unordered_set<int> ban(banned.begin(), banned.end());\n        for (int i = 0; i < n; ++i) {\n            if (i != p and ban.find(i) == ban.end()) s[i % 2].insert(i);\n        }\n\n        vector<int> ret(n, -1);\n        queue<pair<int, int>> q({{p, 0}}); \n\n        while (not q.empty()) {\n            int i = q.front().first, cnt = q.front().second;\n            q.pop();\n            ret[i] = cnt;\n\n            int len = (k - 1) / 2, d1 = min(len, i), d2 = min(len, n - 1 - i);\n            int idx = p % 2;            \n            if (k % 2 == 0 and abs(i - p) % 2 == 0) idx = 1 - idx;\n    \n            while (true) {\n                auto it = s[idx].lower_bound(i + (k - 1) - 2 * d1);\n                if (it == s[idx].end()) break;\n                if (((k - 1) - (*it - i)) / 2 + *it >= n) break;\n                if (*it >= i + k) break;\n                q.push({*it, cnt + 1});\n                s[idx].erase(it);\n            }\n            \n            while (true) {\n                auto it = s[idx].upper_bound(i - (k - 1) + 2 * d2);\n                if (it == s[idx].begin()) break;\n                it = prev(it);\n                if (((k - 1) - (i - *it)) / 2 > *it) break;\n                if (i >= *it + k) break;\n                q.push({*it, cnt + 1});\n                s[idx].erase(it);\n            }        \n        }\n\n        return ret;\n    }\n};\n~~~
['Rust']