// ---------- begin Mo algorithm ----------
pub trait MoOperator {
    type Value;
    type Query;
    type Answer;
    fn init(&mut self);
    fn insert(&mut self, v: &Self::Value);
    fn delete(&mut self, v: &Self::Value);
    fn answer(&mut self, q: &Self::Query) -> Self::Answer;
}

pub struct MoSolver<State: MoOperator> {
    state: State,
    array: Vec<State::Value>,
    query: Vec<(usize, usize, State::Query)>,
}

impl<State: MoOperator> MoSolver<State> {
    fn new(state: State, array: Vec<State::Value>) -> Self {
        MoSolver {
            state: state,
            array: array,
            query: vec![],
        }
    }
    fn add(&mut self, l: usize, r: usize, q: State::Query) {
        assert!(l < r && r <= self.array.len());
        self.query.push((l, r, q));
    }
    fn solve(&mut self, answer: &mut [State::Answer]) {
        assert!(self.query.len() <= answer.len());
        if self.query.is_empty() {
            return;
        }
        let size = self.array.len();
        let batch = (size as f64 / (self.query.len() as f64).sqrt()).ceil() as usize;
        let mut q = (0..(size / batch + 1)).map(|_| vec![]).collect::<Vec<_>>();
        let mut query = vec![];
        std::mem::swap(&mut query, &mut self.query);
        for (k, (l, r, op)) in query.into_iter().enumerate() {
            q[l / batch].push((r, l, k, op));
        }
        let state = &mut self.state;
        state.init();
        let mut x = 0;
        let mut y = 0;
        for (i, q) in q.iter_mut().enumerate() {
            q.sort_by_key(|p| p.0);
            if i % 2 == 1 {
                q.reverse();
            }
            for p in q.iter() {
                let (r, l, k) = (p.0, p.1, p.2);
                for v in self.array[std::cmp::min(y, r)..r].iter() {
                    state.insert(v);
                    y = r;
                }
                for v in self.array[std::cmp::min(l, x)..x].iter().rev() {
                    state.insert(v);
                    x = l;
                }
                for v in self.array[std::cmp::min(r, y)..y].iter().rev() {
                    state.delete(v);
                    y = r;
                }
                for v in self.array[std::cmp::min(x, l)..l].iter() {
                    state.delete(v);
                    x = l;
                }
                answer[k] = state.answer(&p.3);
            }
        }
    }
}
// ---------- end Mo algorithm ----------

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

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

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

#[fastout]
fn run() {
    input! {
        n: usize,
        q: usize,
        c: [Usize1; n],
        ask: [(Usize1, usize); q],
    }
    let state = State {
        cnt: vec![0; n],
        ans: 0,
    };
    let mut solver = MoSolver::new(state, c);
    for (l, r) in ask {
        solver.add(l, r, ());
    }
    let mut ans = vec![0; q];
    solver.solve(&mut ans);
    for a in ans {
        println!("{}", a);
    }
}

fn main() {
    run();
}


Measured memory_bytes: 41356