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)

#![allow(unused, non_snake_case, dead_code, non_upper_case_globals)]
use {
    proconio::{marker::*, *},
    std::{cmp::*, collections::*, mem::*},
};

#[fastout]
fn main() {
    input! {//
        n:usize,
        q:usize,
        c:[Usize1;n],
        mut lr:[(Usize1,Usize1);q],
    };
    let mut bt = BIT::<SUM>::new(n + 111);
    let mut ql = vec![vec![]; n + 5];
    let mut qid = vec![vec![]; q + 5];
    for (i, &(l, r)) in lr.iter().enumerate() {
        ql[r].push(l);
        qid[r].push(i as usize);
    }

    let mut lst = vec![!0; n + 1];
    let mut anss = vec![0; q + 1];
    for i in 0..n {
        if lst[c[i]] != !0 {
            bt.add(lst[c[i]], &(-1));
        }
        bt.add(i, &1);
        lst[c[i]] = i;
        for j in 0usize..qid[i].len() as usize {
            anss[qid[i][j]] = bt.sum(i)
                - if ql[i][j] > 0 {
                    bt.sum(ql[i][j] - 1)
                } else {
                    0
                };
        }
    }

    for i in 0..q {
        println!("{}", anss[i]);
    }
}

pub trait Monoid {
    type T: Clone;
    fn id() -> Self::T;
    fn op(a: &Self::T, b: &Self::T) -> Self::T;
}
pub struct SUM {}
impl Monoid for SUM {
    type T = i64;
    fn id() -> Self::T {
        0
    }
    fn op(a: &Self::T, b: &Self::T) -> Self::T {
        *a + *b
    }
}
/// Generic Binary Indexed Tree
pub struct BIT<M: Monoid> {
    buf: Vec<M::T>,
}
impl<M: Monoid> BIT<M> {
    pub fn new(n: usize) -> Self {
        Self {
            buf: vec![M::id(); n + 10000],
        }
    }
    pub fn sum(&self, i: usize) -> M::T {
        let mut i = i + 100;
        if i == 0 || i == 1 {
            return M::id();
        }
        let mut s = M::id();
        while i > 0 {
            s = M::op(&s, &self.buf[i]);
            i -= 1 << i.trailing_zeros();
        }
        return s;
    }
    pub fn add(&mut self, i: usize, x: &M::T) {
        let mut i = i + 100;
        while i < self.buf.len() {
            let t = &mut self.buf[i];
            *t = M::op(&t, x);
            i += 1 << i.trailing_zeros();
        }
    }
}