A std::set can’t contain duplicate elements. Try a std::multiset instead.
Edit: Sorry, a std::multiset won’t help, either - you need to keep a count of the number of each colour in the range, and use that to maintain a count of the number of different colours in the range.
Is this spoonchef or something. ssjgz already provided a test case and you are too lazy to even find out why the hell it went wrong. It’s also a small test case too.