[Python3] BFS + SortedList, keep track of remaining nodes
Evidently, we should start our investigation with the question: if there is a 1 at position node, what neighboring nodes nei could it reach in one move?\n\nLet 
Evidently, we should start our investigation with the question: if there is a `1` at position `node`, what neighboring nodes `nei` could it reach in one move?\n\nLet `i_left = max(0, node - K + 1)`, `i_right = min(node + K - 1, n - 1) - (K - 1)` be the starting positions of the subarray to be reversed.  Then, after reversing subarray `[i, i+K-1]` (for `i` in `[i_left, i_right]`), `node` goes to `nei = i + (i+K-1) - node`.  In the end, `nei` is chosen from some interval `[lo, lo+2, ..., hi]`.\n\nArmed with this information, let\'s do a standard BFS.  However, this is too slow as we are considering the same neighbors a lot.\n\nTo remedy this, let\'s keep track of which nodes haven\'t been reached by our BFS yet.  We split these by parity into `remaining[0]` and `remaining[1]`.  When we have some interval `[lo, lo+2, ..., hi]` of potential neigbhors, we can query the intersection (of this interval with unvisited nodes) quickly as we only consider each unvisited neighbor once.\n\n**Complexity**: Time $$O(N^2)$$, Space: $$O(N)$$.  (`SortedList` dominates the complexity, and for general purposes it can be thought of as contributing $$\\approx O(N \\log N)$$, but the implementation is actually $$O(N^{\\frac{4}{3}})$$, assuming the load constant is chosen as $$m = N^{\\frac{1}{3}}$$.)\n\n# Code\n```\nfrom sortedcontainers import SortedList\n\nclass Solution:\n    def minReverseOperations(self, n, p, banned_vals, K):\n        remaining = [SortedList(), SortedList()]\n        banned = set(banned_vals)\n        for u in range(n):\n            if u != p and u not in banned:\n                remaining[u & 1].add(u)\n\n        queue = [p]\n        dist = [-1] * n\n        dist[p] = 0\n        for node in queue:\n            lo = max(node - K + 1, 0)\n            lo = 2 * lo + K - 1 - node\n            hi = min(node + K - 1, n - 1) - (K - 1)\n            hi = 2 * hi + K - 1 - node\n\n            for nei in list(remaining[lo % 2].irange(lo, hi)):\n                queue.append(nei)\n                dist[nei] = dist[node] + 1\n                remaining[lo % 2].remove(nei)\n        \n        return dist\n```
['Python3']