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.

algorithms/fft.cpp
$ ./fft --size 1024
FFT complete. 1024 samples processed.
Frequency domain: 512 bins.
$ cat binary_lifting.cpp
// LCA with binary lifting
// O(n log n) build, O(log n) query
$ python3 solve.py < input.txt

About the Author

I'm an undergraduate at Xi'an Jiaotong University (XJTU), pursuing a deep understanding of algorithms, systems, and the principles behind computation. As an independent developer, I build tools and write about the things I learn — from competitive programming tricks to operating system internals.

This site is my public notebook. Every article, every visualization, and every line of code here represents a question I once asked myself and the answer I eventually found — or built from scratch.

Competitive Programming Algorithm Design Systems Programming Open Source

About Trunk Trick

The name comes from a simple metaphor: every complex algorithmic technique is like a tree — its trunk is the core insight, and the tricks are the clever optimizations that make it work in practice. Master the trunk first, then the tricks will follow.

Here you'll find deep dives into graph algorithms, network flow, dynamic programming, computational geometry, and more — written not as dry reference material, but as stories of exploration and discovery.

8+ Blog Posts
50+ Algorithm Articles
3 Interactive Tools

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. This is the art of turning real-world constraints into graph theory.

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.

// 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];
}
Data Structures · Trees

Binary Lifting & LCA

The Lowest Common Ancestor problem is a classic — and binary lifting solves it in $O(n \log n)$ preprocessing and $O(\log n)$ per query. Deceptively simple, yet the devil is in the details: why does depth[0] = -1 matter so much?