D - Unique Matching Editorial
by
Kubic
Obviously, the intervals \([l_i,r_i]\) are distinct. So we can just let \(x_i=i\) and finally multiply the answer by \(n!\).
Consider the uniqueness of bipartite graph matching. Fix a perfect matching, it is equivalent to not existing augmenting cycles.
We claim that \(l,r\) is good if and only if there isn’t \(i,j\), such that \(1\le i<j\le n,r_i\ge j,l_j\le i\). It means there is no cycle of length \(2\). Moreover, it can be proven that there will be no cycle under this condition.
View \(l\) as constraints on \(r\). \(\forall l_i\le j<i\), then \(r_j<i\) must hold.
Let \(a_i=\min\limits_{l_j\le i<j}j\) (if \(j\) doesn’t exist, then \(a_i=n+1\)). We need to calculate the sum of \(\prod (a_i-i)\) for all possible sequences of \(l\).
Fixing \(a\), we consider the number of \(l\) that generates \(a\).
The main idea is to consider all maximums of \(a\), and divide \(a\) into several independent parts.
Let \(f_{i,j}\) denote the answer when only considering \([i,j]\), requiring all \(a_i\dots a_j\le j+1\), with \(l_{j+1}\) determined (\(l_{j+1}<i\)). Let \(g_{i,j}\) denote the answer when \(l_{j+1}\) is undetermined (\(l_{j+1}\ge i\)). Particularly, \(f_{i+1,i}=g_{i+1,i}=1\).
The final answer is \(f_{1,n}\).
Find the smallest \(k\) such that \(i\le k\le j\) and \(a_k=j+1\) (obviously \(a_j=j+1\), then such \(k\) exists). Now \([i,k-1]\) and \([k+1,j]\) are independent subproblems. Thus, we have:
\[ f_{i,j}=\sum\limits_{i\le k\le j}(j-k+1)g_{i,k-1}f_{k+1,j}\\ g_{i,j}=\sum\limits_{i\le k\le j}(k-i+1)(j-k+1)g_{i,k-1}f_{k+1,j} \]
Furthermore, \(f_{i,j}\) is only related to \(j-i+1\), thus it can be rewritten as follows:
\[ f_i=\sum\limits_{1\le j\le i}(i-j+1)g_{j-1}f_{i-j}\\ g_i=\sum\limits_{1\le j\le i}j(i-j+1)g_{j-1}f_{i-j} \]
Brute force runs in \(O(n^2)\) and is allowed to pass. DC & FFT solution runs in \(O(n\log^2 n)\).
posted:
last update: