Skip to content

Instantly share code, notes, and snippets.

@dsjt
Created April 13, 2021 06:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dsjt/c1d414974ec2fbd59578dde10640481a to your computer and use it in GitHub Desktop.
Save dsjt/c1d414974ec2fbd59578dde10640481a to your computer and use it in GitHub Desktop.
ErasableHeap
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