2021-05-01から1ヶ月間の記事一覧

中国じょうよていり

qiita.com

fenwick treeによる転倒数と使い方

class Fenwick_Tree: def __init__(self, n): self._n = n self.data = [0] * n def add(self, p, x): assert 0 <= p < self._n p += 1 while p <= self._n: self.data[p - 1] += x p += p & -p def sum(self, l, r): assert 0 <= l <= r <= self._n return …

DFSでタイムスタンプ

IN = [0 for _ in range(N)]OUT = [0 for _ in range(N)]cnt = 0def DFS(now): global cnt IN[now] = cnt for next in Edge[now]: cnt += 1 DFS(next) cnt += 1 OUT[now] = cntDFS(0)

高速素因数分解

num = 10 ** 6 #素因数分解したい最大の数def primes(n): is_prime = [True] * (n + 1) is_prime[0] = False is_prime[1] = False for i in range(2, int(n**0.5) + 1): if not is_prime[i]: continue for j in range(i * 2, n + 1, i): is_prime[j] = False…