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 PyPy3 (7.3.0) (judge verdict: Accepted)

# -*- coding: utf-8 -*-
from operator import add


import sys
# sys.setrecursionlimit(10**6)
# buff_readline = sys.stdin.buffer.readline
buff_readline = sys.stdin.readline
readline = sys.stdin.readline

INF = 2**62-1


def read_int():
    return int(buff_readline())


def read_int_n():
    return list(map(int, buff_readline().split()))


def read_float():
    return float(buff_readline())


def read_float_n():
    return list(map(float, buff_readline().split()))


def read_str():
    return readline().strip()


def read_str_n():
    return readline().strip().split()


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


class SegmentTree:
    def __init__(self, array):
        _len = len(array)
        self.__size = 1 << (_len - 1).bit_length()
        self.__tree = [0] * self.__size + \
            array + [0] * (self.__size - _len)
        self.__ie = 0

        for i in range(self.__size - 1, 0, -1):
            self.__tree[i] = self.__tree[i * 2] + self.__tree[i * 2 + 1]

    def update(self, i, v):
        i += self.__size
        self.__tree[i] = v
        while i:
            i //= 2
            self.__tree[i] = self.__tree[i * 2] + self.__tree[i * 2 + 1]

    def query(self, l, r):
        """[l, r)
        """
        l += self.__size
        r += self.__size
        ret = 0
        while l < r:
            if l & 1:
                ret += self.__tree[l]
                l += 1
            if r & 1:
                r -= 1
                ret += self.__tree[r]
            l //= 2
            r //= 2
        return ret

    def __getitem__(self, key):
        return self.__tree[key + self.__size]


@mt
def slv(N, Q, C, LR):
    LR.sort(key=lambda x: x[2])
    ans = [-1] * Q

    st = SegmentTree([0]*(N+1))
    la = [-1] * (N+1)
    p = 0
    for i, l, r in LR:
        l -= 1
        r -= 1
        for j in range(p, r+1):
            c = C[j]
            if la[c] != -1:
                st.update(la[c], st[la[c]]-1)
            la[c] = j
            st.update(j, st[j]+1)
        p = r+1
        ans[i] = st.query(l, r+1)
    return ans


def main():
    N, Q = read_int_n()
    C = read_int_n()
    LR = [[_, *read_int_n()] for _ in range(Q)]
    print(*slv(N, Q, C, LR), sep='\n')


if __name__ == '__main__':
    main()