Help in Algomaniac "bulbs"

I’ve been trying to solve this for quite some time, but cannot think of a data structure that would support these operations. Can someone explain what structures and sorts I can use to solve this?