Hiring process8 levels
  1. 1 IQ Test
  2. 2 Additional Online Test (If Required)
  3. 3 Office Interview with Hiring Manager
  4. 4 Unpaid Test Task (Motivation Check)
  5. 5 Paid Test Task and Follow-Up Interview
  6. 6 Iterative Paid Test Tasks
  7. 7 Office Verification
  8. 8 Final Interview with the Founder

Problems > Distinct Values in Ranges > Editorial

Distinct Values in Ranges — Solution & Editorial

Back to the Problem

Offline by right endpoint: sweep r left-to-right keeping, for every value, a 1 only at its LAST occurrence (moving it forward as the value reappears) in a Fenwick tree. A query (l, r) is then the range sum — each distinct value in [l, r] contributes exactly its last occurrence.

Complexity: O((N + Q) log N)

Watch out: The last-occurrence trick is the entire problem; Mo's algorithm also works but is a constant-factor gamble in slow languages.