SPOJ DQUERY

Given a sequence of n numbers, a1, a2, ..., an and a number of d-queries. A d-query is a pair (i,j) (1 ≤ i ≤ j ≤ n). For each d-query (i,j), you have to return the number of distinct elements in the subsequence ai, ai+1, ..., aj.

Input:
 * Line 1: n
 * LIne 2: n numbers (a)
 * Line 3: q, the number of d-queries
 * In the next q lines, each line contains 2 numbers i,j representing a d-query

Output: For each d-query, print the number of distinct elements in the subsequence.

Explanation
Claimed possible solutions include:
 * Sorting + range sum
 * Segment tree, merge sort tree
 * Binary indexed tree with off line algorithms
 * Square root decomposition...

Gotchas

 * Any points one can easily overlook?
 * The correct way to understand ambiguous formulations?

Implementations
Notes/Hints on actual implementation here.

Optimizations
Optimizations here.

Input
5 1 1 2 1 3 3 1 5 2 4 3 5

Output
3 2 3