Olympiad in Informatics

Counting Colors — Solution

Problem Statement

Given a sequence A[] of length n, you need to answer q queries. Each query gives two endpoints and asks for the number of distinct colors in the interval. n, q < 1e5.

Main Idea

Consider building a persistent segment tree, using positions as version indices and the compressed color values as indices, recording the prefix count of each color; then just subtract the version information.