Python 标准库(竞赛常用)

collections、heapq、bisect、math

collections

from collections import deque, Counter, defaultdict

q = deque()
q.append(x); q.popleft()   # BFS 队列 O(1)

cnt = Counter(a)
cnt[x] += 1

dd = defaultdict(list)
dd[key].append(val)

heapq(堆)

import heapq
h = []
heapq.heappush(h, x)   # 小根堆
heapq.heappop(h)
# 大根堆:压入 -x

bisect(二分)

import bisect
i = bisect.bisect_left(a, x)   # 第一个 >= x
i = bisect.bisect_right(a, x)  # 第一个 > x

math

import math
math.gcd(a, b)
math.isqrt(n)      # Python 3.8+ 整数开方
math.inf

itertools

permutationscombinationsproduct 用于暴力枚举。