1756. Design Most Recently Used Queue
Содержание
class BinaryIndexedTree:
def __init__(self, n: int):
self.n = n
self.c = [0] * (n + 1)
def update(self, x: int, v: int):
while x <= self.n:
self.c[x] += v
x += x & -x
def query(self, x: int) -> int:
s = 0
while x:
s += self.c[x]
x -= x & -x
return s
class MRUQueue:
def __init__(self, n: int):
self.q = list(range(n + 1))
self.tree = BinaryIndexedTree(n + 2010)
def fetch(self, k: int) -> int:
l, r = 1, len(self.q)
while l < r:
mid = (l + r) >> 1
if mid - self.tree.query(mid) >= k:
r = mid
else:
l = mid + 1
x = self.q[l]
self.q.append(x)
self.tree.update(l, 1)
return x
# Your MRUQueue object will be instantiated and called as such:
# obj = MRUQueue(n)
# param_1 = obj.fetch(k)