Skip to content

Instantly share code, notes, and snippets.

View Chillee's full-sized avatar

Horace He Chillee

View GitHub Profile
@Chillee
Chillee / mod_inverse.cpp
Last active May 15, 2018 23:06
Modular Multiplicative Inverse
ll bin_exp(ll base, ll exp){
if (exp==0) return 1;
return bin_exp(base*base%MOD, exp/2)*(exp%2==1 ? base : 1)%MOD;
}
ll mod_inv(ll a){
return bin_exp(a, MOD-2);
}
@Chillee
Chillee / lis.cpp
Last active November 14, 2018 00:36
Longest Increasing Subsequence(LIS)
vector<int> ans;
for (auto x : A) {
auto it = lower_bound(ans.begin(), ans.end(), x);
if (it == ans.end())
ans.push_back(x);
else
*it = x;
}
@Chillee
Chillee / bellman-ford.cpp
Last active December 4, 2018 09:28
Min Cost Max Flow
struct MinCostFlow {
const static int MAXV = MAXN;
const int INF = 1e9 + 5;
const int s = MAXV - 2, t = MAXV - 1;
struct edge {
int from, to, cap, flow, cost;
};
int cost[MAXV][MAXV];
vector<edge> edges;
@Chillee
Chillee / adaptive.cpp
Last active December 7, 2018 00:09
Numerical Integration (Simpson's)
double simpson(function<double(double)> f, double a, double b) {
double c = (a + b) / 2;
return (b - a) / 6 * (f(a) + 4 * f(c) + f(b));
}
double rec(function<double(double)> f, double a, double b, double eps, double S, bool rel = true) {
double c = (a + b) / 2;
double S1 = simpson(f, a, c), S2 = simpson(f, c, b), T = S1 + S2;
if (abs(T - S) <= 15 * eps || b - a < 1e-10 || (rel && abs((T - S) / S) <= 15 * eps))
return T + (T - S) / 15;
return rec(f, a, c, eps / 2, S1, rel) + rec(f, c, b, eps / 2, S2, rel);
@Chillee
Chillee / centroid.cpp
Created December 7, 2018 05:12
Centroid Decomposition
int sz[MAXN];
int cpar[MAXN];
bool dead[MAXN];
int sizedfs(int cur, int p) {
sz[cur] = 1;
for (auto i : adj[cur]) {
if (i != p && !dead[i])
sz[cur] += sizedfs(i, cur);
}
return sz[cur];
@Chillee
Chillee / convex_hull_trick.cpp
Last active December 20, 2018 01:30
Convex Hull Trick
struct Line {
mutable ll m, b, p;
bool operator<(const Line &o) const { return m < o.m; }
bool operator<(const ll &x) const { return p < x; }
};
struct LineContainer : multiset<Line, less<>> { // upper convex hull.
// (for doubles, use inf = 1/.0, div(a,b) = a/b)
const ll inf = LLONG_MAX;
ll div(ll a, ll b) { return a / b - ((a ^ b) < 0 && a % b); }
@Chillee
Chillee / bump_allocator.cpp
Created December 20, 2018 16:02
Bump Allocator (Turning dynamic allocation into static allocation)
static char buf[450 << 20];
void *operator new(size_t s) {
static size_t i = sizeof buf;
assert(s < i);
return (void *)&buf[i -= s];
}
void operator delete(void *) {}
@Chillee
Chillee / merging two paths on tree.cpp
Last active December 21, 2018 16:13
Random Snippets (Stuff that's not reusable but were painful to write that I wrote nicely and am happy with)
pair<bool, array<int, 2>> canMerge(array<int, 4> pts) {
auto cmp = [&](int a, int b) { return make_pair(lca.depth[a], a) > make_pair(lca.depth[b], b); };
sort(pts.begin(), pts.end(), cmp);
do {
if (lca.dist(pts[0], pts[1]) + lca.dist(pts[1], pts[2]) + lca.dist(pts[2], pts[3]) == lca.dist(pts[0], pts[3]))
return {true, {pts[0], pts[3]}};
} while (next_permutation(pts.begin() + 1, pts.end(), cmp));
return {false, {0, 0}};
}
@Chillee
Chillee / tarjans.cpp
Last active March 11, 2019 15:27
Tarjans Strongly Connected Components (SCC)
template <int MAXN> struct SCC {
int num[MAXN], low[MAXN];
bool curProcess[MAXN];
stack<int> curVis;
int dfsCnt = 0;
SCC() { fill(begin(num), end(num), -1), fill(begin(low), end(low), -1); }
void dfs(int cur) {
num[cur] = low[cur] = dfsCnt++;
curProcess[cur] = true;
curVis.push(cur);
@Chillee
Chillee / intperm.cpp
Created March 17, 2019 08:45
Permutation Mapping: Permutations to/from integers - order preserving
int fac;
template <class It> int perm_to_int(It begin, It end) {
int x = 0, n = 0;
for (It i = begin; i != end; ++i, ++n)
if (*i < *begin)
++x;
int val = 0;
if (n > 2)
val = perm_to_int(++begin, end);
else