Editorial for Wesley's Anger Contest 1 Problem 4 (Hard Version) - A Red-Black Tree Problem


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Author: wleung_bvg

Please refer to the original problem's editorial for the O(NK2) dynamic programming solution.

To speed up the solution, we can observe that when computing merged, we only need to consider dp[v][i][ri][bi] and dp[w][j][rj][bj] when isize(v) and jsize(w) where size(x) is the size of the subtree of vertex x.

It can be shown that the resulting time complexity is O(N2). Proof is left as an exercise to the reader. Hint: i=1m(si2)(i=1msi)2.

Time Complexity: O(N2)


Comments

There are no comments at the moment.