{ } </> O(n) // [ ] *p log &&

TRUNK TRICK

Trunk Trick

An undergraduate at Xi'an Jiaotong University, an independent developer, and a competitive programmer obsessed with the craft of algorithms.

From number theory to graph theory, from data structures to dynamic programming — this is where I document every "aha!" moment along the journey. Built slowly, written carefully.

lca.cpp
// Lowest Common Ancestor
int lca(int u, int v) {
    if (depth[u] < depth[v]) swap(u, v);
    int diff = depth[u] - depth[v];
    for (int i = 0; diff; i++, diff >>= 1)
        if (diff & 1) u = up[u][i];
    if (u == v) return u;
    for (int i = LOG-1; i >= 0; i--)
        if (up[u][i] != up[v][i])
            u = up[u][i], v = up[v][i];
    return up[u][0];
}

CONTIGUITAS

Physical memory contiguity

Linux vs. Contiguitas physical address space layout Top: Linux's physical address space, with unmovable pages (gray) scattered among movable pages (light red), leaving only limited potential contiguity between any two unmovable pages. Bottom: Contiguitas's layout, where unmovable pages are confined (①) into a separate region at the left end of the address space, that region's boundary resizes dynamically (②), and unmovable pages within it are migrated by hardware from Src to Dst (③), freeing a large run of potential contiguity on the right. Unmovable Movable Potential Contiguity Linux Physical Address Space Unmovable Confinement 1 Contiguitas Physical Address Space Dst Src Unmovable Movable Potential Contiguity Hardware Migration of Unmovable 3 Dynamic Resizing 2
Linux vs. Contiguitas physical address space. Linux scatters unmovable pages (gray) across memory, leaving only short runs of contiguity; Contiguitas confines them to one end and wins back one large run.

One unmovable page inside a 2 MB block is enough to poison the whole block. Contiguitas wins the space back by not scattering those pages in the first place.

  1. 1
    ConfinementUnmovable pages are allocated only inside a dedicated region, never mixed into the movable pool.
  2. 2
    Dynamic resizingThe boundary between the two regions grows and shrinks with memory pressure.
  3. 3
    Hardware migrationHardware moves unmovable pages from Src to Dst — no software in the loop. Dropped for xv6, which has no live-device pages.
Read the xv6 implementation
π λ

TOPICS

Topics I Write About

Every topic here comes from real problems I've encountered. The visualizations are my way of making abstract ideas concrete.

Max Flow Modeling: Baseball Elimination
Graph Theory · Network Flow

Max Flow Modeling

How do you determine whether a sports team is mathematically eliminated from the playoffs? The answer is a maximum flow network — source nodes representing remaining games, team nodes with carefully chosen capacities, and a sink that reveals the truth.

Planar Graphs and Euler's Formula
Graph Theory · Planarity

Planar Graphs & Kuratowski's Theorem

Which graphs can be drawn on a plane without crossing edges? From Euler's formula $V - E + F = 2$ to the forbidden subgraphs $K_5$ and $K_{3,3}$, planarity theory sits at the intersection of discrete mathematics, topology, and computational geometry.

// Disjoint Set Union
int find(int x) {
    return p[x] == x ? x : p[x] = find(p[x]);
}
void unite(int a, int b) {
    a = find(a), b = find(b);
    if (a != b) {
        if (sz[a] < sz[b]) swap(a, b);
        p[b] = a; sz[a] += sz[b];
    }
}
Data Structures · DSU

Disjoint Set Union & Path Compression

The Union-Find data structure with path compression achieves nearly $O(1)$ amortized time per operation. Deceptively simple — just a few lines of code — yet it's the backbone of Kruskal's MST, dynamic connectivity, and countless competitive programming solutions.

T r u n k T r i c k