CF2002E
Step 1
Segments can merge. We can use a monotonic stack to simulate the process.
If the top have to same color, then merge. If they are different the shorter one wouldn't outlast the longer one, we can delete the shorter one. When we have ABA, then AA will merge when B is short, and the effective lifetime of this configuration is \(t_{A_1} + t_{A_2} - t_B\).