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: Accepted)

// ---------- begin snipet ----------
pub trait MoOperator {
    type Value;
    type Answer;
    fn insert(&mut self, x: &Self::Value);
    fn delete(&mut self, x: &Self::Value);
    fn answer(&self) -> Self::Answer;
}

pub struct MoSolver<S: MoOperator> {
    array: Vec<S::Value>,
    query: Vec<(usize, usize)>,
    phantom: std::marker::PhantomData<S>,
}

impl<S: MoOperator> MoSolver<S> {
    fn new(array: Vec<S::Value>) -> Self {
        MoSolver {
            array: array,
            query: vec![],
            phantom: std::marker::PhantomData,
        }
    }
    fn add(&mut self, l: usize, r: usize) {
        self.query.push((l, r));
    }
    fn solve_with_size(&mut self, mut state: S, answer: &mut [S::Answer], batch: usize) {
        let size = self.array.len();
        let mut q = vec![vec![]; size / batch + 1];
        for (k, &(l, r)) in self.query.iter().enumerate() {
            q[l / batch].push((r, l, k));
        }
        let array = &self.array;
        let mut x = 0;
        let mut y = 0;
        for (i, mut q) in q.into_iter().enumerate() {
            q.sort_unstable_by_key(|p| p.0);
            if i % 2 == 1 {
                q.reverse();
            }
            for (r, l, k) in q {
                let min = std::cmp::min;
                for v in array[min(l, x)..x].iter().rev() {
                    state.insert(v);
                    x = l;
                }
                for v in array[min(r, y)..r].iter() {
                    state.insert(v);
                    y = r;
                }
                for v in array[min(l, x)..l].iter() {
                    state.delete(v);
                    x = l;
                }
                for v in array[min(r, y)..y].iter().rev() {
                    state.delete(v);
                    y = r;
                }
                answer[k] = state.answer();
            }
        }
    }
}
// ---------- end Mo algorithm ----------

struct State {
    cnt: Vec<u32>,
    ans: u32,
}

impl MoOperator for State {
    type Value = usize;
    type Answer = u32;
    fn insert(&mut self, x: &Self::Value) {
        let c = &mut self.cnt[*x];
        if *c == 0 {
            self.ans += 1;
        }
        *c += 1;
    }
    fn delete(&mut self, x: &Self::Value) {
        let c = &mut self.cnt[*x];
        if *c == 1 {
            self.ans -= 1;
        }
        *c -= 1;
    }
    fn answer(&self) -> Self::Answer {
        self.ans
    }
}

fn solve(c: &[usize], ask: &[(usize, usize)]) -> Vec<u32> {
    let mut solver = MoSolver::<State>::new(c.iter().cloned().collect());
    for &(l, r) in ask {
        solver.add(l, r);
    }
    let state = State {
        cnt: vec![0; c.len()],
        ans: 0,
    };
    let mut ans = vec![0; ask.len()];
    solver.solve_with_size(state, &mut ans, 1024);
    ans
}

use proconio::*;
use proconio::marker::*;

#[fastout]
fn main() {
    input! {
        n: usize,
        q: usize,
        c: [Usize1; n],
        ask: [(Usize1, usize); q],
    }
    let ans = solve(&c, &ask);
    for a in ans {
        println!("{}", a);
    }
}