Painting the Fence
MediumRating 1700CodeforcesIntervals & Sweep Line
Approach hintTry it first, then revealHide
Count how many painters cover each section. Fix the first painter to drop, then use prefix counts of sections covered exactly once to price every choice of the second painter in O(1). O(n * q) overall.