Buggy python submission (verdict: Time Limit Exceeded):
class BinaryIndexedTree():
    def __init__(self, max_n):
        self.size = max_n + 1
        self.tree = [0] * self.size
        self.depth = self.size.bit_length()
 
    def initialize(self, seq):
        for i, x in enumerate(seq[1:], 1):
            self.tree[i] += x
            j = i + (i & (-i))
            if j < self.size:
                self.tree[j] += self.tree[i]
 
    def __repr__(self):
        return self.tree.__repr__()
 
    def get_sum(self, i):
        s = 0
        while i:
            s += self.tree[i]
            i -= i & -i
        return s
 
    def add(self, i, x):
        while i < self.size:
            self.tree[i] += x
            i += i & -i
 
    def find_kth_element(self, k):
        x, sx = 0, 0
        dx = (1 << self.depth)
        for i in range(self.depth - 1, -1, -1):
            dx = (1 << i)
            if x + dx >= self.size:
                continue
            y = x + dx
            sy = sx + self.tree[y]
            if sy < k:
                x, sx = y, sy
        return x + 1


n,q = map(int,input().split())
c = [0]+list(map(int,input().split()))
query = [list(map(int,input().split()))+[i] for i in range(q)]
query.sort(key=lambda x:x[1])

memo = [0]*(n+1)
ans = [0]*q

bit = BinaryIndexedTree(n)
now = 0
for i in range(q):
    l,r,ind = query[i]
    for j in range(now+1,r+1):
        color = c[j]
        if memo[color] == 0:
            memo[color] = j
            bit.add(j,1)
        else:
            bit.add(memo[color],-1)
            bit.add(j,1)
            memo[color] = j
    now = r
    ans[ind] = bit.get_sum(r)-bit.get_sum(l-1)

for a in ans:
    print(a)

Fixed submission:
class BinaryIndexedTree():
    def __init__(self, max_n):
        self.size = max_n + 1
        self.tree = [0] * self.size
        self.depth = self.size.bit_length()
 
    def initialize(self, seq):
        for i, x in enumerate(seq[1:], 1):
            self.tree[i] += x
            j = i + (i & (-i))
            if j < self.size:
                self.tree[j] += self.tree[i]
 
    def __repr__(self):
        return self.tree.__repr__()
 
    def get_sum(self, i):
        s = 0
        while i:
            s += self.tree[i]
            i -= i & -i
        return s
 
    def add(self, i, x):
        while i < self.size:
            self.tree[i] += x
            i += i & -i
 
    def find_kth_element(self, k):
        x, sx = 0, 0
        dx = (1 << self.depth)
        for i in range(self.depth - 1, -1, -1):
            dx = (1 << i)
            if x + dx >= self.size:
                continue
            y = x + dx
            sy = sx + self.tree[y]
            if sy < k:
                x, sx = y, sy
        return x + 1
"""
n = 2*10**5
bit = BinaryIndexedTree(n) #サイズn
bit.add(1,1) #1スタート
"""


n,q = map(int,input().split())
c = [0]+list(map(int,input().split()))
query = [list(map(int,input().split()))+[i] for i in range(q)]
query.sort(key=lambda x:x[1])

memo = [0]*(n+1)
ans = [0]*q

bit = BinaryIndexedTree(n)
now = 0
for i in range(q):
    l,r,ind = query[i]
    for j in range(now+1,r+1):
        color = c[j]
        if memo[color] == 0:
            memo[color] = j
            bit.add(j,1)
        else:
            bit.add(memo[color],-1)
            bit.add(j,1)
            memo[color] = j
    now = r
    ans[ind] = bit.get_sum(r)-bit.get_sum(l-1)

for a in ans:
    print(a)