#![allow(unused_imports, unused_macros, dead_code)]

macro_rules! min {
    (.. $x:expr) => {{
        let mut it = $x.iter();
        it.next().map(|z| it.fold(z, |x, y| min!(x, y)))
    }};
    ($x:expr) => ($x);
    ($x:expr, $($ys:expr),*) => {{
        let t = min!($($ys),*);
        if $x < t { $x } else { t }
    }}
}
macro_rules! max {
    (.. $x:expr) => {{
        let mut it = $x.iter();
        it.next().map(|z| it.fold(z, |x, y| max!(x, y)))
    }};
    ($x:expr) => ($x);
    ($x:expr, $($ys:expr),*) => {{
        let t = max!($($ys),*);
        if $x > t { $x } else { t }
    }}
}
macro_rules! ewriteln {
    ($($args:expr),*) => { let _ = writeln!(&mut std::io::stderr(), $($args),*); };
}
macro_rules! trace {
    ($x:expr) => { ewriteln!(">>> {} = {:?}", stringify!($x), $x) };
    ($($xs:expr),*) => { trace!(($($xs),*)) }
}
macro_rules! put {
    (.. $x:expr) => {{
        let mut it = $x.iter();
        if let Some(x) = it.next() { print!("{}", x); }
        for x in it { print!(" {}", x); }
        println!("");
    }};
    ($x:expr) => { println!("{}", $x) };
    ($x:expr, $($xs:expr),*) => { print!("{} ", $x); put!($($xs),*) }
}

trait Group:
    std::ops::Add<Output = Self>
    + std::ops::Sub<Output = Self>
    + std::ops::Neg<Output = Self>
    + std::iter::Sum
    + Clone
    + Copy
{
    fn zero() -> Self;
}
impl Group for i32 {
    fn zero() -> Self {
        0
    }
}
impl Group for i64 {
    fn zero() -> Self {
        0
    }
}
impl Group for f32 {
    fn zero() -> Self {
        0.0
    }
}
impl Group for f64 {
    fn zero() -> Self {
        0.0
    }
}

struct BIT<T> {
    size: usize,
    array: Vec<T>,
}
impl<T: Group> BIT<T> {
    fn new(n: usize) -> BIT<T> {
        BIT {
            size: n,
            array: vec![T::zero(); n + 1],
        }
    }
    fn add(&mut self, idx: usize, w: T) {
        let mut x = idx + 1;
        while x <= self.size {
            self.array[x] = self.array[x] + w;
            let xi = x as i32;
            x += (xi & -xi) as usize;
        }
    }
    fn sum_up(&self, idx: usize) -> T {
        // [0, idx)
        let mut sum = T::zero();
        let mut x = idx;
        while x > 0 {
            sum = sum + self.array[x];
            let xi = x as i32;
            x -= (xi & -xi) as usize;
        }
        sum
    }
    fn sum(&self, range: std::ops::Range<usize>) -> T {
        self.sum_up(range.end) - self.sum_up(range.start)
    }
    fn tovec(&self) -> Vec<T> {
        (0..self.size).map(|i| self.sum(i..i + 1)).collect()
    }
}

fn main() {
    let mut sc = Scanner::new();
    let n: usize = sc.cin();
    let q: usize = sc.cin();
    let cs: Vec<usize> = sc.vec(n);

    let mut query_by_right = vec![vec![]; n + 2];
    for i in 0..q {
        let left: usize = sc.cin::<usize>() - 1;
        let right: usize = sc.cin::<usize>() - 1;
        query_by_right[right].push((i, left, right));
    }

    let mut ans = vec![0; q];

    let mut bit = BIT::new(n + 2);
    let mut last = vec![None; n + 2];
    for i in 0..n {
        if let Some(p) = last[cs[i]] {
            bit.add(p, -1);
        }
        last[cs[i]] = Some(i);
        bit.add(i, 1);
        for &(idx, left, right) in query_by_right[i].iter() {
            ans[idx] = bit.sum(left..right + 1);
        }
    }
    for i in 0..q {
        put!(ans[i]);
    }
}

use std::collections::VecDeque;
use std::io::{self, Write};
use std::str::FromStr;

struct Scanner {
    stdin: io::Stdin,
    buffer: VecDeque<String>,
}
impl Scanner {
    fn new() -> Self {
        Scanner {
            stdin: io::stdin(),
            buffer: VecDeque::new(),
        }
    }
    fn cin<T: FromStr>(&mut self) -> T {
        while self.buffer.is_empty() {
            let mut line = String::new();
            let _ = self.stdin.read_line(&mut line);
            for w in line.split_whitespace() {
                self.buffer.push_back(String::from(w));
            }
        }
        self.buffer.pop_front().unwrap().parse::<T>().ok().unwrap()
    }
    fn chars(&mut self) -> Vec<char> {
        self.cin::<String>().chars().collect()
    }
    fn vec<T: FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.cin()).collect()
    }
}


Measured memory_bytes: 59624