Chairman Tree Summary
Overview
The chairman tree is a really interesting data structure. Its full name is actually “persistent segment tree”, and as for why it's called the “chairman tree”, it's because:
Because the initials of its inventor Huang Jiatai's name are the same as those of Hu Jintao (H.J.T.), former General Secretary of the Central Committee of the Communist Party of China and President of China, this data structure can also be called the General Secretary tree or the chairman tree.
From Wikipedia.org
Structure of the Chairman Tree
The chairman tree is a persistent data structure. What does “persistent” mean? “Persistent” means we can access historical versions: every time the segment tree is operated on, a new historical version is produced, kept separate from the old version. The brute-force idea is to store a full copy on every modification, but that's guaranteed to \(MLE\), so we need to use the existing information to compress the space. Let's first think about how to make a segment tree that supports point updates and range queries persistent:

If we now operate on the node with index 3, how do we keep both the old version and the newly produced one? First, notice that when operating on a single point, at most \(log \ n\) nodes in the segment tree get modified. We can consider only these \(log \ n\) nodes. Note that we need dynamic node allocation here, because we're building a new tree inside the current system, and the usual pattern for left and right children no longer applies. Now we modify 3, make a copy of every node we pass through, and then connect the empty left and right children to the places from before:

This way we only add \(log \ n\) nodes. That saves a lot of space: if there are \(s\) operations, the space only needs to be \(n \log n + s \log n\) and we're OK.
// Example on sustainable segment tree;
const int MAX_N = 1e5 + 2000;
struct node
{
int sum, lson, rson;
} nodes[MAX_N << 4];
// the entries for the history versions;
int roots[MAX_N];
// the amount of the nodes in the system;
int tot;
// generate the tree and return the new created node id.
int build(int l, int r)
{
int p = ++tot;
if (l == r)
return p;
int mid = (l + r) >> 1;
nodes[p].lson = build(l, mid), nodes[p].rson = build(mid + 1, r);
return p;
}
// given the previous version, to generate a new version since the update;
int update(int qx, int l, int r, int pre, int val)
{
int p = ++tot;
// copy the info and update;
nodes[p] = nodes[pre], nodes[p].sum += val;
if (l == r)
return p;
int mid = (l + r) >> 1;
// to edit son nodes;
if (qx <= mid)
nodes[p].lson = update(qx, l, mid, nodes[pre].lson, val);
else
nodes[p].rson = update(qx, mid + 1, r, nodes[pre].rson, val);
return p;
}
Use Case A: The k-th Smallest Problem
Example problem: POJ2104
Before tackling this problem, it's recommended to first learn the concept of the value segment tree (the indices are the numbers, and it maintains how many times the numbers in a range appear).
The \(k\)-th smallest problem on a range can actually be solved just as easily as in a value segment tree. We can maintain prefix sums of the occurrence counts, and then, just like in a value segment tree, follow the comparisons of the counts to find the \(k\)-th largest. The query code is as follows:
int query(int l, int r, int previous, int now, int k)
{
int lsWeight = sum[nodes[now].lson] - sum[nodes[previous].lson];
if (l == r)
return l;
if (k <= lsWeight)
return query(l, mid, nodes[previous].lson, nodes[now].lson, k);
else
return query(mid + 1, r, nodes[previous].rson, nodes[now].rson, k - lsWeight);
}
Use Case B: Range Intersection of Permutations
Actually it can also be done without the permutation restriction (just discretize). For convenience, let's set up some notation: permutations \(A[1-n], B[1-m]\).
We can first record the positions where the numbers in \(A\) appear, that is, \(pos[A[i]]=i\). Then consider putting \(B\) into a chairman tree to maintain it. Still using the idea of the value segment tree, each time we insert the numbers of \(B\) into the chairman tree in order, with \(pos[B[i]]\) as the coordinate. Then, when answering \(A[l_1 - r_1] \cap B[l_2 - r_2]\), we take the occurrence-count prefix sum that version \(r_2\) gives for \([l_1 - r_1]\) minus the occurrence-count prefix sum that version \(l_2 - 1\) gives for \([l_1 - r_1]\), and that tells us how many elements the two ranges have in common.
Written as code, it looks like this:
int query(int ql, int qr, int l, int r, int p)
{
if (ql <= l && r <= qr)
return nodes[p].sum;
int mid = (l + r) >> 1, ans = 0;
if (ql <= mid)
ans = query(ql, qr, l, mid, nodes[p].lson);
if (mid < qr)
ans += query(ql, qr, mid + 1, r, nodes[p].rson);
return ans;
}
int inquire(int l1, int r1, int l2, int r2)
{
return query(l1, r1, 1, n, roots[r2]) - query(l1, r1, 1, n, roots[l1 - 1]);
}
void build(int a[], int b[], int n, int m)
{
int pos[n];
for (int i = 1; i <= n; i++)
pos[a[i]] = i;
for (int i = 1; i <= m; i++)
roots[i] = update(pos[b[i]], 1, n, roots[i - 1], 1);
}
That's all for now; I'll fill in the rest later.
催更!(