Problem 1591 · Hard
Build a must-paint-before graph from each color's bounding rectangle and check that it has no cycle.
Open the interactive visualisation