Skip to content

Instantly share code, notes, and snippets.

View Chillee's full-sized avatar

Horace He Chillee

View GitHub Profile
@Chillee
Chillee / egcd.cpp
Last active April 13, 2019 04:18
Extended Euclidean Algorithm
ll euclid(ll a, ll b, ll &x, ll &y) {
if (b) {
ll d = euclid(b, a % b, y, x);
return y -= a / b * x, d;
}
return x = 1, y = 0, a;
}
@Chillee
Chillee / dsu.cpp
Last active April 30, 2019 09:29
DSU (Disjoint Set Union)
template <int MAXN> struct Dsu {
int par[MAXN], sz[MAXN];
void init(int n) { iota(par, par + n, 0), fill(sz, sz + n, 1); }
int root(int v) { return v == par[v] ? v : (par[v] = root(par[v])); }
void merge(int a, int b) {
if ((a = root(a)) == (b = root(b)))
return;
if (sz[a] < sz[b])
swap(a, b);
@Chillee
Chillee / hash table.cpp
Last active January 15, 2022 00:53
Policy based data structures
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
struct chash {
const int RANDOM = (long long)(make_unique<char>().get()) ^ chrono::high_resolution_clock::now().time_since_epoch().count();
static unsigned long long hash_f(unsigned long long x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
@Chillee
Chillee / 2D BIT.cpp
Last active April 25, 2019 23:09
Segment tree
int N, M;
int bit[MAXN][MAXN];
int sum(int x, int y) {
int ret = 0;
for (int i = x; i >= 0; i = (i & (i + 1)) - 1)
for (int j = y; j >= 0; j = (j & (j + 1)) - 1)
ret += bit[i][j];
return ret;
}
void add(int x, int y, int delta) {
@Chillee
Chillee / sosdp.cpp
Created July 31, 2018 00:47
SOS DP (Sum over Subsets DP)
int F[1 << MAXBITS];
for (int i = 0; i <= MAXBITS; ++i) {
for (int mask = 0; mask < (1 << MAXBITS); ++mask) {
if (mask & (1 << i)) {
F[mask] = max(F[mask], F[mask ^ (1 << i)]);
}
}
}
@Chillee
Chillee / dinic.cpp
Last active December 8, 2024 16:50
Max Flow (Dinic's, HLPP)
template <int MAXV, class T = int> struct Dinic {
const static bool SCALING = false; // non-scaling = V^2E, Scaling=VElog(U) with higher constant
int lim = 1;
const T INF = numeric_limits<T>::max();
struct edge {
int to, rev;
T cap, flow;
};
int s = MAXV - 2, t = MAXV - 1;
@Chillee
Chillee / treap.cpp
Last active April 30, 2019 09:28
Treap
auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
mt19937 rnd(seed);
struct node {
node *l = nullptr, *r = nullptr;
int val, key = rnd();
int sz = 1;
node(int value) : val(value) {}
~node() { delete l, delete r; }
node *upd() {
@Chillee
Chillee / base.cpp
Last active April 2, 2020 17:04
Geometry
/** Constants Start **/
const double PI = acos(-1.0);
const double EPS = 1e-8;
const double INF = 1e9 + 5;
template <typename T> int sgn(T x) { return x < -EPS ? -1 : x > EPS; }
/** Constants End **/
/** Base Start **/
struct pt {
T x, y;
pt operator+(pt p) { return {x + p.x, y + p.y}; }
@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 / matrixexpo.cpp
Created August 8, 2018 09:24
Matrix Exponentiation
struct matrix {
vector<vector<ll>> cells;
matrix(vector<vector<ll>> input) : cells(input) {}
matrix(ll n, ll m, ll val) {
cells.resize(n);
vector<ll> row(m);
fill(row.begin(), row.end(), val);
fill(cells.begin(), cells.end(), row);
}