Problem:
Score : 600 points

Problem Statement

We have N colored balls arranged in a row from left to right; the color of the i-th ball from the left is c_i.

You are given Q queries. The i-th query is as follows: how many different colors do the l_i-th through r_i-th balls from the left have?

Constraints

1\leq N,Q \leq 5 \times 10^5

1\leq c_i \leq N

1\leq l_i \leq r_i \leq N

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N Q
c_1 c_2 \cdots c_N
l_1 r_1
l_2 r_2
:
l_Q r_Q

Output

Print Q lines. The i-th line should contain the response to the i-th query.

Sample Input 1

4 3
1 2 1 3
1 3
2 4
3 3

Sample Output 1

2
3
1

The 1-st, 2-nd, and 3-rd balls from the left have the colors 1, 2, and 1 - two different colors.

The 2-st, 3-rd, and 4-th balls from the left have the colors 2, 1, and 3 - three different colors.

The 3-rd ball from the left has the color 1 - just one color.

Sample Input 2

10 10
2 5 6 5 2 1 7 9 7 2
5 5
2 4
6 7
2 2
7 8
7 9
1 8
6 9
8 10
6 8

Sample Output 2

1
2
2
1
2
2
6
3
3
3

Solution in OCaml (4.10.0) (judge verdict: Accepted)

module Fenwick = struct
    module type Element =
        sig
            type t
            val proc: t -> t -> t
            val zero: t
        end
    module Make(E: Element) = struct
        let create n = Array.make (n + 1) E.zero

        let update bit index value =
            let n = Array.length bit - 1 in
            let rec loop ii =
                if ii <= n then (
                    bit.(ii) <- E.proc bit.(ii) value;
                    loop (ii + (ii land (-ii)))
                )
            in
            loop index

        let query bit index = 
            let rec loop ii acc =
                if ii > 0 then (
                    loop (ii - (ii land -ii)) (E.proc bit.(ii) acc)
                ) else acc
            in
            loop index E.zero
    end
end;;
type q = { r:int; l:int; idx:int };;
Scanf.scanf "%d %d" (fun n q ->
    let solve arr n query q =
        let ans = Array.make q 0 in
        let module F = Fenwick.Make (struct type t = int let proc = (+) let zero = 0 end) in
        let bit = F.create n in
        let last_visit = Array.make 500001 (-1) in

        let rec loop i qi =
            if i < n then (
                if last_visit.(arr.(i)) <> -1 then
                    F.update bit (last_visit.(arr.(i)) + 1) (-1);
                last_visit.(arr.(i)) <- i;

                F.update bit (i + 1) 1;

                let rec loop2 qi =
                    if qi < q && query.(qi).r = i then  (
                        ans.(query.(qi).idx) <-
                            F.query bit (query.(qi).r + 1) - F.query bit (query.(qi).l);
                        loop2 (qi + 1)
                    ) else qi
                in
                loop (i + 1) (loop2 qi)
            )
        in
        loop 0 0;
        Array.iter (Printf.printf "%d\n") ans
    in
    let c = Array.init n (fun _ -> Scanf.scanf " %d" (fun a -> a)) in
    let query = Array.init q (fun i -> Scanf.scanf " %d %d" (fun l r -> { r = r - 1; l = l - 1; idx = i})) in
    Array.sort compare query;
    solve c n query q
)