Skip to content

Instantly share code, notes, and snippets.

@maehrm
Last active March 14, 2026 09:41
Show Gist options
  • Select an option

  • Save maehrm/f521a0e26a0dded48b3220f5e751388c to your computer and use it in GitHub Desktop.

Select an option

Save maehrm/f521a0e26a0dded48b3220f5e751388c to your computer and use it in GitHub Desktop.
import heapq
from collections import deque
queA = deque() # まだソートされてない配列Aを表現
heapA = [] # ソート済みの配列Aを表現
Q = int(input())
for _ in range(Q):
query = list(map(int, input().split()))
if query[0] == 1:
x = query[1]
queA.append(x)
elif query[0] == 2:
if heapA:
x = heapq.heappop(heapA)
else:
x = queA.popleft()
print(x)
else:
while queA:
x = queA.popleft()
heapq.heappush(heapA, x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment