Created
April 13, 2021 06:35
-
-
Save dsjt/c1d414974ec2fbd59578dde10640481a to your computer and use it in GitHub Desktop.
ErasableHeap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from heapq import heappush, heappop, heapify | |
class ErasableHeap(object): | |
# https://qiita.com/Salmonize/items/638da118cd621d2628d1 | |
def __init__(self, minheap=True): | |
self.data = [] | |
self.event = [] | |
if minheap: | |
self.c = 1 # 最小ヒープ | |
else: | |
self.c = -1 | |
def push(self, x): | |
return heappush(self.data, self.c * x) | |
def erase(self, x): | |
return heappush(self.event, self.c * x) | |
def _check(self): | |
while self.event and\ | |
self.data[0] == self.event[0]: | |
heappop(self.data) | |
heappop(self.event) | |
def front(self): | |
self._check() | |
if len(self.data) == 0: | |
return None | |
return self.c * self.data[0] | |
def pop(self): | |
self.check() | |
return self.c * heappop(self.data) | |
def __len__(self): | |
return len(self.data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment