Approach hintTry it first, then revealHide
Sweep the array from right to left with a seen-set and store, for each index, how many distinct values its suffix contains; every query then becomes a single lookup.
Sweep the array from right to left with a seen-set and store, for each index, how many distinct values its suffix contains; every query then becomes a single lookup.