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 Rust (1.42.0) (judge verdict: Time Limit Exceeded)

use proconio::{fastout, input};

use std::collections::HashSet;

struct Tree {
    n: usize,
    node: Vec<Option<HashSet<usize>>>,
}

impl Tree {
    fn new(n_: usize) -> Tree {
        let mut n = 1;
        while n < n_ {
            n *= 2;
        }
        Tree {
            n,
            node: vec![None; 2 * n],
        }
    }

    fn set(&mut self, a: usize, x: usize) {
        let mut s = HashSet::new();
        s.insert(x);
        self.node[a + self.n] = Some(s);
    }

    fn build(&mut self) {
        for a in (1..self.n).rev() {
            self.node[a] = {
                let mut s = HashSet::new();
                if let Some(t) = self.node[2 * a].as_ref() {
                    s.extend(t.iter());
                }
                if let Some(t) = self.node[2 * a + 1].as_ref() {
                    s.extend(t.iter());
                }
                Some(s)
            };
        }
    }

    // fn update(&mut self, a: usize, x: usize) {
    //     let mut a = a + self.n;
    //     self.node[a].insert(x);
    //     while a > 0 {
    //         a = a / 2;
    //         // self.node[a].extend(self.node[2 * a].iter());
    //         // self.node[a].extend(self.node[2 * a + 1].iter());
    //         self.node[a] = {
    //             let mut s = HashSet::new();
    //             s.extend(self.node[2 * a].iter());
    //             s.extend(self.node[2 * a + 1].iter());
    //             s
    //         };
    //         // self.node[a].extend(self.node[2 * a].iter());
    //         // self.node[a].extend(self.node[2 * a + 1].iter());
    //     }
    // }

    fn query(&self, a: usize, b: usize) -> Option<HashSet<usize>> {
        // if b <= l || r <= a {
        //     return None;
        // }
        // if a <= l && r <= b {
        //     return Some(self.node[k].clone());
        // }
        let mut s = HashSet::new();
        let mut a = a + self.n;
        let mut b = b + self.n;
        while a < b {
            if a & 1 == 1 {
                if let Some(t) = self.node[a].as_ref() {
                    s.extend(t.iter());
                }
                a += 1;
            }
            if b & 1 == 1 {
                b -= 1;
                if let Some(t) = self.node[b].as_ref() {
                    s.extend(t.iter());
                }
            }
            a = a / 2;
            b = b / 2;
        }
        // if let Some(vl) = self.query(a, b, 2 * k + 1, l, (l + r) / 2) {
        //     s.extend(vl);
        // }
        // if let Some(vr) = self.query(a, b, 2 * k + 2, (l + r) / 2, r) {
        //     s.extend(vr);
        // }
        return Some(s);
    }
}

#[fastout]
fn main() {
    input! {
        n: usize, q: usize,
        cc: [usize; n],
        lr: [(usize, usize); q],
    }
    let mut t = Tree::new(n);
    for i in 0..n {
        t.set(i, cc[i]);
    }
    t.build();
    for (l, r) in lr {
        if let Some(s) = t.query(l - 1, r) {
            println!("{}", s.len());
        }
    }
}