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 Answer;
    fn size(&self) -> usize;
    fn insert(&mut self, x: usize);
    fn delete(&mut self, x: usize);
    fn answer(&self) -> Self::Answer;
}

pub struct MoSolver<S> {
    state: S,
    query: Vec<(usize, usize)>,
}

impl<S: MoOperator> MoSolver<S> {
    fn new(state: S) -> Self {
        MoSolver {
            state: state,
            query: vec![],
        }
    }
    fn add(&mut self, l: usize, r: usize) {
        self.query.push((l, r));
    }
    fn solve_with_size(&mut self, answer: &mut [S::Answer], batch: usize) {
        let state = &mut self.state;
        let size = state.size();
        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 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 {
                while l < x {
                    x -= 1;
                    state.insert(x);
                }
                while y < r {
                    state.insert(y);
                    y += 1;
                }
                while x < l {
                    state.delete(x);
                    x += 1;
                }
                while r < y {
                    y -= 1;
                    state.delete(y);
                }
                answer[k] = state.answer();
            }
        }
    }
}
// ---------- end Mo algorithm ----------

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

impl MoOperator for State {
    type Answer = u32;
    fn size(&self) -> usize {
        self.c.len()
    }
    fn insert(&mut self, x: usize) {
        let c = &mut self.cnt[self.c[x]];
        if *c == 0 {
            self.ans += 1;
        }
        *c += 1;
    }
    fn delete(&mut self, x: usize) {
        let c = &mut self.cnt[self.c[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 state = State {
        c: c.iter().cloned().collect(),
        cnt: vec![0; c.len()],
        ans: 0,
    };
    let mut solver = MoSolver::new(state);
    for &(l, r) in ask {
        solver.add(l, r);
    }
    let mut ans = vec![0; ask.len()];
    solver.solve_with_size(&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);
    }
}