Data Structures19 sections · 729 units
Open in Course

Problem - Distinct Values

Count unique elements

Given an array of nn integers and qq queries, each query asks: how many distinct values are in range [l,r][l, r]? Constraints: n,q2105n, q \leq 2 \cdot 10^5, values up to 10910^9.

This problem is perfect for MO's algorithm.

Segment trees can't easily count distinct values, but extending a range by one element is simple: check if you've seen this value before in the current range. Before reading the solution, think about how you'd implement the addadd and removeremove functions.