LearnPy Hub
← Arena
AdvancedRating 2000

Different Values in a Range

Answer q questions: how many different values are there between positions l and r? Process queries offline sorted by r with a Fenwick tree.

Input

n and q; n integers; q lines 'l r'.

Output

q lines.

Sample 1

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

Sample 2

Input
68 12
6 1 9 5 3 5 9 9 8 9 9 2 8 10 6 9 1 4 10 4 5 10 9 6 9 9 5 1 5 4 4 5 8 4 6 3 9 6 4 5 2 7 10 8 5 9 3 7 3 1 9 8 6 10 8 1 10 10 3 8 7 9 6 7 4 5 4 6
17 55
27 50
1 19
1 7
34 45
11 57
58 66
22 28
40 60
30 31
44 66
5 39
Output
10
10
9
5
9
10
8
5
9
1
9
9
solution.py
Output appears here
Different Values in a Range · Python Arena problem · LearnPy Hub