algorithm_py
Algorithm template for online contests, available on
- Codeforces(pypy 3.9.10)
- Atcoder(Python 3.8.2 | pypy 3.6.9)
basic usage
algorithm template
-
- Segment Tree
- Lazy Segment Tree (Monoid)
- ZKW Segment Tree
- Fenwick Tree
- Sparse Table
- Union Find
- Encoded Dict
- Sorted List
- Sorted Blocks
-
- Quick Power
- Binomial
- Modulo Integral
- Note: this's not faster than python big number with proper modulo operations
- Prime Table
- Exgcd and Chinese remainder theorem
-
- BFS
- Colorize bipartite graph
- Topo sort
- Shortest path
- Floyd
$O(n^3)$ - Single source
-
$O(n)$ on DAG - Dijkstra
- Spfa
-
- Floyd
- Minimal spinning tree
- Prim
- Kruskal
- MaxFlow
- Dinic
-
- 1d Hash
- KMP
- Manacher
- Longest Palindromic Prefix
- Binary Trie(Min XOR)
- Trie
-
- Point (E^d)
- quaternion
- rotate
- matrix (row major)
- polar_cmp
- find_points_in_aabb
- Segment
- intersection
- point on segment
- projection of point
- distance to point / segment
- Plane
- projection of point / segment
- Matrix
- from translate
- from rotate
- from scale
- Triangle
- normal
- area
- bounding box
- centriod
- incircle (内心)
- circumcircle (外心)
- point in triangle
- uniform sample in triangle(three kinds of algorithm)
- Polygon
- is convex or not
- signed area / area
- centriod (area based)
- triangulation by earcut algorithm
- point in polygon
- Convex Hull
- 2d quick convex hull from point cloud
- Delaunay Trianglation
- Fortune algorithm (o(n log n))
- other
- Point (E^d)
-
- BitSet
errors and other notes
known pypy errors
- pypy notes
sqrt
precision error
s = int(1e18)-2
x = math.floor(math.sqrt(s))
while (x+1) * (x+1) <= s: x += 1
while x * x > s: x -= 1
complexity analysis
Big O
Big O notation is used to give an upper bound on the asymptotic growth
O(1)
: constantO(log n)
: logarithmicO(n)
: linearO(n log n)
: log-linearO(n^k)
: polynomial, wherek
is a constantO(c^n)
: exponential, wherec
is a constant
tricks
mock interactive problem
diff with brute force methods
python gen.py > data.in && \ # generate random data
python brute.py > brute.out && \ # brute force output
python e.py > e.out && \ # algorithm output
diff e.out brute.out # diff